如何使用SqlDataAdapter和存储过程检索行?

时间:2014-08-21 14:54:32

标签: c# sql-server stored-procedures sqldataadapter

我已经查看过很多关于此问题的问题,例如herehere。这些都能够给我正确的代码,但我没有收到任何结果。

DataTable CI = new DataTable();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    //TODO: Check functionality of stored procedure 
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "spCreateCIR";

        SqlParameter categoryParameter = new SqlParameter("@cat", SqlDbType.Int)
        {
            Value = cat,
            Direction = ParameterDirection.Input
        };

        command.Parameters.Add(categoryParameter);
        connection.Open(); 

        SqlDataAdapter da = new SqlDataAdapter(command);
        CIR = new DataTable();
        da.Fill(CIR);      
    }
}

所以我去尝试使用SqlReader的另一种方法,它正常工作并带回了我打算接收的数据,如下面的代码所示。

ObservableCollection<TYPE> CIR = new ObservableCollection<TYPE>();
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    //TODO: Check functionality of stored procedure 
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "spCreateCIR";

        SqlParameter categoryParameter = new SqlParameter("@cat", SqlDbType.Int)
        {
            Value = cat,
            Direction = ParameterDirection.Input
        };

        command.Parameters.Add(categoryParameter);

        connection.Open();
        IAsyncResult result = command.BeginExecuteReader();
        while (!result.IsCompleted)
        {
            System.Threading.Thread.Sleep(100);
        }

        SqlDataReader reader = command.EndExecuteReader(result);

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                TYPE CIL = new TYPE();
                CIL.Venue = reader.GetString(0); 
                CIR.Add(CIL);
            }
        }
    }
}

是否有任何想法或想法可以解释为什么我得到空回报?

0 个答案:

没有答案