0

我有一个 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();
    }
}

非常感谢您的帮助!

4

3 回答 3

1

如果我正确地回答了您的问题...问题出在您的数据类型中。我认为它有点领域。

sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
sqlCommmand.Parameters["@Mount"].Value = 1;
于 2013-04-23T05:34:28.397 回答
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参数。我不确定你的意图是什么,但我想这就是你的问题所在。

我建议您从这里开始,开始从数据库中读取数据。

于 2013-04-23T01:10:39.130 回答
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();
 }        
}
于 2013-04-30T14:48:53.833 回答