如何使用我传递的参数使存储过程返回"数据集"?

Ser*_*pia 6 sql t-sql

我对存储过程很新.

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

找到我所有的IDCategory列表等于我告诉你要找的IDCategory.

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

IDListing,IDCategory,Price,Seller,Image.

我怎么能实现这个目标?

Kev*_*che 5

要从存储过程填充数据集,您将拥有如下代码:

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);
Run Code Online (Sandbox Code Playgroud)

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

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

CREATE PROC [dbo].[IDCategory] 
    @IDCategory int
AS 
    SELECT IDListing, IDCategory, Price, Seller, Image
         FROM whateveryourtableisnamed
         WHERE IDCategory = @IDCategory
Run Code Online (Sandbox Code Playgroud)

以下是存储过程基础知识的链接: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