2013-08-28 126 views
0

我目前正在为我在大学的第二个编程课上做最后的练习,并且在通过数据库搜索特定员工姓名时遇到问题,如果用户输入的用户名不在数据库中,程序崩溃给出错误,而不是显示我创建的错误消息。搜索C#数据库查询错误信息?

方法:

public void searchNameDbMethod() 
    { 
     OleDbConnection Myconnection = null; 
     OleDbDataReader dbReader = null; 
     string selectionText = ""; 
     bool errorFlag = true; 

     do 
     { 
      string searchName = ""; 
      Console.Write("Search for Employee Name: "); 
      searchName = Console.ReadLine(); 
      searchName = searchName.ToUpper(); 
      Console.WriteLine("\n"); 

      Myconnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= payrolldb.accdb"); 

      Myconnection.Open(); 
      selectionText = "SELECT * FROM Table1;"; 
      OleDbCommand cmd = Myconnection.CreateCommand(); 
      cmd.CommandText = selectionText; 
      dbReader = cmd.ExecuteReader(); 

      if (dbReader.HasRows) 
      { 
       while (dbReader.Read()) 
       { 
        // since the query produces one column, the GetValue(0)   
        //must be set     
        // with multiple column queries, you have to know which 
        //column holds 
        // the value that you are looking for      
        //field 0 = ID 
        dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
        if (dbName == searchName) 
        { 
         dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
         dbID = dbReader.GetValue(2).ToString(); //2 is field 2 of the db 
         dbHourlyWage = dbReader.GetValue(3).ToString(); 
         dbDependents = dbReader.GetValue(4).ToString(); 

         Console.Clear(); 
         Console.ResetColor(); 
         Console.ForegroundColor = ConsoleColor.Green; 
         Console.WriteLine("*******************************************************"); 
         Console.WriteLine("**************** {0} *****************", date); 
         Console.WriteLine("*******************************************************"); 
         Console.WriteLine("Employee Name: ", dbName); 
         Console.WriteLine("Employee ID: {0}", dbID); 
         Console.WriteLine("Hourly Wage: {0}", dbHourlyWage); 
         Console.WriteLine("Number of Dependents: {0}", dbDependents); 
         Console.WriteLine("*******************************************************"); 
         Console.ResetColor(); 
         Console.Write("\n\n"); 

         errorFlag = false; 
        }//closes if    
       }// end of while 
      }// end of if 
      if (errorFlag == true) 
      { 
       Console.ResetColor(); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Name is not in our database!");//shows the data accumulated from above  
       Console.ResetColor(); 
      }//closes if 
      dbReader.Close(); 
      Myconnection.Close(); 
     }//close do 
     while (errorFlag == true); 
    }//closes searchNameDbMethod 

的错误:http://puu.sh/4cPWU.png

请记住,我使用的Microsoft Access我的数据库为这个项目(不知道它的问题) 。

我该如何做到这一点,以便如果在数据库中找不到名称,它将显示我创建的错误消息,而不是在链接的图像中显示错误(崩溃程序)?

+1

请参阅此链接http://social.msdn.microsoft.com/Forums/en-US/1d5c04c7-157f-4955-a14b-41d912d50a64/how-to-fix-error-the-microsoftaceoledb120-provider-未在本地机器上注册 –

+0

您安装了哪种版本的访问? – Gonzix

+0

@Gonzix Access 2010 – Meta

回答

1

我想你应该在SQL-Where-Clause中搜索你的员工。

selectionText = "SELECT * FROM Table1 WHERE <EmployeeName> like @Name;"; 

然后给你的SQL查询添加一个参数。

一般我会写我的代码是这样的:

void searchNameDbMethod() 
{ 
    OleDbConnection Myconnection = null; 
    OleDbDataReader dbReader = null; 
    string selectionText = ""; 


    string searchName = ""; 
    Console.Write("Search for Employee Name: "); 
    searchName = Console.ReadLine(); 
    searchName = searchName.ToUpper(); 
    Console.WriteLine("\n"); 

    Myconnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= payrolldb.accdb"); 

    Myconnection.Open(); 
    selectionText = "SELECT * FROM Table1 WHERE Employee like @Name;"; 
    OleDbCommand cmd = Myconnection.CreateCommand(); 
    cmd.CommandText = selectionText; 

    cmd.Parameters.Add(new OleDbParameter() { ParameterName = "@Name", Value = searchName, DbType = System.Data.DbType.String }); 

    try 
    { 
     dbReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 

     while (dbReader.Read()) 
     { 
      // since the query produces one column, the GetValue(0)   
      //must be set     
      // with multiple column queries, you have to know which 
      //column holds 
      // the value that you are looking for      
      //field 0 = ID 
      string dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
      if (dbName == searchName) 
      { 
       dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
       string dbID = dbReader.GetValue(2).ToString(); //2 is field 2 of the db 
       string dbHourlyWage = dbReader.GetValue(3).ToString(); 
       string dbDependents = dbReader.GetValue(4).ToString(); 

       Console.Clear(); 
       Console.ResetColor(); 
       Console.ForegroundColor = ConsoleColor.Green; 
       Console.WriteLine("*******************************************************"); 
       Console.WriteLine("**************** {0} *****************", date); 
       Console.WriteLine("*******************************************************"); 
       Console.WriteLine("Employee Name: ", dbName); 
       Console.WriteLine("Employee ID: {0}", dbID); 
       Console.WriteLine("Hourly Wage: {0}", dbHourlyWage); 
       Console.WriteLine("Number of Dependents: {0}", dbDependents); 
       Console.WriteLine("*******************************************************"); 
       Console.ResetColor(); 
       Console.Write("\n\n"); 
      } 
     } 
    } 
    catch 
    { 
     if (dbReader != null) 
     { 
      dbReader.Close(); 
     } 
    } 
    finally 
    { 
     if (Myconnection != null) 
     { 
      Myconnection.Close(); 
     } 
    } 
} 

只是一个例子,以提高您的解决方案。

+0

在这种情况下,我会在哪里放置自定义错误消息“名称不在我们的数据库中!” ?新名称参数行也需要在查询行之上(这种方式在使用之前声明)。 – Meta

+0

哦,我犯了一个错误,你不需要** if(dbName == searchName)** anmyore。但是你可以在你的while循环中做一个计数器并计算找到的雇员。 – BendEg

+0

随着您发布的代码,我得到以下错误http://puu.sh/4cR3b.png以前从未使用过这个东西,所以我不知道该怎么做才能解决这个问题?你怎么建议做这样的事情呢? *即时通讯对编程仍然很新颖,并不是100%熟悉很多东西* – Meta

0

我能够基于一些由@BendEg

这里提供的代码来算起来我们的是我做过什么:

public void searchNameDbMethod() 
    { 
     OleDbConnection Myconnection = null; 
     OleDbDataReader dbReader = null; 
     string selectionText = ""; 
     bool errorFlag = true; 

     do 
     { 
      string searchName = ""; 
      Console.Write("Search for Employee Name: "); 
      searchName = Console.ReadLine(); 
      searchName = searchName.ToUpper(); 
      Console.WriteLine("\n"); 

      Myconnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= payrolldb.accdb"); 

      Myconnection.Open(); 
      selectionText = "SELECT * FROM Table1 WHERE employee_name='" + searchName + "'"; 
      OleDbCommand cmd = Myconnection.CreateCommand(); 
      cmd.CommandText = selectionText; 
      dbReader = cmd.ExecuteReader(); 

      if (dbReader.HasRows) 
      { 
       while (dbReader.Read()) 
       { 
        // since the query produces one column, the GetValue(0)   
        //must be set     
        // with multiple column queries, you have to know which 
        //column holds 
        // the value that you are looking for      
        //field 0 = ID 
        dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
        if (dbName == searchName) 
        { 
         dbName = dbReader.GetValue(1).ToString();   //1 is field 1 of the db 
         dbID = dbReader.GetValue(2).ToString(); //2 is field 2 of the db 
         dbHourlyWage = dbReader.GetValue(3).ToString(); 
         dbDependents = dbReader.GetValue(4).ToString(); 

         Console.Clear(); 
         Console.ResetColor(); 
         Console.ForegroundColor = ConsoleColor.Green; 
         Console.WriteLine("*******************************************************"); 
         Console.WriteLine("**************** {0} *****************", date); 
         Console.WriteLine("*******************************************************"); 
         Console.WriteLine("Employee Name: ", dbName); 
         Console.WriteLine("Employee ID: {0}", dbID); 
         Console.WriteLine("Hourly Wage: {0}", dbHourlyWage); 
         Console.WriteLine("Number of Dependents: {0}", dbDependents); 
         Console.WriteLine("*******************************************************"); 
         Console.ResetColor(); 
         Console.Write("\n\n"); 

         errorFlag = false; 
        }//closes if    
       }// end of while 
      }// end of if 
      else if (!dbReader.HasRows) 
      { 
       Console.ResetColor(); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Name is not in our database!");//shows the data accumulated from above  
       Console.ResetColor(); 
      }//closes if 
      dbReader.Close(); 
      Myconnection.Close(); 
     }//close do 
     while (errorFlag == true); 
    }//closes searchNameDbMethod 

正如你可以看到我改变了我的询问像他建议(但没有额外的复杂的东西),并改变我的if(errorFlag == true)语句,否则如果(!dbReader.HasRows),它似乎像一个魅力工作!