int记录错误=(int)cmd.ExecuteScalar();非法的变量名称/编号

时间:2018-09-22 06:38:25

标签: asp.net oracle

此行代码出现错误:

int records = (int)cmd.ExecuteScalar();

错误是:

  

非法的变量名称/数字

完整代码:

string strq = "select count(*) from groupmembers where group_members='" + txtgrpmem.Text + "'";
cmd = new OracleCommand(strq, Dbconn);
cmd.Parameters.AddWithValue("group_members", txtgrpmem.Text);

if (Dbconn.State == ConnectionState.Closed)
{
    Dbconn.Open();
}

int records = (int)cmd.ExecuteScalar();

if (records == 0)
{
}
else
{
}

2 个答案:

答案 0 :(得分:0)

我相信您的查询需要包含这样的group_members参数:

    string strq = "select count(*) from groupmembers where group_members=:group_members";

注意:参数名称以冒号(:)开头

答案 1 :(得分:0)

错误是因为您在group_members中使用了参数OracleCommand,但是在查询中没有使用参数。在此处查看Parameterised Query。另外,请检查OracleCommand.ParameterCheck

的链接
string strq = "select count(1) from groupmembers where group_members=:group_members";

using (OracleConnection Dbconn = new OracleConnection(CONNECTION_STRING))
{
   if (Dbconn.State == ConnectionState.Closed)
   {
      Dbconn.Open();
   }

   using (OracleCommand cmd = new OracleCommand(strq, Dbconn))
   {
       cmd.Parameters.AddWithValue("group_members", txtgrpmem.Text);

       int records = (int)cmd.ExecuteScalar();
   } 
}

还有why using?