2016-01-09 29 views
-2

我需要幫助來顯示Windows窗體中的SQL Server值。在下面的應用程序中,如果查詢返回一行,則顯示值。我知道我要使用sqldatareader,但到目前爲止,我一直未能成功添加它。C#Windows窗體應用程序 - 在文本框中顯示sql服務器值

SqlConnection ChuoDB_Connection = new SqlConnection("Data Source=test-PC\\tester;Initial Catalog=Chuo;Integrated Security=True;Pooling=False"); 

    SqlDataAdapter select_adapt; 

private void btn_guardian_student_search_Click(object sender, EventArgs e) 
    { 
     if (rd_btn_guardian_student_no.Checked == true) 

     { 
      DataSet ds = new DataSet(); 
      SqlDataReader dr; 
      ChuoDB_Connection.Open(); 
select_adapt = new SqlDataAdapter("SELECT * FROM Guardian WHERE STUDENT_NO = @student_no", ChuoDB_Connection); 

select_adapt.SelectCommand.Parameters.Add("@student_no", SqlDbType.Int).Value = Convert.ToInt32(txt_bx_guardian_student_search.Text); 

      select_adapt.Fill(ds); 

      if (ds.Tables[0].Rows.Count == 0) 
      { 
       lbl_guardian_student_search.Text = "No Guardian record exists for this student. Please enter the Guardian Information"; 
       ChuoDB_Connection.Close(); 

      } 

      if (ds.Tables[0].Rows.Count > 0) 
      { 
       lbl_guardian_student_search.Text = "";  

       while (dr.read()) 
       { 
        txtBox1.Text = rdr.Item["DBFieldName1"].ToString(); 
        txtBox2.Text = rdr.Item["DBFieldName2"].ToString(); 
       } 



      }     


     } 
    } 
+1

此代碼不能工作。您不要初始化DataReader。你在問什麼?如何使用DataReader而不是DataAdapter? – Steve

回答

0

要使用SqlDataReader,您需要使用SqlCommand的ExecuteReader方法來初始化它。因此,在你的代碼,你可以使它是一個使用塊內初始化以及命令局部變量的相對部分全部丟棄到SqlDataAdapter的

private void btn_guardian_student_search_Click(object sender, EventArgs e) 
{ 
    if (rd_btn_guardian_student_no.Checked == true) 
    { 
     using(SqlConnection cnn = new SqlConnection(......)) 
     using(SqlCommand cmd = new SqlCommand(@"SELECT * FROM Guardian 
            WHERE STUDENT_NO = @student_no", cnn)) 
     { 
      cnn.Open(); 
      cnn.Add("@student_no", SqlDbType.Int).Value = Convert.ToInt32(txt_bx_guardian_student_search.Text); 
      using(SqlDataReader rd = new cmd.ExecuteReader()) 
      { 
       if(!rd.HasRows) 
        lbl_guardian_student_search.Text = "No Guardian record exists for this student. Please enter the Guardian Information"; 
       else 
       { 
        rdr.Read(); 
        txtBox1.Text = rdr.Item["DBFieldName1"].ToString(); 
        txtBox2.Text = rdr.Item["DBFieldName2"].ToString(); 
       } 
      }     
     } 
    } 
} 

請注意,我已經搬到裏面的代碼全局連接對象讀者。它們是一次性物品,應在完成後丟棄。使用聲明確保正確處理這些對象

0

我認爲你不需要DataAdapter和DataSet。 儘量只用DataReader的:

string _connectionString = "Data Source=test-PC\\tester;Initial Catalog=Chuo;Integrated Security=True;Pooling=False"; 

string _selectCommand = @"SELECT * FROM Guardian WHERE STUDENT_NO = @student_no"; 

這裏代碼單擊處理程序:

SqlParameter parameter = new SqlParameter("@student_no", SqlDbType.Int); 
parameter.Value = Convert.ToInt32(txt_bx_guardian_student_search.Text); 

    using (IDbConnection connection = new SqlConnection(_connectionString)) 
      { 
       connection.Open(); 

       using (IDbCommand command = connection.CreateCommand()) 
       { 
        command.Connection = connection; 
        command.CommandText = _selectCommand; 
        command.Parameters.Add("@student_no", SqlDbType.Int).Value = Convert.ToInt32(txt_bx_guardian_student_search.Text); 

        IDataReader reader = command.ExecuteReader(); 


        if (reader.Read()) 
        { 
         txtBox1.Text = reader["DBFieldName1"].ToString(); 
         txtBox2.Text = reader["DBFieldName2"].ToString(); 
        } 
        else 
        { 
         lbl_guardian_student_search.Text = "No Guardian record exists for this student. Please enter the Guardian Information"; 
        } 
       } 
      } 
相關問題