对存储过程的调用什么都不返回

时间:2010-09-17 17:09:33

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

我在sql server上调用存储过程,如下所示:

SqlConnection conn = new SqlConnection();
SqlCommand cmd;
XmlDocument xmlDocument;
XmlReader xr;
XmlNode node;
SqlDataReader rdr = null;

try
{
    xmlDocument = new XmlDocument();
    conn.ConnectionString = "Data Source=test;Initial Catalog=teste;Integrated Security=SSPI;";
    cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "[dbo].[spSearchKeywords]";

    cmd.Parameters.Add(new SqlParameter("@VALUE", "XPT"));

    conn.Open();

    xr = cmd.ExecuteXmlReader();

    conn.Close();
    node = xmlDocument.ReadNode(xr);
}

它连接并执行命令,但它什么都不返回 有数据要返回,参数是正确的(当我在sql中使用相同的参数调用过程时,它会返回一个结果)

这是proc:

ALTER PROCEDURE [dbo].[spSearchKeywords]
(
    @VALUE                 NVARCHAR(50)  = NULL,
    @ACCOUNTGROUPID         NVARCHAR(50)  = NULL,
    @ShortCodeId         NVARCHAR(50)  = NULL,
    @VALUETYPE             NVARCHAR(50)  = NULL,
    @ASSEMBLY             NVARCHAR(100) = NULL,
    @ASSEMBLYCONTAINSURI NCHAR   (10)  = NULL,
    @ASSEMBLYTYPE         NVARCHAR(50)  = NULL
)
AS
BEGIN

    SET NOCOUNT ON;

    SELECT [Value]
          ,[AccountGroupId]
          ,[ShortCodeId]
          ,[ValueType]
          ,[assembly]
          ,[assemblyContainsUri]
          ,[assemblyType]
      FROM [teste].[dbo].[keywords]
     WHERE [Value]                 = ISNULL(@VALUE,                [Value])
       AND [AccountGroupId]         = ISNULL(@ACCOUNTGROUPID,        [AccountGroupId])
       AND [ShortCodeId]         = ISNULL(@SHORTCODEID,            [ShortCodeId])
       AND [ValueType]             = ISNULL(@VALUETYPE,            [ValueType])
       AND [assembly]             = ISNULL(@ASSEMBLY,            [assembly])
       AND [assemblyContainsUri] = ISNULL(@ASSEMBLYCONTAINSURI, [assemblyContainsUri])
       AND [assemblyType]         = ISNULL(@ASSEMBLYTYPE,        [assemblyType])
     FOR XML AUTO
END

1 个答案:

答案 0 :(得分:8)

在实际使用XmlReader之前,无法关闭连接。尝试删除conn.Close()以下node = xmlDocument.ReadNode(xr);。并考虑一次性数据库对象的using语句。