我想根据switch表达式的值检查数据表中的值是否存在。 首先我从数据库加载了数据表。
SqlCommand cmd1 = new SqlCommand("Select mainMenuNum, subMenuData from [menudatas] where ivrNumber = '" + number + "';", objConnection);
SqlDataReader dataReader = cmd1.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dataReader);
这里是案例2我正在检查基于switch表达式的值是否存在于datatable中的记录。但它没有用。
switch (number)
{
case 0: TextToSpeech("No information on 0 button"); break;
case 1: TextToSpeech("Hello"); break;
case 2:
{
DataRow[] result = dt.Select("subMenuData where mainMenuNum = '" +number + "'");
if (result != null)
{ LowerMenu();
break;
}
else
{
SqlCommand cmd = new SqlCommand("Select mainMenuData from [menudatas] WHERE mainMenuNum = '" + number + "' AND ivrNumber = '" + number + "'; ", objConnection);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
data = dr.GetString(0);
}
dr.Close();
data1 = data;
objConnection.Close();
TextToSpeech(data1); break;
}
}
}
请帮我解决这个问题。谢谢
答案 0 :(得分:1)
只需编写WHERE条件而不使用WHERE关键字或任何其他信息
DataRow[] result = dt.Select("mainMenuNum = '" +number + "'");
此时,结果数组包含与过滤条件匹配的行。您可以使用
检查行的内容// Check if the result contains at least one row
if(result.Length > 0)
{
....
// if your logic requires another check on the returned columns you
// could add it to the Select expression above or just add a check here
// Here an example with a column of string type
if(result[0].Field<string>("YourColumnName") == "value")
....
....
}
您可以在MSDN上DataColumn.Expression property page的Select
方法中找到要使用的表达式规则