加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – 如何使用我传递的参数使存储过程返回“数据集”?

发布时间:2020-12-12 08:54:55 所属栏目:MsSql教程 来源:网络整理
导读:我对存储过程很新. 假设我有一个IDCategory(int)并将其传递给存储过程.在正常谈话中,它会: Find me all the listings with an IDCategory equal to the IDCategory I’m telling you to find. 所以它会找到说3列表,并创建一个包含列的表: IDListing,IDCateg
我对存储过程很新.

假设我有一个IDCategory(int)并将其传递给存储过程.在正常谈话中,它会:

Find me all the listings with an
IDCategory equal to the IDCategory I’m
telling you to find.

所以它会找到说3列表,并创建一个包含列的表:

IDListing,IDCategory,Price,Seller,Image.

我怎么能实现这个目标?

解决方法

要从存储过程填充数据集,您将拥有如下代码:
SqlConnection mySqlConnection =new SqlConnection("server=(local);database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText = "IDCategory";
    mySqlCommand.CommandType = CommandType.StoredProcedure;
    mySqlCommand.Parameters.Add("@IDCategory",SqlDbType.Int).Value = 5;

    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
    mySqlDataAdapter.SelectCommand = mySqlCommand;
    DataSet myDataSet = new DataSet();
    mySqlConnection.Open();
    mySqlDataAdapter.Fill(myDataSet);

你的连接字符串会有所不同,有几种不同的方法可以做到这一点,但这应该让你去….一旦你掌握了一些这些,请看看使用声明.它有助于清理资源,并且需要少量代码.这假定存储过程名称IDCategory,其中一个参数称为相同.您的设置可能略有不同.

在这种情况下,您的存储过程将类似于:

CREATE PROC [dbo].[IDCategory] 
    @IDCategory int
AS 
    SELECT IDListing,Image
         FROM whateveryourtableisnamed
         WHERE IDCategory = @IDCategory

以下是存储过程基础知识的链接:
http://www.sql-server-performance.com/articles/dba/stored_procedures_basics_p1.aspx

这是ADO.Net上的DataSet和其他项目的链接:
http://authors.aspalliance.com/quickstart/howto/doc/adoplus/adoplusoverview.aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读