我目前在 Visual Studio 中使用 C# 并且正在使用访问数据库。当从列表框中选择客户时,我试图从数据库中取回数据。当 sql 被硬编码时,这非常有效,例如
command.CommandText = "SELECT * FROM Customers WHERE CustomerID = 2 ";
但是,当我尝试使用字符串变量来存储选定的用户 ID 时,我在
"OleDbDataReader reader = command.ExecuteReader();".
我使用消息框来确认 s2 变量在选择时包含正确的 ID,因此我不确定问题所在。
有谁知道这个问题的解决方案?
private void lst_disp_SelectedIndexChanged(object sender, EventArgs e)
{
String s = (String)lst_disp.SelectedItem; // the s string contains the selected customers ID + name,
string s2 = s.Split(' ').FirstOrDefault(); // by spliting we can gain only the ID and store in s2
MessageBox.Show("Selected " + s2);
showCust(s2);
}
private void showCust(string s2)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Add("CustomerID", "Customer ID");
dataGridView1.Columns.Add("CustomerName", "Customer Name");
dataGridView1.Columns.Add("Description", "Description");
dataGridView1.Columns.Add("Email", "Email");
dataGridView1.Columns.Add("Telephone", "Telephone");
dataGridView1.Columns.Add("DeliveryAddress", "Delivery Address");
dataGridView1.Columns.Add("Notes", "Notes");
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Uni\Year 3\Final Year Project\independent foods\coding\showCustomers\Database1.accdb;Persist Security Info=False";
connect.Open();
MessageBox.Show("Connection open");
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
MessageBox.Show("SELECT * FROM Customers WHERE CustomerID = '" + s2 + "' ");
command.CommandText = "SELECT * FROM Customers WHERE CustomerID = '" + s2 + "' ";
try
{
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CustomerID"].Value = reader[0].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CustomerName"].Value = reader[1].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Description"].Value = reader[2].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Email"].Value = reader[3].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Telephone"].Value = reader[4].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["DeliveryAddress"].Value = reader[5].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Notes"].Value = reader[6].ToString();
}
}
catch(Exception e)
{
MessageBox.Show("The File cann't be read. Error: " + e.Message);
}
}