1
CREATE PROCEDURE dbo.Ticket_InsertTicket
2
(
3
@IsExist BIT OUTPUT,
4
@StartStation nvarchar(20),
5
@EndStation nvarchar(50),
6
@Price money,
7
@LowDiscount tinyint,
8
@HighDiscount tinyint,
9
@IsInLand bit,
10
@Continent nvarchar(20) = NULL
11
)
12
AS
13
SET NOCOUNT OFF;
14
IF EXISTS ( SELECT ID FROM [Ticket] WHERE ( [StartStation]=@StartStation AND [EndStation]=@EndStation ) )
15
BEGIN
16
SET @IsExist = 1
17
RETURN
18
END
19
ELSE
20
BEGIN
21
INSERT INTO [Ticket] ([StartStation], [EndStation], [Price], [LowDiscount], [HighDiscount], [IsInLand], [Continent]) VALUES (@StartStation, @EndStation, @Price, @LowDiscount, @HighDiscount, @IsInLand, @Continent);
22
SELECT ID, StartStation, EndStation, StartStation + '至' + EndStation AS Line, Price,
23
LowDiscount, HighDiscount, CAST(LowDiscount AS NVARCHAR(4))+'至'+CAST(HighDiscount AS NVARCHAR(4))+'折' AS Discount, IsInLand, Continent
24
FROM Ticket WHERE (ID = SCOPE_IDENTITY())
25
END
26
GO
27
该存储过程不用详细介绍,只需了解@IsExist为输出参数,并且如已存在相同的StartStation和EndStation记录,则不执行插入操作,@IsExist值为1,否则执行相应的插入操作,@IsExist值为0。
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

然后创建Dataset,对应的Dataset见下图:

注意其中Ticket表中的IsExist字段,该字段的具体属性可参考下图:

配置好Dataset后,下一步是在BLL层中对上述@IsExist参数的调用过程,下面是BLL层插入方法的代码,注意其中的一个ref类型的参数IsExist,调用DAL后的对应结果将通过该参数返回给界面层:
1
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
2
public bool InsertTicket(ref bool IsExist, string StartStation, string EndStation,
3
decimal Price, byte LowDiscount, byte HighDiscount,bool IsInLand,string Continent)
4
{
5
dsForeground.TicketDataTable dtTable = new dsForeground.TicketDataTable();
6
dsForeground.TicketRow dtRow = dtTable.NewTicketRow();
7
8
dtRow.StartStation = StartStation.Trim();
9
dtRow.EndStation = EndStation.Trim();
10
dtRow.Price = Price;
11
dtRow.LowDiscount = LowDiscount;
12
dtRow.HighDiscount = HighDiscount;
13
dtRow.IsInLand = IsInLand;
14
if (IsInLand == false)
15
{
16
if (string.IsNullOrEmpty(Continent))
17
throw new ApplicationException(Resources.Resource.ContinentInputError);
18
else
19
dtRow.Continent = Continent.Trim();
20
}
21
else
22
dtRow.Continent = string.Empty;
23
dtTable.AddTicketRow(dtRow);
24
int affectedRow = Adapter.Update(dtRow);
25
//获取执行结果
26
IsExist = dtRow.IsExist;
27
return affectedRow == 1;
28
}
29

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

界面层:本实例使用了FormView来作为记录的插入的容器,要判断插入操作的详细信息,可在插入后的事件代码中执行下面的语句,根据IsExist的值可判断究竟是插入成功还是记录已经存在而未执行插入操作。
1
protected void ObjectDS_InLandManage_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
2
{
3
//此处代码略
4
IsExist = Convert.ToBoolean(e.OutputParameters["IsExist"]);
5
//此处代码略
6
}
7

2

3

4

5

6

7

总结:在强类型Dataset中使用存储过程输出参数,可获取复杂的存储过程的执行结果,在一次访问数据库时就可完成尽可能多的工作,提高程序访问数据库操作的效率和性能。