0

我有一个 CheckBoxes 表,它们作为“1”和“0”插入到 SQL 数据库中。但是,我想通过加载事件再次检索这些值,但我无法获取它们。这是我的代码:

    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();
    }        
}

从 sql db 获取复选标记我缺少什么?下面是插入sql的方法:

    private void SaveAuditChecklist()
{
    if (auditChecklist != null)
    {
        SqlCommand cmd = null;
        string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        string queryString = @"INSERT INTO AUDITOR_CHECKLIST VALUES(" +
            "@SiteID, @Mount, @Braker, @Access, @ConnNet, @LogBook, @Pictures, @Floor, @CbLenght, @Channel) ";

        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;
            cmd.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit));
            cmd.Parameters["@Mount"].Value = CheckBox1.Checked;
            cmd.Parameters.Add(new SqlParameter("@Braker", SqlDbType.Bit));
            cmd.Parameters["@Braker"].Value = CheckBox2.Checked;
            cmd.Parameters.Add(new SqlParameter("@Access", SqlDbType.Bit));
            cmd.Parameters["@Access"].Value = CheckBox3.Checked;
            cmd.Parameters.Add(new SqlParameter("@ConnNet", SqlDbType.Bit));
            cmd.Parameters["@ConnNet"].Value = CheckBox4.Checked;
            cmd.Parameters.Add(new SqlParameter("@LogBook", SqlDbType.Bit));
            cmd.Parameters["@LogBook"].Value = CheckBox5.Checked;
            cmd.Parameters.Add(new SqlParameter("@Pictures", SqlDbType.Bit));
            cmd.Parameters["@Pictures"].Value = CheckBox6.Checked;
            cmd.Parameters.Add(new SqlParameter("@Floor", SqlDbType.Bit));
            cmd.Parameters["@Floor"].Value = CheckBox8.Checked;
            cmd.Parameters.Add(new SqlParameter("@CbLenght", SqlDbType.Bit));
            cmd.Parameters["@CbLenght"].Value = CheckBox9.Checked;
            cmd.Parameters.Add(new SqlParameter("@Channel", SqlDbType.Bit));
            cmd.Parameters["@Channel"].Value = CheckBox10.Checked;
            cmd.ExecuteReader();
        }
    }
}
4

2 回答 2

1

布尔值在 Sql 数据库中存储为 1 或 0,但数据读取器会为您进行转换。而是使用:

var myBool = reader.GetBoolean(i);

然后只需将值分配给控件的 value 属性。

于 2013-04-25T16:46:55.557 回答
1

最终让读者了解了获取复选标记值的方式。我用工作代码编辑了我的问题;但是,以下是我为读者添加或更改的内容:

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")));
} 
于 2013-04-26T15:08:11.250 回答