Using as parameters stored procedure DataTable

Recent work has written several stored procedures, need to pass the string to the stored procedure, because SQL Server 2000 is not built like split function, had to deal with their own, the reception of a data set with a comma split to keep a List <string> , and then converted to a string to a stored procedure, it is troublesome. Today saw the next SQL Server 2008 new features, found using a table of variables, and the DataTable as a parameter of usage, try a little, talk briefly about the experience.

 Sample code download

 

First, the test environment

1Windows Server 2008 R2 DataCenter

2Visual Studio 2008 Team System With SP1

3SQL Server 2008 Enterprise Edition With SP1

Because it is SQL Server 2008 new features, you can only use 2008 .

 

 Second, the test Overview

Test project is very simple to add new users

1.jpg

 

 

Third, prepare data

1 , the establishment of a database, table, type, stored procedures

 

ExpandedBlockStart.gif Code
 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  The 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  The END 23
  GO
24 
ExpandedBlockStart.gif Code
 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

Reception build a good form, the background is primarily a function of:

ExpandedBlockStart.gif Code
 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      }

  Click to call a stored procedure when [Add] button. Testing is completed, it is very simple, pass a DataTable parameter really easy to do it, can easily complete the previously required a lot of coding work. About table variable or some round after round, such as when you create determine whether the statement of its existence, you need to delete the stored procedures that reference a table variable and other variables before deleting the table. General development mostly I would choose to use a temporary table, processing more convenient, table variables can be used as stored procedure parameter is really a unique advantage, hoping to continue to enhance support for table variables and temporary tables in a future version of SQL Server, in particular, early support is a temporary table debugging :)

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/05/19/2050752.html

Guess you like

Origin blog.csdn.net/weixin_34060741/article/details/93496103