
用checkboxlist选择后存如数据库
2个回答
展开全部
protected void btnd_Click(object sender, EventArgs e)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{ //存数据库
string courename = this.CheckBoxList1.Items[i].Value.ToString();
string sqlstr = "Data Source=.;Initial Catalog=JobWanted Apartment;Integrated Security=true";
string sql = "insert into test (dfdf)values('" + courename + "')";
SqlConnection con = new SqlConnection(sqlstr);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
//读数据库
string sql2 = "select * from test";
SqlCommand cmd1 = new SqlCommand(sql2, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(ds);
dt = ds.Tables[0];
this.TextBox1.Text = dt.Rows[0][0].ToString();
this.TextBox2.Text = dt.Rows[0][1].ToString();
con.Close();
}
else
{
Response.Write("<script>alert('您还未选课!')</script>");
}
}
}
这是大致思想,一些细节你自己来弄了。。。数据库名 表名 列名改下
你的做法 展示已选课的课程的时候应该用数据控件的,直接把dataset绑定到gridview上就OK了。。。
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{ //存数据库
string courename = this.CheckBoxList1.Items[i].Value.ToString();
string sqlstr = "Data Source=.;Initial Catalog=JobWanted Apartment;Integrated Security=true";
string sql = "insert into test (dfdf)values('" + courename + "')";
SqlConnection con = new SqlConnection(sqlstr);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
//读数据库
string sql2 = "select * from test";
SqlCommand cmd1 = new SqlCommand(sql2, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
adapter.Fill(ds);
dt = ds.Tables[0];
this.TextBox1.Text = dt.Rows[0][0].ToString();
this.TextBox2.Text = dt.Rows[0][1].ToString();
con.Close();
}
else
{
Response.Write("<script>alert('您还未选课!')</script>");
}
}
}
这是大致思想,一些细节你自己来弄了。。。数据库名 表名 列名改下
你的做法 展示已选课的课程的时候应该用数据控件的,直接把dataset绑定到gridview上就OK了。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询