【发布时间】:2018-08-28 09:21:36
【问题描述】:
我的应用通过 openfiledialog 连接到 excel 文件。我有两个搜索,主要搜索和次要搜索。我想在一个 datagridview 中得到他们的结果。我的代码(主要搜索):
private void searchbtn_Click(object sender, EventArgs e)
{
try
{
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(connStr);
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [" + comboBox1.SelectedItem.ToString() + "] = '" + textBox5.Text + "'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
和二次搜索:
private void button1_Click(object sender, EventArgs e)
{
try
{
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(connStr);
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [" + addcb.SelectedItem.ToString() + "] = '" + addtb.Text + "'", conn);
DataTable ds = new DataTable();
da.Fill(ds);
dataGridView2.DataSource = ds;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
有什么想法吗?
【问题讨论】: