Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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#_Sql_Asp.net_Sql Server - Fatal编程技术网

C# 检查数据库中的用户名和密码

C# 检查数据库中的用户名和密码,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我正在使用asp.net创建一个网站。到目前为止,我已经完成了一个注册页面,它将详细信息保存到数据库表中 如何检查该表中是否有用户名和密码,然后允许他们进入下一页 这是我的注册代码 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString); conn.Open(); string insertQuery

我正在使用asp.net创建一个网站。到目前为止,我已经完成了一个注册页面,它将详细信息保存到数据库表中

如何检查该表中是否有用户名和密码,然后允许他们进入下一页

这是我的注册代码

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString);
conn.Open();

string insertQuery = "INSERT INTO [user] (UserName, FirstName, LastName, Email, Password, Country) VALUES (@uname, @fname, @lname, @email, @password, @country)";

SqlCommand comm = new SqlCommand(insertQuery, conn);

comm.Parameters.AddWithValue("@uname", usernametextbox.Text);
comm.Parameters.AddWithValue("@fname", fnametextbox.Text);
comm.Parameters.AddWithValue("@lname", lnametextbox.Text);
comm.Parameters.AddWithValue("@email", emailtextbox.Text);
comm.Parameters.AddWithValue("@country", DropDownListcountry.Text);
comm.Parameters.AddWithValue("@password", passwordtextbox.Text);

comm.ExecuteNonQuery();

conn.Close();
我猜我需要创建一个SELECT查询,可能需要一个if语句

你在这个问题上的得票率越来越低,因为你在问一些你可以用谷歌自己很容易弄明白的问题

也就是说,是的,您需要使用SELECT语句是正确的。我不会给你确切的.NET或SQL语法,因为那样会剥夺你的学习经验。但是,请了解如何执行SELECT COUNT查询。基本上,您需要计算已经存在的带有用户名和密码的行数。不使用ExecuteOnQuery,而是使用ExecuteScalar,它返回单个值

然后在.NET代码中,您将看到返回的计数值。如果为0,则继续。如果大于0,请执行其他操作

话虽如此,.NET有一些内置工具,可以为您完成所有这些工作。找到并使用它们

将来,在来这里寻求帮助之前,试着自己花更多的时间做一些研究


祝你好运

如果您在登录页面中使用文本框作为用户名和密码

      string connectionstring = WebConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString;
                string sqlstring;
                sqlstring = "Select UserName,Password from user  where UserName='" + 
Texboxusername.Text + "' and Password ='" + Textboxpassword.Text + "'";

                SqlConnection con = new SqlConnection(connectionstring);
                SqlCommand command = new SqlCommand(sqlstring, con);

                System.Data.SqlClient.SqlDataReader reader;

                // open a connection with sqldatabase
                con.Open();

                reader = command.ExecuteReader();

                if (reader.Read())//Reader.read() true if found in database
                {

        Response.Redirect("userHome.aspx");
        }
        con.close();
第二种解决方案是使用表单身份验证。将“工具箱”中的登录添加到登录设计页面。单击两次。删除相同的代码,但改为Texboxusername.Text和texboxPassword。Text使用Login.Username和Login.Password

 if (reader.Read())
{ e.Authenticated = true;}
else{e.Authenticated=false;}
最后,在Web.config中的某处
中添加此内容


经过身份验证后,用户将自动重定向到defaultUrl,用户只能通过loginUrl访问。 如果您在UnobtrusiveValidationMode方面出现错误。 将此添加到



展示你的努力,问题出在哪里?发布一些代码!您可以查询数据库。。。或者这是重复的,请检查链接侧注释,您应该这样做。您没有给出重新创建Microsoft已经创建的内容的原因(至少现在创建了3次)。相反,我建议您阅读所选平台的教程:,或。此外,您绝对不应该以纯文本形式存储密码。:-)我看到其他人已经给了您确切的语法,以及内置的.NET工具(ASP成员身份)的名称。太糟糕了。我同意凯西的观点。显然,你不想强迫自己去学习,而是想看到结果。但我喜欢给出直接的解决方案,因为我还是有点懒:p
   <forms loginUrl="login.aspx" defaultUrl="userHome.aspx" timeout="60"/>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>