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屋!