从选择查询的存储过程返回结果到列表 [英] Return Result from Select Query in Stored Procedure to a List

查看:608
本文介绍了从选择查询的存储过程返回结果到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写,目前只包含一个SELECT查询的存储过程。这将扩大做一些其他的事情,这就是为什么它是一个存储过程,但现在它是一个简单的查询。事情是这样的:

I'm writing a Stored Procedure that currently contains only a SELECT query. It will be expanded to do a number of other things, which is why it has to be a Stored Procedure, but for now it is a simple query. Something like this:

SELECT姓名,职业,乔布斯位置...

我期待返回此查询的结果在C#中使用。我想将它添加到列表中,这样我可以把它绑定到一个GridView组件。我不知道如何去了解这一点,虽然。如果我有返回所有选定的数据后,将其插入到一个列表,那么这是正常的,我只需要知道如何正确地返回数据,这样我可以做到这一点。如果我能在可以右键被弹出到一个列表的形式返回它,不过,这将是理想的。

I'm looking to return the results of this query to be used in C#. I want to add it to a list so that I can bind it to a GridView component. I don't know how to go about this, though. If I have to insert it into a list after returning all selected data, then that's alright, I just need to know how to properly return the data so that I can do that. If I can return it in a format that can be popped right into a list, though, that would be ideal.

推荐答案

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

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. :)

这篇关于从选择查询的存储过程返回结果到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆