错误“必须声明标量变量@ username” [英] Error"must declare the scalar variable @username"

查看:148
本文介绍了错误“必须声明标量变量@ username”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我的代码有问题,特别是在登录表单上。我想登录我的应用程序,但是当我输入凭据时,我得到错误必须声明变量@UserName。我设置断点以查看该值是否为空,但事实并非如此,除UserName之外一切正常。我查看了绑定和sintax,一切正常。这个是VM:

Hello,

I have a problem with my code,specifically at the login form.I want to sign into my application,but when i enter the credentials,I get the error"Must declare the variable @UserName".I set the breakpoint to see if the value is null,but it's not,everything works except for the UserName.I looked over the binding and the sintax and everything is ok.This is the VM:

public void SubmitButton(object param)
       {

           SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\user0909\Documents\AttendanceListStudents.mdf;Integrated Security=True;Connect Timeout=30");
           try
           {



               if (conn.State == System.Data.ConnectionState.Closed)
                   conn.Open();
               String query = "SELECT COUNT (*) FROM RegisterTeacher WHERE UserName=@UserName,pwd=@pwd and pwd2=@pwd2";
               SqlCommand cmd = new SqlCommand(query, conn);
               cmd.CommandType = CommandType.Text;


               cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value =_UserName;//here I have the error
               SqlParameter Pwd = cmd.Parameters.AddWithValue("@pwd", SqlDbType.Char);

               cmd.Parameters["@pwd"].Value = TestPassword;
               if (Pwd.Value == null)
               {
                   Pwd.Value = DBNull.Value;
               }
               SqlParameter Pwd2 = cmd.Parameters.AddWithValue("@pwd2", SqlDbType.Char);
               //cmd.Parameters["@pwd"].Value = _pwd.pwd;
               cmd.Parameters["@pwd2"].Value = pwd2;
               if (Pwd2.Value == null)
               {
                   Pwd2.Value = DBNull.Value;
               }
               SqlDataAdapter da = new SqlDataAdapter(query, conn);
               DataTable dt = new DataTable();
               da.Fill(dt);
               if(dt.Rows.Count==1)
               {
                   TestPassword = dt.Rows[0][1].ToString();
                   byte[] hashBytes = Convert.FromBase64String(TestPassword);
                   byte[] salt = new byte[16];
                   Array.Copy(hashBytes, 0, salt, 0, 16);
                   var pbkdf2 = new Rfc2898DeriveBytes(TestPassword, salt, 10000);
                   byte[] hash = pbkdf2.GetBytes(20);
                   int count = Convert.ToInt32(cmd.ExecuteScalar());
                   for (int i = 0; i < 20; i++)
                   {
                       if(hashBytes[i+16]!=hash[i])
                           {
                           count = 0;
                       }if(count==1)
                       {
                           var app = new TextBoxFocusView();
                           var context = new TextBoxFocusDb();
                           app.DataContext = context;
                           app.Show();
                       }
                   }
               }



这是视图中的datacontext:


And this is the datacontext int the View:

<Label Content="Username" Foreground="White"/>
        <TextBox Name="txtusername" Text="{Binding Path=UserName}"   Background="#545d6a" Foreground="White" FontSize="18"/>
        <Label Content="Password"     Foreground="White"/>
        <PasswordBox x:Name="passwordBox"
                     MaxLength="10"
                local:PasswordHelper.BindPassword="true"
                     local:PasswordHelper.BoundPassword="{Binding Path=TestPassword, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
            PasswordChar="*"
                     Background="#545d6a"
                     Foreground="White"
         FontSize="18"/>



有人可以帮我理解问题所在吗?提前谢谢!



我尝试了什么:




Can someone please help me understand what the issue is?Thank you in advance!

What I have tried:

I have tried multiple things to make it work,I tried creating another variable and assigning the value to it,I tried this:
<a href="https://stackoverflow.com/questions/8933634/must-declare-the-scalar-variable-username">c# - Must declare the scalar variable "@UserName" - Stack Overflow</a>[<a href="https://stackoverflow.com/questions/8933634/must-declare-the-scalar-variable-username" target="_blank" title="New Window">^</a>]



c# - 必须声明标量变量@username。如何克服它 - Stack Overflow [ ^ ]

推荐答案

AddwithValue不是这样写的:

AddwithValue isn't written like that:
cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value =_UserName;//here I have the error

它应该是:

It shoudl be:

cmd.Parameters.AddWithValue("@UserName", _UserName);



修复它,它应该工作。并解决这个问题:


Fix that, it should work.And fix this as well:

cmd.ParametersAddWithValue("@pwd", TestPassword);



但是不要做那样的密码!切勿以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ] - 它可能对您来说有点先进,但只需使用代码,而不用担心它现在是如何工作的。


But don't do passwords like that! Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - it'#s probably a bit advanced for you, but just use the code, and don't worry about how it works for the moment.


这篇关于错误“必须声明标量变量@ username”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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