我的C#项目中有以下Code / SQL语句,但是select语句没有显示列中的值,它只显示列名称为什么会这样?
我的代码
string Table = ListBox2.Text;
string column= listBox1.GetItemText(listBox1.SelectedItem);
foreach (object Tables in ListBox2.SelectedItems)
{
using (SqlDataAdapter SQLStatement = new SqlDataAdapter("SELECT '"+column+"' FROM " + table, ConnectionString))
{
DataTable dt= new DataTable();
SQLStatement.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
}
答案 0 :(得分:1)
如果您使用SELECT '"+column+"' FROM table_name
,生成的查询将如下所示:SELECT "column" FROM table_name
。
请试试这个:
string sqlCommandStatement = string.Format("SELECT {0} FROM {1}", column,table);
using (SqlDataAdapter SQLStatement = new SqlDataAdapter(sqlCommandStatement, ConnectionString))
{
DataTable dt= new DataTable();
SQLStatement.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}