将DataTable作为存储过程参数的用法
比较新奇的用法。
最近工作中写了几个存储过程,需要向存储过程中传递字符串,因为SQL Server 2000中没有内置类似于 split 的函数,只好自己处理,将前台数据集中的一列用逗号拆分存到一个List<string>中,再转化为字符串传给存储过程,很是麻烦。今天看了下SQL Server 2008的新特性,发现有表变量的使用,及其将DataTable作为参数的用法,就尝试了一下,简单谈谈心得。
一、测试环境
1、Windows Server 2008 R2 DataCenter
2、Visual Studio 2008 Team System With SP1
3、SQL Server 2008 Enterprise Edition With SP1
由于是SQL Server 2008新特性,所以只能用2008。
二、测试概述
测试项目很简单,就是添加新用户
三、准备数据
1、建立数据库、表、类型、存储过程
代码
1 IF NOT EXISTS(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('Users') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
2 BEGIN
3 CREATE TABLE dbo.Users
4 (
5 UserID INT IDENTITY(-1, -1) NOT NULL,
6 UserName VARCHAR(20) NOT NULL,
7 UserPass VARCHAR(20) NOT NULL,
8 Sex BIT NULL,
9 Age SMALLINT NULL,
10 CONSTRAINT PK_Users_UserID PRIMARY KEY(UserID)
11 )
12 END
13 IF NOT EXISTS(SELECT * FROM sys.table_types WHERE name = 'UserTable' AND is_user_defined = 1)
14 BEGIN
15 CREATE TYPE UserTable AS TABLE
16 (
17 UserName VARCHAR(20) NOT NULL,
18 UserPass VARCHAR(20) NOT NULL,
19 Sex BIT NULL,
20 Age SMALLINT NULL
21 )
22 END
23 GO
24
2 BEGIN
3 CREATE TABLE dbo.Users
4 (
5 UserID INT IDENTITY(-1, -1) NOT NULL,
6 UserName VARCHAR(20) NOT NULL,
7 UserPass VARCHAR(20) NOT NULL,
8 Sex BIT NULL,
9 Age SMALLINT NULL,
10 CONSTRAINT PK_Users_UserID PRIMARY KEY(UserID)
11 )
12 END
13 IF NOT EXISTS(SELECT * FROM sys.table_types WHERE name = 'UserTable' AND is_user_defined = 1)
14 BEGIN
15 CREATE TYPE UserTable AS TABLE
16 (
17 UserName VARCHAR(20) NOT NULL,
18 UserPass VARCHAR(20) NOT NULL,
19 Sex BIT NULL,
20 Age SMALLINT NULL
21 )
22 END
23 GO
24
代码
1 IF EXISTS(SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID('sp_InsertSingleUser') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
2 BEGIN
3 DROP PROCEDURE dbo.sp_InsertSingleUser
4 END
5 GO
6 CREATE PROCEDURE dbo.sp_InsertSingleUser
7 (
8 @User UserTable READONLY
9 )
10 AS
11
12 SET XACT_ABORT ON
13 BEGIN TRANSACTION
14
15 INSERT INTO dbo.Users(UserName, UserPass, Sex, Age)
16 SELECT UserName, UserPass, Sex, Age FROM @User
17
18 COMMIT TRANSACTION
19 SET XACT_ABORT OFF
20 GO
2 BEGIN
3 DROP PROCEDURE dbo.sp_InsertSingleUser
4 END
5 GO
6 CREATE PROCEDURE dbo.sp_InsertSingleUser
7 (
8 @User UserTable READONLY
9 )
10 AS
11
12 SET XACT_ABORT ON
13 BEGIN TRANSACTION
14
15 INSERT INTO dbo.Users(UserName, UserPass, Sex, Age)
16 SELECT UserName, UserPass, Sex, Age FROM @User
17
18 COMMIT TRANSACTION
19 SET XACT_ABORT OFF
20 GO
前台搭建好表单,后台主要是一个函数:
代码
1 public void fnInsertSingleUser(DataTable v_dt)
2 {
3 try
4 {
5 SqlConnection cn = new SqlConnection(CONN);
6 SqlCommand cmd = cn.CreateCommand();
7 cmd.CommandType = CommandType.StoredProcedure;
8 cmd.CommandText = @"sp_InsertSingleUser";
9 SqlParameter p = cmd.Parameters.AddWithValue("@User", v_dt);
10
11 DataSet ds = new DataSet();
12 SqlDataAdapter da = new SqlDataAdapter(cmd);
13 da.Fill(ds);
14 }
15 catch (Exception ex)
16 {
17 throw ex;
18 }
19 }
2 {
3 try
4 {
5 SqlConnection cn = new SqlConnection(CONN);
6 SqlCommand cmd = cn.CreateCommand();
7 cmd.CommandType = CommandType.StoredProcedure;
8 cmd.CommandText = @"sp_InsertSingleUser";
9 SqlParameter p = cmd.Parameters.AddWithValue("@User", v_dt);
10
11 DataSet ds = new DataSet();
12 SqlDataAdapter da = new SqlDataAdapter(cmd);
13 da.Fill(ds);
14 }
15 catch (Exception ex)
16 {
17 throw ex;
18 }
19 }
点击【添加】按钮时调用存储过程。测试是完成了