1
set ANSI_NULLS ON
2
set QUOTED_IDENTIFIER ON
3
go
4
5
6
-- =============================================
7
-- Author: <Vitoria Tang>
8
-- Create date: <2006.0804>
9
-- Description: <It is a sample procedure for get sales record as specific datetime>
10
-- =============================================
11
ALTER PROCEDURE [dbo].[User_SalesByYear]
12
-- Input parameters: Begin date, End date
13
@BeginDate datetime , @EndDate datetime, @Price int = 0 OUTPUT
14
AS
15
BEGIN
16
--declare @Price money
17
CREATE TABLE #TempTable
18
(ID int not null, ProductName nvarchar(40) not null, Price money not null)
19
20
INSERT INTO #TempTable(ID, ProductName, Price)
21
SELECT Orders.OrderID, Products.ProductName, [Order Details].UnitPrice * [Order Details].Discount
22
FROM Orders, Products, [Order Details]
23
WHERE (
24
((Orders.ShippedDate) Is Not Null And
25
(Orders.ShippedDate) Between @BeginDate And @EndDate)
26
AND
27
(Orders.OrderID = [Order Details].OrderID AND [Order Details].ProductID = Products.ProductID)
28
);
29
30
Select * from #TempTable
31
32
Select @Price = sum(Price) from [#TempTable]
33
Print @Price
34
return (@Price)
35
END
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Sample界面:
首先建立与数据库的连接:
1
private System.Data.SqlClient.SqlConnection _connection = null;
2
private void InitializeSource()
3
2
3
其次创建执行存储过 程的SqlCommand,当然前后需要打开和关闭数据库连接,调用存储过程需要给SqlCommand的CommandType属性赋值为 CommandType.StoredProcedure,我们的存储过程是有输入和输出参数的,那么在添加到SqlCommand.Prameters 集合中就可以了。如果是输出参数,设置SqlParameter实例的Direction值就可以了,该属性默认值为Input,所以不设置的话,在执行 完后,得不到输出值哦。具体参见下面的代码。
1
private SqlCommand GetCommand(object sender, EventArgs e)
2
2
本文的示例是用SqlDataAdapter来填充了DataSet并把它显示在DataGridView control上,所以接下来,打开数据库连接创建SqlDataAdapter,并填充DataSet吧.
1
private void button1_Click(object sender, EventArgs e)
2
2