如何从列表中获取值并将其放在where条件中 [英] How to get values from list and put it in where condition
问题描述
我有datagridview已选中复选框
但我的问题是如何更新数据库中datagridview的复选框值。
button1_click event
{
string subjId;
List< string> lines = new List< string>();
for ( int i = 0 ; i < gvSubjectsList.Rows.Count; i ++)
{
bool Ischecked = Convert.ToBoolean(gvSubjectsList.Rows [i] .Cells [ 选择]。值);
if (Ischecked == true )
{
subjId = gvSubjectsList.Rows [i] .Cells [ SubjectId]。Value.ToString();
lines.Add(subjId);
}
}
// 更新课程设置courseStatus = true其中subjId位于行
}
我的问题如何
/ /更新课程设置courseStatus = true其中subjId在行中找到
如何在c#中编写查询选择以获取列表中的数字?
更新课程设置courseStatus = true其中??????
我写的代替?????? ????
谢谢
我的尝试:
如何从列表中获取值并将其置于条件
可能最简单的方法是在循环内单独进行更新。这意味着您准备SQL语句并在循环外创建参数,并在循环内设置参数值并执行语句。
另一个选项是收集循环中的键,完成后,在SQL语句中使用IN
比较。换句话说,比如
UPDATE mytable SET 列 = value WHERE keycolumn IN ( @ key1 , @ key2 , @ key3 ...)
为了使这更容易,你可以创建参数并在循环中设置它们的值。
有关第一个选项的具体示例,请查看正确执行数据库操作/使用准备重复的陈述 [ ^ ]
I have datagridview have check boxes checked
but my problem how to update checked box values from datagridview in database .
button1_click event
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
//update courses set courseStatus = true where subjId found in lines
}
my problem how to
//update courses set courseStatus = true where subjId found in lines
How to write query select in c# to get what numbers found in list ?
update courses set courseStatus = true where ??????
what i write in place of ?????????
thanks
What I have tried:
How to get values from list and put it in where condition
Probably the easiest way would be to do updates individually, inside the loop. This would mean that you prepare the SQL statement and create the parameters outside the loop and inside the loop you set the values for parameters and execute the statement.
Another option would be to gather the keys inside the loop and when done, use anIN
comparison in the SQL statement. In other words something like
UPDATE mytable SET column = value WHERE keycolumn IN (@key1, @key2, @key3...)
To make this easier you can create the parameters and set their values inside the loop.
For a concrete example for the first option, have a look at Properly executing database operations / Use prepare for repeated statements[^]
这篇关于如何从列表中获取值并将其放在where条件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!