美好的一天。
我正在使用数据库应用程序(Windows窗体,C#)。
(我希望)我的应用程序有一个选项可以按不同的参数进行搜索。
第一个搜索参数/条件由单击按钮确定。 单击按钮(例如“ button1”)时,将为“ label4”分配一个值(不同按钮的值;有6个按钮)。此值等于我的数据库表中的列名。
此外,在单击button1之后,将用数据填充组合框(“ comboBox1”)。 [用于定义查询参数(从comboBox选择中),我使用了: Stackoverflow_1和Stackoverflow_2。]
在单击“ button9”之后,结果应显示在“数据网格视图”中。 我是否对Data Grid View的“数据适配器”和“数据集”出错? 如果我对列值进行“硬编码”,那么它将起作用,因此我的怀疑对象是“列变量”。
帮助。谢谢。
TLDR:如何在SQL字符串搜索中设置变量?
public void button9_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
string column = this.label4.Text; //column is the variable/value which I get when selecting "sort by" button
string XXX = this.comboBox1.GetItemText(this.comboBox1.SelectedItem); //XXX is the variable/value which I get when comboBox item is selected
SqlCommand cmd9 = new SqlCommand("SELECT * FROM tblTable WHERE @column=@XXX", con);
cmd9.Parameters.Add("@column", SqlDbType.NVarChar, -1);
cmd9.Parameters["@column"].Value = column;
cmd9.Parameters.Add("@XXX", SqlDbType.NVarChar, -1);
cmd9.Parameters["@XXX"].Value = XXX;
SqlDataAdapter adapter9 = new SqlDataAdapter(cmd9); //to view data in DataGridview I need a "data adapter" and "data set"
DataSet ds9 = new DataSet();
adapter9.Fill(ds9, "tblTable");
dataGridView1.DataSource = ds9.Tables["tblTable"];
con.Close();
}
}
答案 0 :(得分:0)
@column is not a parameter
....
如果您要进行动态查询,则应使用带有隐藏查询字符串的if-else
语句。(列表StringBuilder
)
您需要以下内容:
con.Open();
string column = this.label4.Text;
string XXX = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
StringBuilder sb = new StringBuilder("SELECT * FROM tblTable WHERE 1=1");
if(column == "...")
{
sb .Append(" column=@XXX...");
}
SqlCommand cmd9 = new SqlCommand(sb.ToSTring(), con);
cmd9.Parameters.Add("@XXX", SqlDbType.NVarChar, -1);
cmd9.Parameters["@XXX"].Value = XXX;
SqlDataAdapter adapter9 = new SqlDataAdapter(cmd9); //to view data in DataGridview I need a "data adapter" and "data set"
DataSet ds9 = new DataSet();
adapter9.Fill(ds9, "tblTable");
dataGridView1.DataSource = ds9.Tables["tblTable"];
con.Close();
也不要像这样打开和关闭连接,请使用Using
答案 1 :(得分:0)
Omriman12向我发送了正确的信息。 这是有效的方法:
public void button9_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
string column = this.label4.Text; //column is the variable/value which I get when selecting "sort by" button
string XXX = this.comboBox1.GetItemText(this.comboBox1.SelectedItem); //XXX is the variable/value which I get when comboBox item is selected
StringBuilder sb = new StringBuilder();
//1
if (column == "AAA")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
//2
if (column == "BBB")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
//3
if (column == "CCC")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
//4
if (column == "DDD")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
//5
if (column == "EEE")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
//6
if (column == "FFF")
{
sb.Append("SELECT * FROM tblTable_values WHERE ").Append(column).Append(" =@XXX");
}
SqlCommand cmd9 = new SqlCommand(sb.ToString(), con);
cmd9.Parameters.Add("@XXX", SqlDbType.NVarChar, -1);
cmd9.Parameters["@XXX"].Value = XXX;
SqlDataAdapter adapter9 = new SqlDataAdapter(cmd9); //to view data in DataGridview I need a "data adapter" and "data set"
DataSet ds9 = new DataSet();
adapter9.Fill(ds9, "tblTable_values");
dataGridView1.DataSource = ds9.Tables["tblTable_values"];
con.Close();
}
yaaaaay!