C# 如果用户名和密码存在,则查询数据库并登录
将数据库添加到web窗体项目中。名为Users的数据库表由3列组成。Id、用户名、密码。数据库中的数据是手动插入的。 若登录页面上的用户插入了正确的用户和密码,则应将其重定向到其他页面 我的c#代码:C# 如果用户名和密码存在,则查询数据库并登录,c#,asp.net,database,webforms,C#,Asp.net,Database,Webforms,将数据库添加到web窗体项目中。名为Users的数据库表由3列组成。Id、用户名、密码。数据库中的数据是手动插入的。 若登录页面上的用户插入了正确的用户和密码,则应将其重定向到其他页面 我的c#代码: 如何从数据库中获取数据并检查用户是否输入了正确的用户名和密码登录 您的方法应该是这样的 string Command = "SELECT Id FROM User WHERE Username = @Username AND Password = @Password;"; using (SqlCo
如何从数据库中获取数据并检查用户是否输入了正确的用户名和密码登录 您的方法应该是这样的
string Command = "SELECT Id FROM User WHERE Username = @Username AND Password = @Password;";
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
myConnection.Open();
using (SqlCommand myCommand = new SqlCommand(Command, myConnection))
{
myCommand.Parameters.Add("@Username", tbUser.Text);
myCommand.Parameters.Add("@Password", tbPass.Text);
return myCommand.ExecuteScalar() != null;
}
}
你的方法应该是这样的
string Command = "SELECT Id FROM User WHERE Username = @Username AND Password = @Password;";
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
myConnection.Open();
using (SqlCommand myCommand = new SqlCommand(Command, myConnection))
{
myCommand.Parameters.Add("@Username", tbUser.Text);
myCommand.Parameters.Add("@Password", tbPass.Text);
return myCommand.ExecuteScalar() != null;
}
}
理想情况下,密码不应以明文形式存储(应改为加盐和散列) 但如果它是纯文本,则会出现以下情况:
using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("select * from Users where username like @username and password = @password;");
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Connection = con;
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
bool loginSuccessful = ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0));
if (loginSuccessful)
{
Console.WriteLine("Success!");
} else {
Console.WriteLine("Invalid username or password");
}
}
理想情况下,密码不应以明文形式存储(应改为加盐和散列) 但如果它是纯文本,则会出现以下情况:
using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("select * from Users where username like @username and password = @password;");
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Connection = con;
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
bool loginSuccessful = ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0));
if (loginSuccessful)
{
Console.WriteLine("Success!");
} else {
Console.WriteLine("Invalid username or password");
}
}
将代码保留在按钮单击事件中。用户在各自的文本框中输入用户名和密码后,单击登录按钮,在该按钮单击事件中给出如下代码。在aspx文件中,给出如下标签
<asp:Label ID="lbluser" runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblpwd" runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblmessage" runat="server" Visible="false" Text="Incorrect Username and Password"></asp:Label>
将代码保留在按钮单击事件中。用户在各自的文本框中输入用户名和密码后,单击登录按钮,在该按钮单击事件中给出如下代码。在aspx文件中,给出如下标签
<asp:Label ID="lbluser" runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblpwd" runat="server" Visible="false"></asp:Label>
<asp:Label ID="lblmessage" runat="server" Visible="false" Text="Incorrect Username and Password"></asp:Label>
请参阅ADO.NET的一些基本教程。这对这个社区来说太广泛了。我同意这太广泛了。还强烈建议您阅读以下内容:。有很多方法可以让你自己出错。请参考
ADO.NET
的一些基本教程。这对这个社区来说太广泛了。我同意这太广泛了。还强烈建议您阅读以下内容:。有很多方法可以让你自己去做,谢谢你的回复。还添加了代码段以获取用户名,因为以后需要存储在会话中。下面是代码:stringusername=Convert.ToString(ds.Tables[0].Rows[0][“username]”)代码>谢谢你的回复。还添加了代码段以获取用户名,因为以后需要存储在会话中。下面是代码:stringusername=Convert.ToString(ds.Tables[0].Rows[0][“username]”)代码>Sri,恐怕您的代码易受sql注入攻击。用户输入不应成为查询的一部分-最好使用参数化查询。感谢您告知我的代码是错误的。现在我更正了它。Sri,恐怕您的代码容易受到sql注入的攻击。用户输入不应成为查询的一部分-最好使用参数化查询。感谢您告知我的代码是错误的。现在我把它取出来了。