我正在使用 VS 2012 C#。我正在尝试遍历数据集以从 Access 数据库中获取信息。I have a combobox that when an item is selected it should print out all the data for that given scenario. 对于组合框中的某些值,我收到错误消息,“在调用 'Fill' 之前尚未初始化 SelectCommand 属性。” 但是对于其他一些我没有的值,并且可以毫无问题地检索信息。这是所有必要的代码,
con2.Open();
ad2 = new OleDbDataAdapter(query, con2);
ad2.Fill(ds2, "AC_SCENARIO");
con2.Close()
try
{
string end = "ENDDATE";
string start = "START";
foreach (DataRow drRow in ds2.Tables[0].Rows)
{
for (int i = 0; i <= ds2.Tables[0].Columns.Count; i++)
{
string qual0 = ds2.Tables["AC_SCENARIO"].Rows[i]["QUAL0"].ToString();
string qual1 = ds2.Tables["AC_SCENARIO"].Rows[i]["QUAL1"].ToString();
string qual2 = ds2.Tables["AC_SCENARIO"].Rows[i]["QUAL2"].ToString();
string qual3 = ds2.Tables["AC_SCENARIO"].Rows[i]["QUAL3"].ToString();
string qual4 = ds2.Tables["AC_SCENARIO"].Rows[i]["QUAL4"].ToString();
//HERE IS A SAMPLE QUERY
if (qual0 != null && (string)comboBox1.SelectedItem == qual0)
{
ad.SelectCommand = new OleDbCommand("SELECT b.RSV_CAT, b.SEQNUM, b.LEASE, b.WELL_ID, a.QUALIFIER, a.KEYWORD, a.EXPRESSION FROM [AC_ECONOMIC] a INNER JOIN [AC_PROPERTY] b on a.PROPNUM=b.PROPNUM WHERE a.KEYWORD = '"
+ end + "' AND (a.QUALIFIER = '" + qual0 + "' OR a.QUALIFIER IS NULL) AND NOT a.EXPRESSION Like '%[/@]%'", con);
}
ds.Clear();
ad.Fill(ds); //The SelectCommand property has not been initialized before calling 'Fill'.
//ERROR OCCURS HERE
con.Open();
ad.SelectCommand.ExecuteNonQuery();
con.Close();
编辑:这是骗局代码
String filePath = textBox1.Text;
con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath);
我有更多查询,例如为另一个数据集列出的查询,然后我将两个数据集合并在一起并将它们输出到 datagridview。错误发生在 ad.Fill(ds); 如果有人可以向我解释这个问题或有任何帮助,那就太好了。