首页 > 解决方案 > 如何使用 SQL 参数在 C# 中编辑登录表单?

问题描述

所以我有登录表单,我需要在其中插入 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();
    }

标签: c#sql-servervisual-studio

解决方案


这是我为一个小项目编写的非常古老的代码片段,但是

它的工作方式是存在一个类来散列密码(加密和解密)当用户添加他/她的密码时,应用程序加密密码字段中的任何内容并将其与数据库中的内容匹配如果用户名和密码匹配用户表上的记录,它将继续登录。

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");
            }

推荐阅读