我正在 c#.net 创建一个表单。我想gridview
用符合搜索条件的条目填充唯一的按钮单击。
我已经尝试过,但是在搜索 ID 时它可以工作,但是在搜索 FirstName 时它也会给出错误,请检查 SQL。
后面的代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=L:/New project/Project/Project/Data.accdb";
string sql = "SELECT * FROM AddressBook WHERE FirstName='" + textBox1.Text.ToString();
OleDbConnection connection = new OleDbConnection(strConn);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "AddressBook");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "AddressBook";
}
catch (System.Exception err)
{
this.label27.Visible = true;
this.label27.Text = err.Message.ToString();
}
}