1.在資料庫中建立Type 類型
Use TestTVP --假設已經建立了TestTVP 資料庫和TestTVP 的schemago--建立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
.....見預存程序的定義