SqlCommand INSERT INTO仅按复选框选择行 [英] SqlCommand INSERT INTO only selected rows by checkbox
问题描述
大家好,我有这些插入和选择的SqlCommands。我需要添加到prikaz2(插入)条件,它将INSERT INTO只有这些行被复选框选中。我很长时间都对此感到好奇,但我的代码都没有能够做到这一点。你们有人会建议我吗?在我看来,我必须添加WHERE?
Hello guys I have got these SqlCommands which inserts and selects. I need to add to prikaz2 (which inserts) condition it would INSERT INTO only these rows wich were selected by checkbox. I have wondered about this for quiet long time but none of my codes were able to do that. Do you guys would suggest me something? In my opinion I have to add "WHERE" ?
SqlCommand comm = new SqlCommand("Select MAX (ID_K) FROM klient", spojeni);
spojeni.Open();
int max = (int)comm.ExecuteScalar();
spojeni.Close();
for (int i = 0; i < dtg_ksluzby.Rows.Count; i++)
{
SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni); // here I need to add condition to INSERT INTO only selected rows
prikaz2.Parameters.AddWithValue("@val1", dtg_ksluzby.Rows[i].Cells["text"].Value);
prikaz2.Parameters.AddWithValue("@val2", dtg_ksluzby.Rows[i].Cells["pocet"].Value);
prikaz2.Parameters.AddWithValue("@val3", dtg_ksluzby.Rows[i].Cells["akce"].Value);
prikaz2.Parameters.AddWithValue("@val4", max + 1); spojeni.Open();
prikaz2.ExecuteNonQuery();
spojeni.Close();
}
private void dtg_ksluzby_CellValueChanged(object sender,
DataGridViewCellEventArgs e)
这是我选择row.e的代码。
This is my code for selecting row.e
foreach (DataGridViewRow row in dtg_ksluzby.Rows)
{
DataGridViewCheckBoxCell chk1 = row.Cells[3] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chk1.Value) == true)
{
MessageBox.Show("Služba byla vybrána");
}
else
{
}
}
推荐答案
首先,我建议你不要在代码中使用insert / update / delete语句。您是否听说过 SQL注入 [ ^ ]?更好的方法是使用存储过程 [ ^ ]。br />
其次,你需要声明变量(例如)oCell
,输入:DataGridViewCheckBoxCell
。然后,您需要将特定单元格转换为DataGridViewCheckBoxCell以读取其已检查的属性值。
First of all, i would suggest you to NOT use insert/update/delete statements in code. Have you ever heard about SQL injection[^]? Better way is to use stored procedures[^].
Second, you need to declare variable (for example)oCell
, type:DataGridViewCheckBoxCell
. Then you need to cast specific cell to DataGridViewCheckBoxCell to read its checked property value.
DataGridViewCheckBoxCell oCell;
foreach (DataGridViewRow row in dgv1.Rows)
{
//oCell = row.Cells["name of checkboxcolumn"] as DataGridViewCheckBoxCell;
oCell = row.Cells[checkboxcolum_index] as DataGridViewCheckBoxCell;
bool bChecked = (null != oCell && null != oCell.Value && true == (bool)oCell.Value);
if (true == bChecked)
{
//call stored procedure here
}
}
< br $> b $ b
HOW TO:使用ADO调用参数化存储过程.NET和Visual C#.NET [ ^ ]
演练:仅使用存储过程(C#) [ ^ ]
如何:执行不返回值的存储过程 [ ^ 一>]
如何:执行返回行的存储过程 [< a href =http://msdn.microsoft.com/en-us/library/d7125bke.aspxtarget =_ blanktitle =新窗口> ^ ]
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET[^]
Walkthrough: Using Only Stored Procedures (C#)[^]
How to: Execute a Stored Procedure that Returns No Value[^]
How to: Execute a Stored Procedure that Returns Rows[^]
你好,
你可以尝试一下:
Hello,
U can try it:
for (int i = 0; i < dgv1.Rows.Count; i++)
{
if((bool)this.dgv1.Rows[i].Cells["checkBoxColumn"].Value == true)
{
SqlCommand prikaz2 = new SqlCommand("INSERT INTO klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni); // here I need to add condition to INSERT INTO only selected rows
prikaz2.Parameters.AddWithValue("@val1", dtg_ksluzby.Rows[i].Cells["text"].Value);
prikaz2.Parameters.AddWithValue("@val2", dtg_ksluzby.Rows[i].Cells["pocet"].Value);
prikaz2.Parameters.AddWithValue("@val3", dtg_ksluzby.Rows[i].Cells["akce"].Value);
prikaz2.Parameters.AddWithValue("@val4", max + 1); spojeni.Open();
prikaz2.ExecuteNonQuery();
spojeni.Close();
}
}
祝你好运!
Best regards!
这篇关于SqlCommand INSERT INTO仅按复选框选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!