我正在编写一个VS 2012 C#WinForms应用程序,其中包含我需要与数据库匹配的数据列表。列表我只包含“PROPNUM”值,如字符串,如I8KJ7SNRXY。我希望能够循环遍历列表和数据库,以查明列表中是否有相应的PROPNUM条目。
这是我的代码,它将所有信息放入列表中,
con7 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath);
ad7.SelectCommand = new OleDbCommand("SELECT b.PROPKEY FROM [PROJECT]
a INNER JOIN [PROJLIST] b on a.PROJKEY=b.PROJKEY WHERE a.NAME =
'" + (string)comboBox2.SelectedItem + "'", con7);
ds7.Clear();
con7.Open();
ad7.SelectCommand.ExecuteNonQuery();
ad7.Fill(ds7);
con7.Close();
List<string> propnumList = new List<string>();
foreach (DataRow drRow in ds7.Tables[0].Rows)
{
for (int i = 0; i < ds7.Tables[0].Columns.Count; i++)
{
propnumList.Add(drRow[i].ToString());
}
}
这是我正在尝试使用的代码,以便找到匹配的值,
foreach (string propnum in propnumList)
{
int selection = comboBox1.SelectedIndex;
string endDate = "ENDDATE";
string qual = ds2.Tables["AC_SCENARIO"].Rows[0][selection].ToString(); //if qual is null break out of for //if qual is found store and move onto next property
MessageBox.Show(qual);
ad.SelectCommand = new OleDbCommand("SELECT b.PROPNUM,
a.QUALIFIER, b.KEYWORD, b.EXPRESSION FROM [AC_ECONOMIC]
b INNER JOIN [AC_SCENARIO] a on a.PROPNUM=b.PROPNUM
WHERE QUALIFIER = '" + qual + "' AND PROPNUM = '" +
propnum + "' AND KEYWORD = '"+endDate+"' AND NOT
EXPRESSION LIKE '%[/@]%'", con);
ds2.Clear();
con.Open();
ad.SelectCommand.ExecuteNonQuery();
ad.Fill(ds2);
con.Close();
}
我收到错误消息,说“没有给出一个或多个必需参数的值”。所以我假设第二个查询有问题。如果有人可以帮我循环数据库匹配列表,将非常感激。