First, the test environment
1、Windows Server 2008 R2 DataCenter
2、Visual Studio 2008 Team System With SP1
3、SQL 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
Third, prepare data
1 , the establishment of a database, table, type, stored procedures
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
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:
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