【问题标题】:how to pass the values from txtbox form to sql table如何将txtbox表单中的值传递到sql表
【发布时间】:2015-02-07 07:20:40
【问题描述】:
using (SqlConnection conn = new SqlConnection())
{
    conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\hp\documents\visual studio 2010\Projects\FinalProject\FinalProject\InfoEmp.mdf;Integrated Security=True;User Instance=True";
    conn.Open();

    SqlCommand comm = new SqlCommand("Select * from UserLog where Username = '" + txtUname.Text + "' and Password = '" + txtPword.Text + "' ;", conn);

    SqlDataReader sdr;
    sdr = comm.ExecuteReader();

    while (sdr.Read())
    {
        if (comm.Equals(txtUname.Text) &&
            comm.Equals(txtPword.Text))
        {
            using (frmMain frmmain = new frmMain())
            {
                this.Hide();
                frmmain.ShowDialog();
            }
        }
        else if (comm.Equals(txtUname.Text) &&
                 comm.Equals(txtPword.Text))
        {
            using (frmMain2 frmmain2 = new frmMain2())
            {
                this.Hide();
                frmmain2.ShowDialog();
            }
        }
        else if (txtPword.Text == "" && txtUname.Text == "")
        {
            MessageBox.Show("Please Fill in the blanks..");
        }
        else
        {
            MessageBox.Show("Please make sure that you have access of being admin");
        }
    }

    conn.Close();

这是我的代码.. 为什么在 if else 条件下.. 他们只阅读 else .. 请回答谢谢

【问题讨论】:

  • 您可以编辑您的帖子...格式显示不正确
  • 这是什么鬼?
  • SQL Injection alert - 你应该将你的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入

标签: c# database-connection


【解决方案1】:

存在语义错误,您需要使用 OR 运算符 ||对于您的第二个“else if”,第一个“else if”的情况类似。请查看更多与您的用例相关的方法的 SqlCommand 文档。 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand_methods(v=vs.110).aspx

SqlCommand.Equals() 

在这种情况下不符合您的目的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多