0

「True」と「False」として SQL データベースに挿入される CheckBoxes のテーブルがあります。しかし、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();
    }
}

助けてくれてありがとう!

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 に答える