2013-08-28 127 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),它似乎像一個魅力工作!