2011-04-01 56 views
2

東西停止工作在我的應用程序。我測試了我的存儲過程,它確實返回3行(太大而無法在此轉發)。之後在Visual Studio的輸出中說:我的存儲過程返回的東西,但sqlDataAdapter.Fill離開數據表爲空

(1 row(s) affected) 
(3 row(s) returned) 
@RETURN_VALUE = 0 
Finished running [dbo].[GetCaseDocumentFile]. 

爲什麼我不能把它放到名爲dtResult的數據表中?命令sqlDataAdapter.Fill(dtResult)應該這樣做。相反,我在調試模式下查看它時會{}。

string connectionString = @"Data Source=34.56.55.251;Initial Catalog=DBXXX;User ID=xx;Password=XXXXXXXX;"; 
string queryString = "GetCaseDocumentFile"; 

using (SqlConnection connection = new SqlConnection(connectionString)) 
      { 
    SqlCommand command = new SqlCommand(queryString, connection); 
    command.CommandType = System.Data.CommandType.StoredProcedure; 
    command.Parameters.AddWithValue("@key", 200012); 

    connection.Open(); 

    // Works! Three lines are returned. 
    SqlDataReader reader = command.ExecuteReader(); 
    try 
    { 
     while (reader.Read()) 
     { 
      Console.WriteLine(String.Format("{0}, {1}", 
       reader[0], reader[1])); 
     } 
    } 
    finally 
    { 
     reader.Close(); 
    } 

    // Does not work! dtResult remains empty. {} 
    DataTable dtResult = new DataTable(); 
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command); 
    sqlDataAdapter.Fill(dtResult); 

    DataTable result = dtResult; 
} 

更新:

沒關係。這實際上起作用。這是我的測試代碼和我認爲我的真實代碼所做的。但我想我忽略了真實代碼中的某些內容。

+1

SqlConnection不是唯一真正具有使用語句的東西,SqlCommand,SqlDataReader,Datatable和SqlDataAdapter都具有dispose方法,並且應該可能包含在using中(除非您手動處理它們) – Manatherin 2011-04-01 09:36:33

回答

2

你有沒有在你的程序中設置計數?
設置NoCount On;