C#本地SQL行返回,如果语句

问题描述 投票:3回答:4

我正在尝试让我的登录系统工作。目前,我认为除了if语句条件(如果返回行,那么if语句为真,否则登录不成功)之外,我已经准备好了所有的工作。我不知道如何读取返回的行数,我确实尝试使用ExecuteReader方法,但不能让它工作。感谢任何帮助,谢谢。

代码。

private void btn_login_Click(object sender, EventArgs e)
{
    SqlCeConnection connection = new SqlCeConnection(@"Data Source=C:\\temp\\Project\\WindowsFormsApplication2\\Database.sdf");

    connection.Open();

    SqlCeCommand command = new SqlCeCommand("SELECT * FROM Technician WHERE Name = '" + txt_username.Text + "' AND Password = '" + txt_password.Text + "' ");
    SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(command);

    if ()
    {
        MessageBox.Show("Login Successful");
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(MainMenuForm));
        t.Start();
        this.Close();
    }
    else
    {
        MessageBox.Show("Login Unsuccessful");
        return;
    }

    connection.Close();
}
c# .net sql-server sql-server-ce sqlcommand
4个回答
3
投票

我已经修改了你的代码,使用一个更简单的ExecuteScalar来返回你的查询所获得的第一行的第一列。

当然,极为重要的是,你不要把你的sql命令写成串联字符串,因为这可能会以惊人的方式失败。(如果你的文本框中包含一个单引号,如果你的用户写的是 恶文

using(SqlCeConnection connection = new SqlCeConnection(.....)) 
{
     connection.Open();
     string sqlText = "SELECT Count(*) FROM Technician WHERE Name = @name AND Password=@pwd"
     SqlCeCommand command = new SqlCeCommand(sqlText, connection);
     command.Parameters.AddWithValue("@name", txt_username.Text);
     command.Parameters.AddWithValue("@pwd", txt_password.Text);
     int result = (int)command.ExecuteScalar();
     if (result > 0)
     {
          MessageBox.Show("Login Successful");
          System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(MainMenuForm));
          t.Start();
          this.Close();
     }
     else
     {
           MessageBox.Show("Login Unsuccessful");
           return;
     }
 }

请注意使用语句,在你之前的代码中,如果没有发现登录,你就从过程中退出,但你忘记关闭连接。这可能会成为你的应用程序生命周期中的一个大问题。使用语句可以防止这种情况的发生

现在我应该开始讨论用明文存储和传输密码的弱点,但这是另一回事。


2
投票

该方法 ExecuteNonQuery 将返回受影响的行数。

int rowsAffected = command.ExecuteNonQuery();
bool userExists = rowsAffected > 0;
if (userExists) // The user exists
{

}

注意:然而你的应用程序很容易被SQL注入。 例如,我可以输入 ;DROP TABLE Technician 进入 txt_password 文本框。

您应该使用参数化查询,或者使用其他更安全的验证方法(例如ASP.NET会员资格)。

要使用参数化查询,您可以更改 CommandText 到。

 SqlCeCommand command = new SqlCeCommand("SELECT * FROM Technician WHERE Name=@username AND password=@password";

然后添加参数在通过。

    command.Parameters.AddWithValue("@username", txt_username.Text);  
    command.Parameters.AddWithValue("@password", txt_password.Text);

http:/johnhforrest.com201010参数化的SQL查询------。


0
投票

private void btn_login_Click(object sender, EventArgs e){。

SqlConnection connection = new SqlConnection(@"Data Source=C:\\temp\\Project\\WindowsFormsApplication2\\Database.sdf");

connection.Open();

SqlCommand command = new SqlCommand("SELECT * FROM Technician WHERE Name = '" + txt_username.Text + "' AND Password = '" + txt_password.Text + "' ");
int row=command.ExecuteNonQuery();

if (row>0)
{
    MessageBox.Show("Login Successful");
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(MainMenuForm));
    t.Start();
    this.Close();
}
else
{
    MessageBox.Show("Login Unsuccessful");
    return;
}

connection.Close();

}


0
投票
    a=1;
    b=1;


 if a=b
    {
    a=c;
    } 
 else
    {
    a=b;
    }

else if
{
MessageBox.Show("Login Unsuccessful");
return i;
© www.soinside.com 2019 - 2024. All rights reserved.