ExecuteScalar SQLException:未声明标量变量

时间:2017-03-23 12:37:03

标签: c# asp.net sql-server

我试图检查SQL-Server中的表中是否存在用户。我创建了连接字符串,打开了连接 -

            var settings = ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString;
            SqlConnection conn = new SqlConnection(settings);
            using (conn)
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                    var checkUser = "SELECT COUNT(*) FROM dbo.be_Users WHERE UserName = @UserName";
                    using (SqlCommand userquery = new SqlCommand(checkUser,conn))
                    {
                        var parma = userquery.Parameters;
                        parma.AddWithValue("@UserName", activedirectory.DisplayName);
                        int UserExist = (int)userquery.ExecuteScalar();

                        if (UserExist > 0)
                        {
                            //Username exist
                            AuthenticateUser(staffId, password, rmb);
                        }
                        else
                        {
                            //Username doesn't exist.
                            var sqlQuery = "INSERT INTO dbo.be_Users (BlogID, UserName, Password, LastLoginTime, EmailAddress, Department)" + "VALUES (@BlogID, @UserName, @Password, @LastLoginTime, @EmailAddress, @Department)";
                            using (SqlCommand qe = new SqlCommand(sqlQuery))
                            {

                                var parms = qe.Parameters;
                                parms.AddWithValue("@BlogID", Blog.CurrentInstance.Id.ToString());
                                parms.AddWithValue("@UserName", activedirectory.DisplayName); 
                                parms.AddWithValue("@Password", (passwordFormat == MembershipPasswordFormat.Hashed ? Utils.HashPassword(DEFAULT_PASSWORD) : DEFAULT_PASSWORD));
                                parms.AddWithValue("@LastLoginTime", DateTime.Now);
                                parms.AddWithValue("@EmailAddress", DEFAULT_EMAIL);
                                parms.AddWithValue("@Department", activedirectory.department);

                                qe.ExecuteNonQuery();
                            }
                            AuthenticateUser(staffId, password, rmb);
                        }
                    }conn.Close();
                }

我一直收到此错误 - 类型' System.Data.SqlClient.SqlException'发生在System.Data.dll中但未在用户代码中处理

附加信息:必须声明标量变量" @ UserName"。

异常发生在int UserExist = (int)userquery.ExecuteScalar();行。

请问,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

由于您在此处添加了参数:

parma.AddWithValue("@UserName", activedirectory.DisplayName);

我的猜测是activedirectory.DisplayName null 。 ADO.NET中的一个奇怪的故障是,null的参数未通过。要将null向下传递到数据库,您需要传递DBNull.Value。所以:

parma.AddWithValue("@UserName", ((object)activedirectory.DisplayName) ?? DBNull.Value);

或更长的手:

object displayName = activedirectory.DisplayName;
if(displayName == null) { displayName = DBNull.Value; }
parma.AddWithValue("@UserName", displayName);

然而!然后,这会引发一个问题:如果显示名称为null 此代码应该执行什么操作?显示的SQL查询显然不会以预期的方式运行。

答案 1 :(得分:0)

您好,请将您的代码更改为

var parma = new SqlParameter("@UserName",activedirectory.DisplayName);
                userquery.Parameters.Add(parma);