OLEDB 参数化查询 [英] OLEDB Parameterized Query
问题描述
public void LoadDB()
{
string FileName = @"c:asdf.accdb";
string query = "SELECT ID, Field1 FROM Table1 WHERE ID=? AND Field1=?";
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName;
OleDbConnection odc = new OleDbConnection(strConn);
dAdapter = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand(query,odc);
cmd.Parameters.Add("?", OleDbType.Integer, 5).Value = 1234;
cmd.Parameters.Add("?", OleDbType.BSTR, 5).Value ="asdf";
dAdapter.SelectCommand = cmd;
ds = new DataSet();
dAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
我正在尝试使用参数化查询将访问文件绑定到 datagridview.它发现列名很好,但内容为空.
I'm trying to use parametrized query to bind the access file into the datagridview. It finds the column names fine, but the contents are empty.
我该如何解决这个问题?
How can I fix this issue?
推荐答案
在我的测试程序中,ds.Tables[0].Rows.Count 数据表实际上返回了 1 行(因为我的测试数据库中有一行返回匹配查询本身).如果您在这条线上中断,您首先应该能够看到数据是否正在进入数据表.试试这个:
In my test program, the ds.Tables[0].Rows.Count datatable actually had 1 row returned (as there was one row in my test database that matched the query itself). If you put a break on this line you should be able to see whether or not data is getting into the datatable in the first place. Try this:
dataGridView1.DataSource = ds.Tables[0];
dataGridView1的前端绑定是什么样子的?在 Access 中运行查询也可以说明情况.
What does the front end binding of dataGridView1 look like? Running the query in Access could shed some light on the situation too.
这篇关于OLEDB 参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!