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!");
}
}
}
當我構建在customerIDListBox_SelectedIndexChanged的代碼,我只是雙擊了列表框,是不是假設自動添加處理程序? – user3350153
@ user3350153是的,在這種情況下,處理程序已連接。如果在運行時確實沒有發生異常,那麼最好的辦法是逐行檢查代碼行並檢查查詢返回的結果。順便說一句,你爲什麼刪除你的示例代碼? –
對不起,新的這個,現在將它添加回來。幾分鐘前註冊。 – user3350153