如何在WinForm应用程序的文本框中显示特定的数据库条目

本文关键字:显示 数据库 WinForm 应用程序 文本 | 更新日期: 2023-09-27 18:08:44

更新:感谢大家,代码不是问题所在,尽管有关SQL注入的信息很有用,但我的问题是我使用的是数据库的旧版本,该版本没有相应的产品ID,所以它使用的是它能找到的第一个产品。现在感觉很愚蠢,但谢谢你的建议。

我目前有以下代码:

SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)'v11.0 AttachDbFilename=C:'Users'h8005267'Desktop'Practical Project'Build'System4'System'StockControl.mdf;Integrated Security=True;Connect Timeout=30");
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE ProductID='" + textBox3.Text + "'", connection); 
SqlDataReader re = cmd.ExecuteReader();
if (re.Read())
{
  textBox4.Text = re["ProductTitle"].ToString(); // only fills using first product in table
  textBox5.Text = re["ProductPublisherArtist"].ToString();
  comboBox1.Text = re["ProductType"].ToString();
  textBox6.Text = re["Price"].ToString();
}
else
{
  MessageBox.Show("Please enter a valid item barcode");
}
re.Close();
connection.Close();

我目前遇到的问题是,尽管文本框在单击按钮时显示信息,但显示的信息只是数据库中的第一行数据,而不是与sql语句中的文本框3相对应的行

如何在WinForm应用程序的文本框中显示特定的数据库条目

试试这个。避免以动态方式构建SQL语句。打开数据库时会面临SQL注入的风险。已使用参数insead。

using (var connection = new SqlConnection("connection string"))
{
    connection.Open();
    using (var cmd = new SqlCommand("SELECT * FROM Product WHERE ProductID=@MYVALUE", connection))
    {
        cmd.Parameters.Add("@MYVALUE", SqlDbType.VarChar).Value = textBox3.Text;
        SqlDataReader re = cmd.ExecuteReader();
        if (re.Read())
        {
            textBox4.Text = re["ProductTitle"].ToString(); // only fills using first product in table
            textBox5.Text = re["ProductPublisherArtist"].ToString();
            comboBox1.Text = re["ProductType"].ToString();
            textBox6.Text = re["Price"].ToString();
        }
        else
        {
            MessageBox.Show("Please enter a valid item barcode");
        }
    }
}

在线上设置断点

SqlDataReader re = cmd.ExecuteReader();

并在文本框3 中输入以下内容

'; DROP TABLE Product; SELECT '

将在您的文本框中输入。现在执行您的方法并仔细阅读生成的sql命令。。。欢迎使用sql注入;(

@M Patel:谢谢你的评论,你是完全正确的

结果将是以下SQL

SELECT * FROM Product WHERE ProductID=''; DROP TABLE Product; SELECT ''

这将允许恶意用户破坏您的数据库。

为了防止这种情况发生,您应该使用M Patel在其回答

中建议的准备好的语句

'" + textBox3.Text + "'" 存在SQL注入问题

你不必这样命名你的控件,你必须使用一个有意义的名称

你可以使用这个代码

using (SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)'v11.0 AttachDbFilename=C:'Users'h8005267'Desktop'Practical Project'Build'System4'System'StockControl.mdf;Integrated Security=True;Connect Timeout=30"))
{
    connection.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE ProductID=@ProductID", connection);
    cmd.Parameters.AddWithValue("@ProductID", textBox3.Text);
    SqlDataReader re = cmd.ExecuteReader();
    if (re.Read())
    {
        textBox4.Text = re.GetString(re.GetOrdinal("ProductTitle")); // only fills using first product in table
        textBox5.Text = re.GetString(re.GetOrdinal("ProductPublisherArtist"));
        comboBox1.Text = re.GetString(re.GetOrdinal("ProductType"));
        textBox6.Text = re.GetString(re.GetOrdinal("Price"));
    }
    else
    {
        MessageBox.Show("Please enter a valid item barcode");
    }
    re.Close();
}