我有一个CheckBoxes表,它以“True”和“False”的形式插入到SQL数据库中。但是,我想用load事件再次检索这些值,但我无法得到它们。这是我的代码:
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();
}
}
非常感谢您的帮助!
答案 0 :(得分:1)
如果我的问题正确...问题出在您的数据类型中。 我认为它有点领域。
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommmand.Parameters["@Mount"].Value = 1;
答案 1 :(得分:0)
您提到您的值插入为'True'和'False',但您将1和0传递到nvarchar字段中。数据库字段是nvarchar还是位?
如果是位字段,请将参数更改为位数据类型:
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommand.Parameters["@Mount"].Value = 1;
如果它是nvarchar字段,您应该重新考虑您的模式,因为您应该使用位数据类型。但是,为了这个问题,你可以这样做:
sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.NVarChar));
sqlCommand.Parameters["@Mount"].Value = "True";
但是,您要添加@Mount
参数,但queryString
未引用@Mount
参数。我不确定你的意图是什么,但我想这就是你的问题所在。
我建议你开始HERE开始从数据库中读取数据。
答案 2 :(得分:0)
这是我从数据库中读取/获取复选标记值的方法:
private void getAuditChecklist()
{
SqlCommand cmd = null;
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string queryString = @"SELECT Mount, Braker, Access, Conn_Net, Log_Book, Pictures, Floor, Cb_Lenght, Channel FROM AUDITOR_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();
while (reader.Read())
{
CheckBox1.Checked = (reader.GetBoolean(reader.GetOrdinal("Mount")));
CheckBox2.Checked = (reader.GetBoolean(reader.GetOrdinal("Braker")));
CheckBox3.Checked = (reader.GetBoolean(reader.GetOrdinal("Access")));
CheckBox4.Checked = (reader.GetBoolean(reader.GetOrdinal("Conn_Net")));
CheckBox5.Checked = (reader.GetBoolean(reader.GetOrdinal("Log_Book")));
CheckBox6.Checked = (reader.GetBoolean(reader.GetOrdinal("Pictures")));
CheckBox8.Checked = (reader.GetBoolean(reader.GetOrdinal("Floor")));
CheckBox9.Checked = (reader.GetBoolean(reader.GetOrdinal("Cb_lenght")));
CheckBox10.Checked = (reader.GetBoolean(reader.GetOrdinal("Channel")));
}
reader.Close();
}
}