「1」と「0」として SQL データベースに挿入される CheckBoxes のテーブルがあります。しかし、load イベントでそれらの値を再度取得したいのですが、取得できません。これは私のコードです:
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 データベースからチェックマークを取得するには何が欠けていますか? 以下は、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();
}
}
}