存储过程从后面的代码返回空表但从sql查询返回一个表

时间:2015-01-23 20:46:47

标签: c# asp.net sql-server stored-procedures

我有一个sql 2008存储过程,它返回一个表。 当我从sql查询文件中调用它时,它会选择当前值,但是当从后面的代码调用时它什么也不返回。 可能有什么不对?

以下是存储过程:

Create Procedure getDGMT
(
    @gene varchar(500), @mut int, @patientId varchar(500)
)
As
Begin
    DECLARE @dgmtSelect TABLE(              
                drugGsId int,
                tumorTypeGsId int,
                geneGsID int,
                mutationGsId int,   
                priority int
                             );
    DECLARE @geneID int;    
    DECLARE @tumorID int;   

    select @geneID=geneGsID from gene where name=@gene; 
    select @tumorID=tumorTypeGsId from tumorType t inner join report r on t.name=r.tumorType where r.patientGsId=@patientId

    insert into @dgmtSelect select drugGsId, tumorTypeGsId, geneGsID, mutationGsId, priority from DGMT where tumorTypeGsId=@tumorID and geneGsID=@geneID and mutationGsId=@mut and priority=1
    DECLARE @v XML = (SELECT * FROM @dgmtSelect FOR XML AUTO)

    select * from @dgmtSelect;
End

如果我这样称呼它:

exec getDGMT 'GTA3', 1, '033333336'

它运行正常并选择:

但是,如果我从后面的代码中调用它:

public string getDGMT(int mut, string tumor, string gene)
     {
        DataTable dt = new DataTable();
        DataTable tbl2 = new DataTable();      

        using (SqlConnection con = connect("GeneSortDB"))
        {
            //string cmdTxt = "select drugGsId, tumorTypeGsId, geneGsID, mutationGsId, priority from DGMT where tumorTypeGsId=@tumorID and geneGsID=@geneID and mutationGsId=@mut and priority=@priority";
            using (SqlCommand cmd = new SqlCommand("getDGMT", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@gene", gene);
                cmd.Parameters.AddWithValue("@mut", mut);
                cmd.Parameters.AddWithValue("@patientId", tumor);
                //cmd.Parameters.AddWithValue("@priority", 1);

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(dt);
                }

            }  

它不会向数据表(dt)返回任何内容

以下是调用此函数时从客户端获取的变量:

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试在存储过程中使用SET NOCOUNT ON

N rows affected查询后,它会抑制insert消息。这可能会生成一个额外的结果集,您的客户端代码必须处理它。