从.net 程序中传递table 参数个存储过程

1.在数据库中创建Type 类型

Use TestTVP --假设已经建立了TestTVP 数据库和TestTVP 的schema
go

--创建TVP 
CREATE 
TYPE Test.TestTVP AS TABLE
{
    SNo varchar(20) Not NULL,
    SName varchar(50)
}
go

--创建使用TVP的存储过程  
Create  
PROCEDURE TestTVP.testTVP_Proc 
  @StudentTVP  Test.TestTVP 
as
  Select * from @StudentTVP  
  -- 对@StudentTVP  做其它操作...

go 

2.连接上数据库并使用对的Type创建DBTable
SqlConnection conn= new SqlConnection();
conn.ConnectionString = "initial catalog=northwind;data source=localhost;Integrated Security=SSPI;connect Timeout=20";
conn.Open(); 

DataTable tableTVP = this.CreateDataTableFromTvpTypeName(conn,"Test.TestTVP ");

//对tableTVP 进行填充

 ......

  3.将DBTable传给存储过程  
SqlCommand TVPcmd = new SqlCommand ("TestTVP.testTVP_Proc",conn);
TVPcmd. CommandType = 
CommandType.StoredProcedure; 
SqlParameter TVPSqlParameter=
TVPcmd.Parameters.AddWithValue("@StudentTVP", tableTVP);  
TVPSqlParameter.SqlDbType = System.Data.SqlDbType.Structured; 
TVPSqlParameter.TypeName ="Test.TestTVP ";
TVPcmd.ExecuteNonQuery();

4.在存储过程使用传入的DBTable
.....见存储过程的定义  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值