[英]How can I search in a column of a datatable in SQL?
这是代码:
// get id groupe
cmd = new SqlCommand("select idgroupe from enseigner where idformateur = '"+Session["matricule"]+"'", cn);
dr = cmd.ExecuteReader();
dt.Load(dr);
dr.Close();
// get group name
cmd = new SqlCommand("select nom from groupe where ID IN("+dt.Columns[0]+")", cn);
dr = cmd.ExecuteReader();
// fill dropdown list
while (dr.Read())
{
DropDownList1.Items.Add(dr[0].ToString());
}
dr.Close();
cn.Close();
我不能得到所有的列,像这样我只得到列的名称,所以会出现问题。
我收到此错误:
System.Data.SqlClient.SqlException :'冒号无效:'idgroupe'
注意: idgroupe
是 DataTable 中列的名称
错误是命令
cmd = new SqlCommand("select nom from groupe where ID IN("+dt.Columns[0]+")", cn);
此文本翻译为“从 ID IN(idgroupe) 的 groupe 中选择 nom”意思是正在搜索表组上的列 nom,其中 Id 在列 idgroupe 中,这可能不存在。
我们不知道您真正想要什么,但至少应该在括号周围加上简单的引号,类似于您在第一个命令中所做的。
无论如何,您应该阅读有关 Sql 注射( https://www.troyhunt.com/stored-procedures-and-orms-wont-save/ )
并将您的命令更改为类似
using(cmd = new SqlCommand("select idgroupe from enseigner where idformateur = @id", cn))
{
cmd.Parameters.AddWithValue("@id", Session["matricule"]);
(...)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.