How can I retrieve a table from stored procedure to a datatable?

后端 未结 3 542
盖世英雄少女心
盖世英雄少女心 2020-12-05 04:26

I created a stored procedure so as to return me a table.

Something like this:

create procedure sp_returnTable
body of procedure
select * from table
e         


        
3条回答
  •  没有蜡笔的小新
    2020-12-05 04:57

    Set the CommandText as well, and call Fill on the SqlAdapter to retrieve the results in a DataSet:

    var con = new SqlConnection();
    con.ConnectionString = "connection string";
    var com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "sp_returnTable";
    var adapt = new SqlDataAdapter();
    adapt.SelectCommand = com;
    var dataset = new DataSet();
    adapt.Fill(dataset);
    

    (Example is using parameterless constructors for clarity; can be shortened by using other constructors.)

提交回复
热议问题