【问题标题】:C# Local SQL rows returned if statementC# 本地 SQL 行返回 if 语句
【发布时间】:2013-04-19 11:32:32
【问题描述】:
我正在尝试让我的登录系统正常工作。目前,我认为除了 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
【解决方案1】:
我已更改您的代码以使用更简单的 ExecuteScalar,它返回您的查询获得的第一行的第一列
当然,不要编写连接字符串的 sql 命令,这一点非常重要,因为这可能会以惊人的方式失败。 (如果你的文本框包含一个单引号,如果你的用户写 malicious text like this 会怎么样
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;
}
}
还要注意 using 语句,在您之前的代码中,如果未找到登录但您忘记关闭连接,您将退出该过程。在您的应用程序的生命周期中,这可能会成为一个大问题。 Using 语句可以防止这种情况发生
现在我应该开始谈谈以明文形式存储和传输密码的弱点,但那是另一回事
【解决方案2】:
ExecuteNonQuery 方法将返回受影响的行数。
int rowsAffected = command.ExecuteNonQuery();
bool userExists = rowsAffected > 0;
if (userExists) // The user exists
{
}
注意:但是,您的应用程序容易受到 SQL 注入的攻击。 IE。我可以在txt_password 文本框中输入;DROP TABLE Technician。
您应该改用参数化查询或其他更安全的身份验证方法(例如 ASP.NET 成员身份)。
要使用参数化查询,您可以将 CommandText 更改为:
SqlCeCommand command = new SqlCeCommand("SELECT * FROM Technician WHERE Name=@username AND password=@password";
然后在via中添加参数:
command.Parameters.AddWithValue("@username", txt_username.Text);
command.Parameters.AddWithValue("@password", txt_password.Text);
http://johnhforrest.com/2010/10/parameterized-sql-queries-in-c/
【解决方案3】:
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();
}
【解决方案4】:
a=1;
b=1;
if a=b
{
a=c;
}
else
{
a=b;
}
else if
{
MessageBox.Show("Login Unsuccessful");
return i;