1C#操作存储过程,输入参数,返回结果set ANSI_NULLS ON
 2C#操作存储过程,输入参数,返回结果set QUOTED_IDENTIFIER ON
 3C#操作存储过程,输入参数,返回结果go
 4C#操作存储过程,输入参数,返回结果
 5C#操作存储过程,输入参数,返回结果
 6C#操作存储过程,输入参数,返回结果-- =============================================
 7C#操作存储过程,输入参数,返回结果-- Author:        <Vitoria Tang>
 8C#操作存储过程,输入参数,返回结果-- Create date: <2006.0804>
 9C#操作存储过程,输入参数,返回结果-- Description:    <It is a sample procedure for get sales record as specific datetime>
10C#操作存储过程,输入参数,返回结果-- =============================================
11C#操作存储过程,输入参数,返回结果ALTER PROCEDURE [dbo].[User_SalesByYear] 
12C#操作存储过程,输入参数,返回结果    -- Input parameters: Begin date, End date
13C#操作存储过程,输入参数,返回结果    @BeginDate datetime , @EndDate datetime@Price int = 0 OUTPUT
14C#操作存储过程,输入参数,返回结果AS
15C#操作存储过程,输入参数,返回结果BEGIN
16C#操作存储过程,输入参数,返回结果    --declare @Price money
17C#操作存储过程,输入参数,返回结果    CREATE TABLE #TempTable
18C#操作存储过程,输入参数,返回结果    (ID int not null, ProductName nvarchar(40not null, Price money not null)
19C#操作存储过程,输入参数,返回结果
20C#操作存储过程,输入参数,返回结果    INSERT INTO #TempTable(ID, ProductName, Price)
21C#操作存储过程,输入参数,返回结果        SELECT Orders.OrderID,  Products.ProductName, [Order Details].UnitPrice * [Order Details].Discount
22C#操作存储过程,输入参数,返回结果            FROM Orders, Products, [Order Details]
23C#操作存储过程,输入参数,返回结果            WHERE (
24C#操作存储过程,输入参数,返回结果                ((Orders.ShippedDate) Is Not Null And 
25C#操作存储过程,输入参数,返回结果                    (Orders.ShippedDate) Between @BeginDate And @EndDate)
26C#操作存储过程,输入参数,返回结果                    AND 
27C#操作存储过程,输入参数,返回结果                    (Orders.OrderID = [Order Details].OrderID AND [Order Details].ProductID = Products.ProductID)
28C#操作存储过程,输入参数,返回结果                );
29C#操作存储过程,输入参数,返回结果
30C#操作存储过程,输入参数,返回结果   Select * from #TempTable
31C#操作存储过程,输入参数,返回结果    
32C#操作存储过程,输入参数,返回结果   Select @Price = sum(Price) from [#TempTable]
33C#操作存储过程,输入参数,返回结果   Print @Price
34C#操作存储过程,输入参数,返回结果   return (@Price)
35C#操作存储过程,输入参数,返回结果END
36C#操作存储过程,输入参数,返回结果

Sample界面:
C#操作存储过程,输入参数,返回结果

        首先建立与数据库的连接:

1C#操作存储过程,输入参数,返回结果private System.Data.SqlClient.SqlConnection _connection = null;
2C#操作存储过程,输入参数,返回结果private void InitializeSource()
3

        其次创建执行存储过 程的SqlCommand,当然前后需要打开和关闭数据库连接,调用存储过程需要给SqlCommand的CommandType属性赋值为 CommandType.StoredProcedure,我们的存储过程是有输入和输出参数的,那么在添加到SqlCommand.Prameters 集合中就可以了。如果是输出参数,设置SqlParameter实例的Direction值就可以了,该属性默认值为Input,所以不设置的话,在执行 完后,得不到输出值哦。具体参见下面的代码。

 1C#操作存储过程,输入参数,返回结果private SqlCommand GetCommand(object sender, EventArgs e)
 2

        本文的示例是用SqlDataAdapter来填充了DataSet并把它显示在DataGridView control上,所以接下来,打开数据库连接创建SqlDataAdapter,并填充DataSet吧.

 1C#操作存储过程,输入参数,返回结果private void button1_Click(object sender, EventArgs e)
 2

相关文章:

  • 2022-12-23
  • 2021-04-25
  • 2021-12-27
  • 2021-09-08
  • 2021-11-27
  • 2022-02-23
  • 2022-01-05
猜你喜欢
  • 2021-08-16
  • 2022-01-29
  • 2021-10-14
  • 2022-12-23
相关资源
相似解决方案