从SQL列中检索多值到复选框 [英] Retrieve multi value to checkbox from SQL column
问题描述
i会试着告诉你我想要完成什么我觉得它会更清楚,
在sql我有一些包含一些值的列by - liek(abc)
并且在我的表格中我有3个复选框,文字= a和b和c
所以我想检查这个值是否存在复选框在这里检查是我的代码
SqlCommand cmd = new SqlCommand( 从tblvl中选择st,其中ID = + textBox1.Text + 跨度>,CN);
cn.Open();
SqlDataReader sqld = cmd.ExecuteReader();
while (sqld.Read())
{
string st =( string )sqld [ st]; // 此检索值
string [] aray = st.Split(' - ') ; // 这里是单独的
foreach ( string sta in aray) // 在这里它们之间循环
{
if (sta == a.Text)
{
a.BackColor = Color.Red;
a.Checked = true ;
}
else {a.Checked = false ; }
if (sta == b.Text)
{
baChecked = 真跨度>;
}
其他 {baChecked = false }
}
}
cn.Close();
此代码有效,但第二次或第三次循环的问题不等于它取消选中它。如果有任何其他方法我可以做到这一点请指导我
任何帮助都会如此赞赏
什么我试过了:
i尝试使用switch cas但没有工作
< blockquote class =quote>Quote:此代码工作
不,它甚至不编译。
baChecked = true ;
应
b.Checked = true ;
Richard Deeming指出您的响应
引用:感谢man那么真,但这只是为了尝试..
尝试立即养成使用参数化查询的习惯。您不必返回并重新访问您的代码,您更有可能避免可能蔓延的琐碎错误。
SqlCommand cmd = new SqlCommand( 从tblvl中选择st,其中ID = @ id,cn);
cmd.Parameters.AddWithValue( @ id,textBox1.Text);
引用:但第二次或第三次循环的问题不相等然后取消选中它
再次查看你的代码,参见
if (sta == a.Text)
{
a.BackColor = Color.Red;
a.Checked = true ;
}
else {a.Checked = false ; }
第一次 sta
与a.Text和Checkbox a相同。但第二次 sta
与b.Text相同,所以你立即取消选中Checkbox a!摆脱 else
子句并移动a.Checked = false;循环外。
a.Checked = false ;
b.Checked = false ;
c.Checked = false ;
foreach ( string sta 在 aray) // 在这里它们之间循环
{
if (sta == a.Text)
{
a.BackColor = Color.Red;
a.Checked = true ;
}
if (sta == b.Text)
b.Checked = 真跨度>;
if (sta == c.Text)
c.Checked = true ;
}
如果你已经正确调试了,你可能会发现这一点 - 请参阅这篇文章以便快速调试掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
现在解决其他一些问题...在数据库的单个列中存储多个值绝不是一个好主意 - 正如您所发现的那样,尝试以合理的方式恢复值是一种痛苦。
为什么不只有3位列 sta
, stb
, stc
其中如果值= 0,则应取消选中相应的复选框,如果值为1则应选中已检查。例如。
使用( var cn = new SqlConnection(constr))
{
var cmd = new SqlCommand( 从tblvl中选择sta,stb,stc,其中ID = @ id, CN);
cmd.Parameters.AddWithValue( @ id,textBox1.Text);
cn.Open();
var sqld = cmd.ExecuteReader();
if (sqld.Read())
{
a.Checked =( int )sqld [ sta] == 1 跨度>;
b.Checked =( int )sqld [ stb] == 1 ;
c.Checked =( int )sqld [ stc] == 1 ;
if (a.Checked)a.BackColor = Color.Red;
}
cn.Close();
}
hello dears,
i will try to tell you what im trying to accomplish i think it will be more clear,
in sql i have column that contain some value separated by - liek(a-b-c)
and in my form i have 3 checkbox that text =a and b and c
so i wanna check if this value exist checkbox get checked here is my code
SqlCommand cmd = new SqlCommand("select st from tblvl where ID="+textBox1.Text+"",cn);
cn.Open();
SqlDataReader sqld = cmd.ExecuteReader();
while (sqld.Read())
{
string st = (string)sqld["st"];//this retrieve value
string[] aray = st.Split('-');//here is separate it
foreach (string sta in aray)//in here it loop between them
{
if (sta == a.Text)
{
a.BackColor = Color.Red;
a.Checked = true;
}
else { a.Checked = false; }
if(sta ==b.Text)
{
b.a.Checked = true;
}
else { b.a.Checked = false }
}
}
cn.Close();
this codes work but the problem the loop second time or third is not equal then it unchecked it .and if there any way else i can do it plz guide me
any help will be so appreciated
What I have tried:
i have tried with switch cas but not worked
Quote:this codes work
No, it doesn't even compile.
b.a.Checked = true;
should be
b.Checked = true;
Richard Deeming pointed out the vulnerability to SQL Injection to which your respondedQuote:thanks man thats so true,but this was just for trying..
Try to get into the habit of using parameterised queries immediately. You won't have to go back and revisit your code and you are more likely to avoid the trivial errors that can creep in.
SqlCommand cmd = new SqlCommand("select st from tblvl where ID=@id", cn); cmd.Parameters.AddWithValue("@id", textBox1.Text);
Quote:but the problem the loop second time or third is not equal then it unchecked it
Have a look at your code again, see
if (sta == a.Text) { a.BackColor = Color.Red; a.Checked = true; } else { a.Checked = false; }The first time around
sta
is the same as a.Text and Checkbox a is checked. But 2nd time aroundsta
is the same as b.Text so you immediately uncheck Checkbox a! Get rid of theelse
clauses and move the a.Checked = false; to outside the loop.a.Checked = false; b.Checked = false; c.Checked = false; foreach (string sta in aray) //in here it loop between them { if (sta == a.Text) { a.BackColor = Color.Red; a.Checked = true; } if(sta == b.Text) b.Checked = true; if (sta == c.Text) c.Checked = true; }You would have probably spotted this if you had debugged properly - see this article to get up to speed with debugging Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Now to address some of the other issues...Storing multiple values in a single column on the database is never a good idea - as you have discovered, it is a pain trying to get the values back out in a sensible way.
Why not just have 3 bit columnssta
,stb
,stc
where if the value = 0 the corresponding Checkbox should be unchecked and if the value is 1 then it should be Checked. Eg.using (var cn = new SqlConnection(constr)) { var cmd = new SqlCommand("select sta, stb, stc from tblvl where ID=@id", cn); cmd.Parameters.AddWithValue("@id", textBox1.Text); cn.Open(); var sqld = cmd.ExecuteReader(); if (sqld.Read()) { a.Checked = (int)sqld["sta"] == 1; b.Checked = (int)sqld["stb"] == 1; c.Checked = (int)sqld["stc"] == 1; if (a.Checked) a.BackColor = Color.Red; } cn.Close(); }
这篇关于从SQL列中检索多值到复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!