我正在尝试创建一个执行简单选择的SQL命令但该表和字段作为参数接收。这些字段从listView中获取并添加到List中以使事情变得更容易。您不知道选择了多少项,因此它必须是通用的,它必须适用于任意数量的字段。这是我的连接以及该函数的外观......
private List<string> LoadListParam(ListView lv)
{
List<string> list=new List<string>();
foreach (ListViewItem item in lv.Items)
list.Add(item.ToString());
return list;
}
private SqlCommand CreateComnd(List<string> param, string table)
{
SqlConnection connection = new SqlConnection("server=localhost;" +
"Trusted_Connection=yes;" +
"database=Baza de date proiect; " +
"connection timeout=30");
SqlCommand cmd = new SqlCommand();
foreach(string i in param)
cmd.CommandText = "Select "+..;
}
有什么建议吗?
答案 0 :(得分:6)
没有foreach:
cmd.CommandText = string.Format("Select {0} from {1}",string.Join("," param), table);