c#select查询不适用于参数

时间:2011-11-05 07:05:00

标签: c# winforms oledb

我遇到过这种情况,这个SQL语句不起作用..

command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = [@OA_Name]";

但是当我将['@OA_Name']更改为表中找到的值时,此SQL语句有效。

command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = ['OA 101']";

我测试了从obj.GetOperatingauthority()得到的值,它与数据库中的值完全相同。我使用了reader.hasrows来检查是否有任何行返回..

顺便说一句,我正在使用c#并访问2010数据库..

编辑:我输入了下面的大部分代码:

        using (var command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandType = CommandType.Text;

            command.Parameters.AddWithValue("@NRIC", obj.GETnricfinnumber());
            command.Parameters.AddWithValue("@Participant_Name", obj.GETname());
            command.Parameters.AddWithValue("@Gender", obj.GETgender());
            command.Parameters.AddWithValue("@DOB", obj.GETdateofbirth());
            command.Parameters.AddWithValue("@Nationality", obj.GETnationality());
            command.Parameters.AddWithValue("@Race", obj.GETrace());
            command.Parameters.AddWithValue("@Residential_Address", obj.GETresidentialaddress());
            command.Parameters.AddWithValue("@Contact_Number", obj.GETcontactnumber());
            command.Parameters.AddWithValue("@Email_Address", obj.GETemailaddress());

            command.CommandText = "SELECT [NRIC] FROM [Participant_Table] WHERE [NRIC] = [@NRIC]";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (reader.GetString(0).ToLower().Equals(obj.GETnricfinnumber().ToLower()))
                    {
                        message.show(1, "", "exists");
                        //return false;
                    }   
                }
            }

            command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = @OA_Name";
            command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());

            using (var reader = command.ExecuteReader())
            {
                if (!reader.HasRows)
                {
                    message.show(1, "", "no rows!"); return false; //to test whether has rows or no rows
                }

编辑2:getOperatingAuthority不能错,因为它基本上是一个“闭环”系统,其中OA_Name用于填充组合框,从这个组合框中选择的值用于返回表格获取ID。

3 个答案:

答案 0 :(得分:1)

尝试删除@OA_Name

的括号
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = @OA_Name";
command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());

答案 1 :(得分:0)

将其更改为:

如果你的obj.GEToperatingauthority()有一个值,请先尝试测试。

 command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = @OA_Name";

  var getoperation = Convert.ToString(obj.GEToperatingauthority());
     command.Parameters.AddWithValue("@OA_Name", getoperation );
 //use this for checking      

  var dt = new DataTable();

   var da = new SqlDataAdapter(command);

    connection.Open();

   da.Fill(dt);

   if(dt.Rows.Count > 0)
   {
     //records found
   }
   else
   {
      //No records Found
   }

此致

答案 2 :(得分:0)

尝试测试:

command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = [" + obj.GEToperatingauthority() + "]";

不是那么优雅,但它会起作用,如果它没有,那么它肯定是

的问题
command.Parameters.AddWithValue("@OA_Name", obj.GEToperatingauthority());

编辑 - 确定回应您的评论:现在试试这个:

command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = [" + "'" + obj.GEToperatingauthority() + "'" + "]";

如果有效,则缺少''