问题描述
嗨
我想执行插入数据并查看数据。我的表单中没有多个复选框。我现在在数据库中插入数据我需要查看插入的数据。请给我建议。
谢谢。
我尝试了什么:
private void button1_Click(object sender,EventArgs e)
{
if(validate()== 1)
{
con.Open() ;
string query =插入general_ward值(@ fno,@ rtype,@ tbed,@ incharge,@ abed,@ specialites);
cmd = new SqlCommand (query,con);
cmd.Parameters.Add(@ fno,textBox1.Text);
cmd.Parameters.Add(@ rtype,comboBox2 .Text);
cmd.Parameters.Add(@ tbed,textBox2.Text);
cmd.Parameters.Add(@ incharge,textBox3.Text );
cmd.Parameters.Add(@ abed,textBox4.Text);
string s = spl();
cmd .Parameters.Add(@ SPECIALI tes,s);
cmd.ExecuteNonQuery();
MessageBox.Show(Data Added);
cmd.Dispose( );
con.Close();
}
}
private void button2_Click(object sender,EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand(select * from general_ward where fno ='+ textBox1.Text +',con);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
string aa = dr [specialites]。ToString();
string [] a = aa.Split('/');
foreach(控制cc in this.Controls)
{
if(cc is CheckBox)
{
CheckBox spl =(CheckBox)cc;
for(int j = 0; j< a.length; j ++)>
{
if(a [j] .ToString()== spl.Text)
{
spl.Checked = true;
}
}
}
}
}
con.Close();
}
Hi
I want to perform an insert the data and view the data . I had few multiple checkboxes in my form. I inserted the data in database now i need to view the inserted datas. Pls give me suggestion.
Thank you.
What I have tried:
private void button1_Click(object sender, EventArgs e)
{
if (validate()==1)
{
con.Open();
string query = "insert into general_ward values (@fno,@rtype,@tbed,@incharge,@abed,@specialites)";
cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@fno",textBox1.Text);
cmd.Parameters.Add("@rtype", comboBox2.Text);
cmd.Parameters.Add("@tbed", textBox2.Text);
cmd.Parameters.Add("@incharge", textBox3.Text);
cmd.Parameters.Add("@abed", textBox4.Text);
string s = spl();
cmd.Parameters.Add("@specialites", s);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added");
cmd.Dispose();
con.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand ("select * from general_ward where fno='"+textBox1.Text+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
string aa = dr["specialites"].ToString();
string[] a = aa.Split('/');
foreach(Control cc in this.Controls)
{
if(cc is CheckBox)
{
CheckBox spl = (CheckBox)cc;
for(int j=0;j<a.length;j++)>
{
if (a[j].ToString()==spl.Text)
{
spl.Checked= true;
}
}
}
}
}
con.Close();
}
推荐答案
private void SetSelected(string selected)
{
CheckBox[] myCheckBoxes = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10 };
string[] selectedItems = selected.Split('/');
myCheckBoxes.ToList().ForEach(k =>
{
k.Checked = false;
if (selectedItems.Contains(k.Text))
k.Checked = true;
});
}
private string GetSelected()
{
CheckBox[] myCheckBoxes = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10 };
string selected = "";
myCheckBoxes.ToList().ForEach(k =>
{
if (k.Checked)
selected += k.Text + "/";
});
return selected.TrimEnd('/');
}
这篇关于如何以视图格式获取插入的多个复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!