且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从选择查询的存储过程返回结果到列表

更新时间:2023-02-05 23:33:05

在存储过程,你只需要编写类似下面的选择查询:

In Stored procedure, you just need to write the select query like the below:

CREATE PROCEDURE TestProcedure
AS
BEGIN
    SELECT ID, Name From Test
END

在C#的一面,你可以访问使用阅读器,数据表,适配器。

On C# side, you can access using Reader, datatable, adapter.

使用适配器只是苏珊娜Floora解释说。

Using adapter has just explained by Susanna Floora.

使用阅读器:

SqlConnection connection = new SqlConnection(ConnectionString);

command = new SqlCommand("TestProcedure", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
reader = command.ExecuteReader();

List<Test> TestList = new List<Test>();
Test test;

while (reader.Read())
{
    test = new Test();
    test.ID = int.Parse(reader["ID"].ToString());
    test.Name = reader["Name"].ToString();
    TestList.Add(test);
}

gvGrid.DataSource = TestList;
gvGrid.DataBind();

使用dataTable的:

Using dataTable:

SqlConnection connection = new SqlConnection(ConnectionString);

command = new SqlCommand("TestProcedure", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();

DataTable dt = new DataTable();

dt.Load(command.ExecuteReader());
gvGrid.DataSource = dt;
gvGrid.DataBind();

我希望这会帮助你。 :)

I hope it will help you. :)