以下是我的应用程序的示例代码,该代码仅从客户表中检索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 :(得分:0)
您是否尝试过调试应用程序?是否抛出任何异常(隐藏在try...catch
块中的代码)?尝试单步执行代码并查看正在发生的事情。
根据提供的信息,我可以想到两个可能的错误原因:
customerIDListBox_SelectedIndexChanged
实际上没有被添加为SelectedIndexChanged
的处理程序,因此当您点击列表框时未被调用ExecuteQuery
可能会为您的查询抛出异常 - 在这种情况下,您的代码会悄然无效