SQL查询内嵌带参数。在执行查询时,参数不读 [英] Sql inline query with parameters. Parameter is not read when the query is executed

查看:175
本文介绍了SQL查询内嵌带参数。在执行查询时,参数不读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在c#我的SQL查询的问题,基本上是与参数在线查询,但是当我运行它,它告诉我,参数1或参数2是不存在的。

下面是在页面顶部公共宣布我的查询:

 公共常量字符串InsertStmtUsersTable =插入到用户(用户名,密码,电子邮件,userTypeID,MEMBERID,CM7Register)+
               值(@username,@Password,@email,@userTypeID,@ MEMBERID,@ CM7Register);选择@@认同;

这是我的code分配的参数,我知道我有问题,所以我两次分配PARAMS:

  =用户名(cmd.Parameters [@用户名]值=行[用户名]的ToString()。)作为串;
。cmd.Parameters [@用户名] =行[用户名]值的ToString()。

在1 methopd它调用此查询,并试图插入到表中,这里是code:

 结果= Convert.ToInt32(SqlHelper.ExecuteScalar(CON,CommandType.Text,InsertStmtUsersTable));

确切的错误信息是:必须声明变量@用户名。
难道这code是一个问题,因为所有的previous编码在此使用声明中宣布用,除了查询声明,这里使用的语句:

 使用(CMD的SqlCommand =新的SqlCommand(InsertStmtUsersTable,CON))


解决方案

这只是形形色色的丑恶。这可能是简单了很多(虽然我不知道有什么不提供SQLHelper:

 使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
    conn.Open();    的SqlCommand命令=新的SqlCommand(InsertStmtUsersTable,康涅狄格州);
    command.CommandType = CommandType.Text;    command.Parameters.Add(新的SqlParameter(用户名,userNameString));
    command.Parameters.Add(新的SqlParameter(密码,passwordString));
    command.Parameters.Add(新的SqlParameter(电子邮件,都是EmailString));
    command.Parameters.Add(新的SqlParameter(userTypeId,userTypeId));
    command.Parameters.Add(新的SqlParameter(MEMBERID,MEMBERID));
    您的参数//在这里休息    VAR的结果=(INT)command.ExecuteScalar();
}

I am having a problem with my sql query in c#, basically it's inline query with parameters, but when I run it it tells me that parameter 1 or parameter 2 is not there

here is my query declared on top of the page as public:

public const string InsertStmtUsersTable = "insert into Users (username, password, email, userTypeID, memberID, CM7Register) " +
               "Values(@username, @password, @email, @userTypeID, @memberID,@CM7Register ); select @@identity";

this is my code for assigning the parameters, I know I am having problem so I am assigning the params twice:

Username =(cmd.Parameters["@username"].Value = row["username"].ToString()) as string; 
cmd.Parameters["@username"].Value = row["username"].ToString();

In 1 methopd it calls this query and tries to insert to table, here is the code:

Result = Convert.ToInt32(SqlHelper.ExecuteScalar(con, CommandType.Text,InsertStmtUsersTable));

Exact error message is: Must declare the variable '@username'. Could this code be a problem, because all the previous coding is declared with in this using statement, except declaration of query, here the using statement:

  using (SqlCommand cmd = new SqlCommand(InsertStmtUsersTable, con))

解决方案

That is just all kinds of ugly. This could be a lot simpler (even though I'm not sure what SqlHelper does:

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    SqlCommand command = new SqlCommand(InsertStmtUsersTable, conn);
    command.CommandType = CommandType.Text;

    command.Parameters.Add(new SqlParameter("username", userNameString));
    command.Parameters.Add(new SqlParameter("password", passwordString));
    command.Parameters.Add(new SqlParameter("email", emailString));
    command.Parameters.Add(new SqlParameter("userTypeId", userTypeId));
    command.Parameters.Add(new SqlParameter("memberId", memberId));
    // Rest of your Parameters here

    var result = (int)command.ExecuteScalar();
}

这篇关于SQL查询内嵌带参数。在执行查询时,参数不读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆