2013-04-22 3 views
0

SQL 데이터베이스에 'True'및 'False'로 삽입되는 CheckBoxes 테이블이 있습니다. 그러나로드 이벤트를 사용하여 해당 값을 다시 검색하려고하지만이를 가져올 수 없습니다. 이건 내 코드입니다 :asp를 검색하는 방법 : SQL db에서 CheckBox 값을?

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

당신이 값을 검색하고 데시벨 값으로 컨트롤을 설정하거나 컨트롤을 저장하려고를 값을 데이터베이스에 저장 하시겠습니까? getAuditChecklist 메소드는 다른 f}으로 데이터베이스에서 상태 만 검색합니다. – angusf

답변

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

값이 은 '참'으로, '거짓'은이지만, nvarchar 필드에는 1과 0이 전달됩니다. 데이터베이스 필드가 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을 시작하는 것이 좋습니다.

+0

데이터베이스 필드는 Nvarchar이며 확인란에서 체크 표시를 검색하려고하므로 사용자는 마지막 세션에서 이전에 검사 한 내용을 알고 있습니다. 도와 주셔서 감사합니다. – Jacman

+0

CheckBox의 동등한 SQL 데이터 유형은'bit' 필드입니다. 따라서 CheckBox가 표시된 사실을 기록하려면 데이터베이스의 해당 항목에 대해'nvarchar '를 사용하지 않아야합니다. 한 사용자 필드가 의도하지 않게 'True'에서 'Trye'로 변경되면 어떻게 될까요? 그런 다음 정확하게 쿼리 할 수 ​​없으므로 데이터 조각을 잃게됩니다. 'bit' 필드는 그 가능성을 제거합니다. – McCee

+0

그 확인란의 값으로 "참"을 사용했지만 어떤 이유로 체크 박스가 체크 표시되지 않습니다. 확인 된 값이 표시되지 않습니다. – Jacman

1

질문이 일 경우 ... 문제가 데이터 유형에 있습니다. 나는 그 비트 필드를 생각한다.

sqlCommand.Parameters.Add(new SqlParameter("@Mount", SqlDbType.Bit)); 
sqlCommmand.Parameters["@Mount"].Value = 1; 
+0

감사합니다,이 방법은 DB에서 체크 표시를 보여줄까요? 즉, 값을 검색하여 확인란에 체크 표시로 넣을까요? – Jacman

+0

예 당신도 할 수 있습니다 : _checkboxName_.checked = dr [ "Mount"]; –

+0

예 당신은 또한 그 작업을 수행 할 수 있습니다 는 ... 즉 'CheckBoxname.checked = TRUE ' 선택 쿼리 당신의 체크 박스에 참 또는 거짓을 제공하고()가 ExecuteReader 'SqlDataReader 개체의 박사 = commandobjectname.ExecuteReader으로 실행; While (dr.Read()) { _checkboxName_.checked = dr [ "Mount"]; } ' Mount는 열 이름입니다. –

관련 문제