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