Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 必须声明标量变量"@用户名";_C#_Asp.net_Webforms - Fatal编程技术网

C# 必须声明标量变量"@用户名";

C# 必须声明标量变量"@用户名";,c#,asp.net,webforms,C#,Asp.net,Webforms,我必须做一个简单的登录,当你在浏览器中插入(“)时不会崩溃,所以我需要参数化查询字符串,但出于某种原因,我得到一个错误,说: 必须声明标量变量“@UserName” 这是密码 private void DoSqlQuery() { try { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RolaConnectionString"].Connecti

我必须做一个简单的登录,当你在浏览器中插入(“)时不会崩溃,所以我需要参数化查询字符串,但出于某种原因,我得到一个错误,说:

必须声明标量变量“@UserName”

这是密码

private void DoSqlQuery() 
{
    try 
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RolaConnectionString"].ConnectionString);
        conn.Open();
        string checkUser = "select * from UserData where UserName = @UserName";
        SqlCommand com = new SqlCommand(checkUser, conn);
        com.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());

        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        conn.Close();
        if (temp == 1)
        {
            conn.Open();
            string checkPassword = "select Password from UserData where UserName = @UserName";
            SqlCommand passConn = new SqlCommand(checkPassword, conn);
            com.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
            string password = passConn.ExecuteScalar().ToString();
            conn.Close();
            if (password == txtPassword.Text)
            {
                Session["New"] = txtUserName.Text;
                Response.Write("Password is correct");
                Response.Redirect("~/LoggedIn.aspx");
            }
            else
            {
                Response.Write("Password is not correct");
            }
        }
        else
        {
            Response.Write("Username is not correct");
        }
    }
    catch(Exception e)
    {
        Response.Write(e.ToString());
    }
}

您在内部
if
语句中引用了错误的命令:

string checkPassword = "select Password from UserData where UserName = @UserName";
SqlCommand passConn = new SqlCommand(checkPassword, conn);
com.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
^^^--  should be passConn 
因此,您的第二个命令永远不会添加参数,因此您会得到您提到的错误。区分大小写也可能是一个问题,但这取决于数据库的排序规则-默认情况下SQL Server不区分大小写

其他一些与您的问题无关的建议:

  • 使用语句在
    中包装命令和连接
  • 在一次查询中查询用户名和密码(
    ,其中username=@username and password=@password
    )。黑客将首先搜索有效用户名,然后尝试使用字典攻击破解密码。试图找到匹配的组合要困难得多
  • 不要以纯文本形式存储密码-请使用
  • 或者只使用内置的安全提供者,而不是自己的

具体来说,请注意每个字符的大小写。
@Username
不等于
@Username
在第二次查询中,您有名为
passConn
SqlCommand
,但您正在将参数添加到
com
-而不是
passConn
…并且:请不要将密码存储在明文i中n你的数据库!!是的,我同意你的看法。这只是一个演示如何防止有人在文本框中插入SQL简介,或在url中插入任何内容以进入帐户。好的,谢谢,我现在就开始工作了。我将进行这些更改。谢谢你的建议和帮助。