如何使用 SQL 参数在 C# 中编辑登录表单?
Posted
技术标签:
【中文标题】如何使用 SQL 参数在 C# 中编辑登录表单?【英文标题】:How can i edit Login Form in C# using SQL parameters? 【发布时间】:2021-08-28 12:27:19 【问题描述】:所以我有登录表单,我需要在其中插入 sql 数据库中的名称和密码,但由于我的字段是 nvarchar,因此出现错误,所以我想使用 sql 参数对其进行编辑。
“回车”按钮代码:
private void button1_Click(object sender, EventArgs e)
if (DocName.Text == "" || PassTb.Text == "")
MessageBox.Show("Enter login and password");
else
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select Count(*) from Doctor where DocName='"+DocName.Text+"' and DocPass='"+PassTb.Text+"'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
Home H = new Home();
H.Show();
this.Hide();
else
MessageBox.Show("Invalid login or password");
conn.Close();
我想如果我使用 cmd.parameters.AddWithValue 它会解决我的问题,但我不知道该怎么做,所以我会很高兴得到任何帮助
编辑:好的,所以我实际上编辑了代码并添加了参数,但我仍然无法弄清楚如何继续使用 DataAdapter..所以它给出了一个错误“System.Data.SqlClient.SqlException:”必须声明标量变量“@DocName”。”
按钮的编辑代码:
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Doctor " +
"WHERE DocName = @DocName AND DocPass = @DocPass", conn);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@DocName", SqlDbType.NVarChar, 50);
command.Parameters.Add("@DocName", SqlDbType.NVarChar, 50);
adapter.SelectCommand = command;
return adapter;
private void button1_Click(object sender, EventArgs e)
conn.Open();
SqlDataAdapter sda = CreateCustomerAdapter(conn);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
Home H = new Home();
H.Show();
this.Hide();
else
MessageBox.Show("Invalid login or password");
conn.Close();
【问题讨论】:
Don't useAddWithValue
, it's evil.
虽然没有 SQL 注入那么邪恶。或以明文形式存储的密码。
至于你做有什么,它是完全可以注入的;有人可以通过将“DocName”作为' OR 1 = 1; --
传递并按字面意思输入anything 作为他们的密码来轻松登录。 Parameters.Add
是你想要的,根据我最后评论中链接的 2 篇文章。另外,正如@JeroenMostert 所暗示的那样,您的密码似乎是纯文本,这也是一个大问题; 始终将密码存储为盐和哈希组合。
这能回答你的问题吗? How to give ADO.NET Parameters
互联网上有成千上万篇关于如何做到这一点的文章。自 2005 年的 .Net 1.1 以来一直有人问这个问题
【参考方案1】:
我想这就是你要找的东西
command.Parameters.Add("@DocName", SqlDbType.NVarChar, 50);
你必须添加
command.Parameters["@DocName"].Value = DocName.text;
这样,您将 docname.text 的值传递给参数,同时仍确保它之前接受正确的数据类型。
【讨论】:
【参考方案2】:您可以尝试以下代码来检查您的用户名或密码是否正确。
private void button1_Click(object sender, EventArgs e)
string connstr = @"connstr";
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
string sql = "select * from Doctor WHERE DocName = @DocName AND DocPass = @DocPass";
SqlCommand command = new SqlCommand(sql,connection);
command.Parameters.AddWithValue("@DocName",txtName.Text);
command.Parameters.AddWithValue("@DocPass", txtPwd.Text);
SqlDataReader reader = command.ExecuteReader();
if(reader.HasRows)
Form2 form2 = new Form2();
form2.Show();
this.Hide();
private void Form1_Load(object sender, EventArgs e)
txtPwd.PasswordChar = '*';
【讨论】:
【参考方案3】:这是我为一个小项目编写的非常古老的代码片段,但是
它的工作方式是存在一个类来散列密码(加密和解密)当用户添加他/她的密码时,应用程序会加密密码字段中的任何内容并将其与数据库中的内容进行匹配如果用户名和密码与用户表上的记录匹配,它将继续登录。
try
string LoginCommand = "select * from user_login where UserName ='" + textBox1.Text + "'";
using (SqlConnection LoginCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConfigString"].ToString()))
using (SqlCommand LoginCom = LoginCon.CreateCommand())
label8.Text = "Finding User in system..";
LoginCom.CommandText = LoginCommand;
LoginCon.Open();
using (SqlDataReader Reader = LoginCom.ExecuteReader())
if (Reader.Read())
//label8.Text = priv;
label8.Text = "User Found!";
Password = Reader["EncrPassword"].ToString();
priv = Reader["Privillages"].ToString();
IsExist = true;
LoginCon.Close();
catch (Exception ex)
MessageBox.Show(ex.ToString());
if (IsExist) //if record exis in db , it will return true, otherwise it will return false
if (Cryptography.Decrypt(Password).Equals(textBox2.Text))
label8.Text = "Making sure Everything is Ready!";
User = textBox1.Text;
// MessageBox.Show("Login Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
GetAcces();
this.Hide();
MainWindow frm1 = new MainWindow();
frm1.ShowDialog();
//We Show pop up here.
try
// show
catch (Exception)
throw;
else
MessageBox.Show("Password is wrong!...", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
label8.Text = "Your Password or UserName is incorrect - Please Try again!";
else //showing the error message if user credential is wrong
MessageBox.Show("Please enter the valid credentials", "error", MessageBoxButtons.OK, MessageBoxIcon.Information);
catch (Exception ex)
MessageBox.Show(ex.Message.ToString(), "Cannot Locate Server");
【讨论】:
它非常有用,但我只需要一些简单的解决方案来结束项目,所以我想弄清楚如何通过 TextBox 而不是 command.Parameters.Add("@DocName" , SqlDbType.NVarChar, 50);我需要用文本框的 DocName.Text 替换 SqlDbType.NVarChar, 50,但它不能正常工作 command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = customerID;以上是关于如何使用 SQL 参数在 C# 中编辑登录表单?的主要内容,如果未能解决你的问题,请参考以下文章