可能重复:
SqlDataReader 参数不起作用
我正在尝试从文本框中获取 pc 名称到 Windows 窗体应用程序中的 sql 参数。当我输入如下参数时,它工作正常。
string query = @"select stationipaddress from station where stationname = @Name";
using (SqlCommand cmd = new SqlCommand(query, cs))
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@Name";
param.Value = "demo";
cmd.Parameters.Add(param);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
label3.Text = dr.GetSqlValue(0).ToString();
results = dr.GetValue(0).ToString();
MessageBox.Show(dr.GetValue(0).ToString());
MessageBox.Show(results);
}
但是当我尝试从文本框中获取数据时它什么也没做。
string query = @"select stationipaddress from station where stationname = @Name";
using (SqlCommand cmd = new SqlCommand(query, cs))
{
SqlParameter param = new SqlParameter();
param.ParameterName = "@Name";
param.Value = textBox1.Text;
cmd.Parameters.Add(param);
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
label3.Text = dr.GetSqlValue(0).ToString();
results = dr.GetValue(0).ToString();
MessageBox.Show(dr.GetValue(0).ToString());
MessageBox.Show(results);
}