SQL Query给我一个非布尔表达式错误

时间:2013-07-24 22:29:22

标签: c# sql

我收到的错误是读取在预期条件的上下文中指定的非布尔类型的表达式,接近'20'。 20是查询字符串中的SelectedPort值我认为此查询中存在错误。

string tablename = cboNetGuid.SelectedItem.ToString();

SqlConnection sqlConnectionCmdString = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Rick\Documents\Visual Studio 2010\Projects\Server\database\ClientRegit.mdf;Integrated Security=True;User Instance=True");

//Properly Defines the string for naming the table according to the systems naming scheme
string Command = "SELECT Client_Groups, Company, Occupation, ClientID FROM [" + tablename + "] WHERE Port = [" + SelectedPort + "]";

SqlCommand sqlCommand = new SqlCommand(Command, sqlConnectionCmdString);

// will be retrieved - reading methods
// sqlComgroup.Parameters["@Network"].Value = cboNetGuid.SelectedItem.ToString();

sqlConnectionCmdString.Open(); // open database connection

// create database reader to read information from database
SqlDataReader objReader = sqlCommand.ExecuteReader();

// retrieve information from database
while (objReader.Read())
{

  cboClientGroup.Items.Add(Convert.ToString(objReader["Client_Groups"]));

  cboOccupation.Items.Add(Convert.ToString(objReader["Occupation"]));

  cboCompany.Items.Add(Convert.ToString(objReader["Company"]));

  cboClientID.Items.Add(Convert.ToString(objReader["ClientID"]));
}

objReader.Close();
sqlConnectionCmdString.Close();

1 个答案:

答案 0 :(得分:3)

您没有将SelectedPort与SQL中的任何内容进行比较。 SQL服务器需要一个类似的条件:

WHERE SelectedPort = Something