我有一个 CheckBoxes 表,它们作为“True”和“False”插入到 SQL 数据库中。但是,我想通过加载事件再次检索这些值,但我无法获取它们。这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (auditChecklist != null)
{
//for loading audit checklist
getAuditChecklist();
}
}
我尝试使用以下函数检索它们,我认为它可能会这样做:
private void getAuditChecklist()
{
//int i = 0;
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["PSCV1ConnectionString"].ConnectionString;
string queryString = @"SELECT * FROM AUDIT_CHECKLIST " +
"WHERE SITE_ID = @SiteID";
using (SqlConnection connection =
new SqlConnection(conn))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
cmd = new SqlCommand(queryString);
cmd.Connection = connection;
cmd.Parameters.Add(new SqlParameter("@SiteID", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Site_ID")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@SiteID"].Value = foo.Site_ID;
SqlDataReader reader = cmd.ExecuteReader();
if (CheckBox1.Checked == true)
{
while (reader.Read())
{
cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Mount")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@Mount"].Value = "True";
}
}
else
{
cmd.Parameters.Add(new SqlParameter("@Mount", //the name of the parameter to map
System.Data.SqlDbType.NVarChar, //SqlDbType value
20, //The width of the parameter
"Mount")); //The name of the column source
//Fill the parameter with the value retrieved
//from the text field
cmd.Parameters["@Mount"].Value = "False";
}
reader.Close();
}
}
非常感谢您的帮助!