2

的情況:調用從CLR存儲過程中的存儲過程與表值參數

是建立一個數據表,然後試圖調用存儲過程與TVP參數的SQL

一個CLR存儲過程。

代碼:

public class tvp_test : System.Data.DataTable 
    { 
    public tvp_test() 
    { 
     this.Column.Add("field1",typeof(System.Int32)); 
     this.Column.Add("field2",typeof(System.Int32)); 
    } 
    } 

............................................................. 

    { 
    var tvp = new tvp_test(); 

............................................................. 

    using(SqlConnection con = new SqlConnection("context connection=true")) 
    { 
     con.Open(); 
     var command = con.CreateCommand(); 

     command.CommandType = CommandType.StoredProcedure; 
     command.CommandTest = "prc_test"; 
     command.Parameters.Add("@a",System.Data.SqlDbType.Structured); 
     command.Parameters["@a"].Value = tvp; 
     command.ExecuteNonQuery(); 

     con.Close(); 
    } 
    } 

.................................... ..................................

create procedure prc_test 
    @a tvp_test readonly 
begin 
    insert into #tmp (field1,field2) /* #tmp is created before the call of the CLR SP */ 
    select field1,field2 from @a 
end 

問題:

行爲是不穩定的。有一次,它在x(其中x是隨機的)之後起作用,它會給我y(其中y是隨機的)「發生嚴重錯誤」,沒有其他細節,之後循環將再次開始。從我看到問題出現在CLR SP端,因爲錯誤開始發生時,由於錯誤保持不變,SQL SP可以更改併發生錯誤。 也許CLR SP和TVP之間沒有愛,但是我沒有找到任何關於此的參考。作爲一個側面說明,它的工作原理可能會工作很長一段時間,但如果我刪除了SP計劃,那麼它將開始錯誤。 (在那個「觸發器」中也找不到邏輯)

有什麼想法? 預先感謝您。

+1

向我們展示SP的代碼也 – 2012-01-05 07:16:01

+0

,你能否告訴我們的表型的定義是什麼? – 2012-09-10 20:50:37

+0

它複製DataTable,tvp_test與2可以爲空:創建類型dbo.tvp_test作爲表(field1 int null,field2 int null) – 2012-09-11 06:41:23

回答

0

看起來像這條線有錯誤。

command.CommandTest = "prc_test"; 

將其更改爲

command.CommandText = "prc_test"; 
+0

雖然這是真的,我不認爲行爲是連接到那。 由於'SqlCommand'中沒有'CommandTest'屬性,程序根本不會編譯。 – jAC 2015-07-10 21:18:24