從.net 程式中傳遞table 參數個預存程序

來源:互聯網
上載者:User

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
.....見預存程序的定義 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.