1、先建立一个类为:db.cs
public static SqlConnection createConnection()
{
System.Data.SqlClient.SqlConnection con = new SqlConnection("server=localhost;database=.;uid=sa;pwd=sa;");
return con;
}
2、控件里的代码为:
SqlConnection con = DB2.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from personlike", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.CheckBoxList1.DataTextField = "likeContent";
this.CheckBoxList1.DataValueField = "id";
this.CheckBoxList1.DataSource = sdr;
this.CheckBoxList1.DataBind();
sdr.Close();
con.Close();
public static SqlConnection createConnection()
{
System.Data.SqlClient.SqlConnection con = new SqlConnection("server=localhost;database=.;uid=sa;pwd=sa;");
return con;
}
2、控件里的代码为:
SqlConnection con = DB2.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from personlike", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.CheckBoxList1.DataTextField = "likeContent";
this.CheckBoxList1.DataValueField = "id";
this.CheckBoxList1.DataSource = sdr;
this.CheckBoxList1.DataBind();
sdr.Close();
con.Close();
转载于:https://blog.51cto.com/o8988/229207