2014-02-25 68 views
0

以下是我的应用程序的示例代码,它只从客户表中检索ID,但是当点击customerIDListBox以查看客户信息时,不检索相关信息。无法检索客户表信息

public partial class Form1 : Form 
{ 
    OleDbConnection dbConn; 

    private void ConnectToDatabase() 
    { 
     //initialise the connection 
     dbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; 
     User Id=; 
     Password=; 
     Data Source=DMarx v1.0.mdb"); 

     //open connection 
     dbConn.Open(); 
    } 

    /* private void DisconnectFromDatabase() 
    *{ 
    * dbConn.Close(); 
    *} 
    */ 
    private OleDbDataReader ExecuteQuery(string query) 
    { 
     /* 
     * try...catch because ExecuteReader can throw an exception 
     * if the query is incorrect, or if the database has not yet been connected 
     */ 
     try 
     { 
      //create the command object 
      OleDbCommand cmd = dbConn.CreateCommand(); 
      //assign the query to it 
      cmd.CommandText = query; 
      //execute the command 
      return cmd.ExecuteReader(); 
     } 
     catch (OleDbException) 
     { 
      //if an exception ocurrs, return nothing 
      return null; 
     } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     //connect to the database 
     ConnectToDatabase(); 

     //select all the customer id's from the database 
     string query = "SELECT ID FROM Customer;"; 
     OleDbDataReader dbReader = ExecuteQuery(query); 

     //if any rows were returned 
     if (dbReader != null && dbReader.HasRows) 
     { 
      //dbReader.Read() will return true until there are 
      //no more rows to be read 
      while (dbReader.Read()) 
      { 
       customerIDListBox.Items.Add(dbReader["ID"]); 
      } 
     } 

     //we are finished with the database for now, disconnect 
     // DisconnectFromDatabase(); 
    } 

    private void customerIDListBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ConnectToDatabase(); 

     //construct the query string 
     string queryString = "SELECT Name, Surname, TelNo FROM"; 
     queryString = queryString + "Customer WHERE ID = " + customerIDListBox.SelectedItem + ";"; 

     //execute the query 
     OleDbDataReader dbReader = ExecuteQuery(queryString); 

     //if we have results 
     if (dbReader != null && dbReader.HasRows) 
     { 
      //we are only expecting one row, so it is not neccessary to iterate through the results 
      dbReader.Read(); 
      nameTextBox.Text = dbReader["Name"].ToString(); 
      surnameTextBox.Text = dbReader["Surname"].ToString(); 
      telephoneNumberBox.Text = dbReader["TelNo"].ToString(); 
     } 

     //now to get the clients accounts 
     queryString = "SELECT Id, AccountType, Balance FROM Account WHERE ClientId = "; 
     queryString = queryString + customerIDListBox.SelectedItem + ";"; 

     //clear the accounts list (so that only the currently selected client's accounts are shown) 
     accountsListBox.Items.Clear(); 

     //execute the query 
     if (dbReader != null && dbReader.HasRows) 
     { 
      while (dbReader.Read()) 
      { 
       //construct a string with the details 
       String accountDetails = dbReader["Id"] + " - "; 
       accountDetails = accountDetails + dbReader["AccountType"] + " - "; 
       accountDetails = accountDetails + dbReader["Balance"]; 

       //add the string to the accounts list box 
       accountsListBox.Items.Add(accountDetails); 
      } 
     } 

     // DisconnectFromDatabase(); 

    } 

    private void saveChangesButton_Click(object sender, EventArgs e) 
    { 
     //make sure the use has selected something before clicking the button 
     if (customerIDListBox.SelectedItems.Count > 0) 
     { 
      ConnectToDatabase(); 

      //create the query 
      String queryString = "UPDATE Customer SET Name = '" + nameTextBox.Text + "', Surname = '"; 
      queryString = queryString + surnameTextBox.Text + "', TelNo= '"; 
      queryString = queryString + telephoneNumberBox.Text + "'"; 
      queryString = queryString + " WHERE Id = " + customerIDListBox.SelectedItem + ";"; 

      ExecuteQuery(queryString); 

     // DisconnectFromDatabase(); 
     } 
     else 
     { 
      MessageBox.Show("Please select a customer first!"); 
     } 
    } 
} 

回答

0

您是否尝试过调试您的应用程序?是否有任何例外(隐藏在您的try...catch块中的代码)?尝试单步执行代码并查看正在发生的事情。

基于提供我能想到的两个可能的原因的错误信息:

  • customerIDListBox_SelectedIndexChanged没有实际添加的处理程序的SelectedIndexChanged,当你点击列表框,因此不能被称为
  • ExecuteQuery可能会抛出异常的查询 - 在这种情况下,你的代码静静地什么也不做
+0

当我构建在customerIDListBox_SelectedIndexChanged的代码,我只是双击了列表框,是不是假设自动添加处理程序? – user3350153

+0

@ user3350153是的,在这种情况下,处理程序已连接。如果在运行时确实没有发生异常,那么最好的办法是逐行检查代码行并检查查询返回的结果。顺便说一句,你为什么删除你的示例代码? –

+0

对不起,新的这个,现在将它添加回来。几分钟前注册。 – user3350153