C#查询数据库然后将查询到的结果绑定到DATAGRIDVIEW 写到这里不会写了 帮忙看一下代码 谢谢
代码如下 数据库名字叫 db1.mdb 里面就一个表 table1 然后有3个字段 一个是主键,一个是qus 一个是 ans 主要是查询qus字段 看有没有和textbox1里面输入的汉字一样的
程序代码:private void button1_Click(object sender, System.EventArgs e)
{
if (this.textBox1 .Text =="")
MessageBox.Show ("请输入查询内容!");
else {
try
{
//查询ACCESS数据库
string constr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=db1.mdb";
string selecttxt = textBox1.Text;
OleDbConnection conn = new OleDbConnection(constr);
conn.Open();
string seclec="select qus from table1 where qus like selecttxt";
OleDbCommand cmd = new OleDbCommand(seclec, conn);
conn.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}conn.Close();上面空白的地方不会写了 请教各位前辈 诚信感谢找到解决办法了
程序代码:private void button1_Click(object sender, System.EventArgs e)
{
if (this.textBox1 .Text =="")
MessageBox.Show ("请输入查询内容!");
else {
try
{
string constr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=db1.mdb";
string selecttxt = textBox1.Text.Trim ();
OleDbConnection conn = new OleDbConnection(constr);
string seclec="select * from table1 where qus like '%" + selecttxt + "%'";
OleDbCommand cmd = new OleDbCommand(seclec, conn);
OleDbDataAdapter ada = new OleDbDataAdapter(seclec, conn);
DataTable table = new DataTable();
ada.Fill(table);
dataGridView1.DataSource = table;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
}[ 本帖最后由 risheng022 于 2011-11-5 22:33 编辑 ]




