从代码执行时存储过程不返回值,从管理工作室正常工作

时间:2012-05-15 18:27:22

标签: c# sql-server xml

我喜欢

CREATE PROCEDURE [dbo].[ExportTestAsXML]
    @TestId         int
AS
BEGIN
    SET NOCOUNT ON;

    declare @x xml;
    set @x = (
       select
          (select * from table1 where testid = @TestId FOR XML auto, Type, Elements),
          (select * from table2 where testid = @TestId FOR XML auto, Type, Elements),
          (select * from table3 
           where objectiveid in 
               (select objectiveid from objectives where testid = @TestId) 
           FOR XML auto, Type, Elements),
          (select * from table4 
           where objectiveid in 
               (select objectiveid from objectives where testid = @TestId) 
           FOR XML auto, Type, Elements),
          (select * from table5 
           where questionid in 
               (select questionid from questions 
                where objectiveid in 
                      (select objectiveid from objectives where testid = @TestId)
               ) 
           FOR XML auto, Type, Elements)
       for xml path(''), type
    );

   select @x as my_xml
END

当我从SQL Server 2005 Management Studio运行它时,我得到一个包含单个记录的表,其中包含select语句中的组合XML。当我从我的Web服务代码运行它并使用数据表可视化工具检查表时它是空的。这是我用来执行proc

的代码
SqlParameter[] Parameters = new SqlParameter[1];
Parameters[0] = new SqlParameter();
Parameters[0].ParameterName = "@TestId";
Parameters[0].Value = TestId;
Parameters[0].SqlDbType = SqlDbType.Int;
Parameters[0].Size = 50;

DataTable data = ExecuteDataSet("ExportTestAsXML", Parameters);

private DataTable ExecuteDataSet(string storedProcName, SqlParameter[] parameters)
{
    SqlCommand command = new SqlCommand();
    command.CommandText = storedProcName;
    command.Parameters.AddRange(parameters);
    command.CommandType = CommandType.StoredProcedure;
    command.Connection = (SqlConnection)dcMUPView.Connection;

    command.Connection.Open();
    command.Prepare();

    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataTable ds = new DataTable();
    adapter.Fill(ds);

    command.Connection.Close();
    return ds;
}

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:1)

如果您填充DataSet而不是DataTable,则您的示例有效。

以下是源代码的副本,需要进行最少的更改。请注意,当您使用DataSet时,您应该添加代码以检查是否返回了任何表,以及第一个表中是否有行等等。

呼叫者:

SqlParameter[] Parameters = new SqlParameter[1];
Parameters[0] = new SqlParameter();
Parameters[0].ParameterName = "@TestId";
Parameters[0].Value = TestId;
Parameters[0].SqlDbType = SqlDbType.Int;
Parameters[0].Size = 50;

DataSet data = ExecuteDataSet("ExportTestAsXML", Parameters);

// Read First table (Tables[0]), First Row (Rows[0]), First Column of that Row (Rows[0][0])
System.Diagnostics.Debug.Write(data.Tables[0].Rows[0][0]);

方法:

private DataSet ExecuteDataSet(string storedProcName, SqlParameter[] parameters)
{
    SqlCommand command = new SqlCommand();
    command.CommandText = storedProcName;
    command.Parameters.AddRange(parameters);
    command.CommandType = CommandType.StoredProcedure;
    command.Connection = (SqlConnection)dcMUPView.Connection;

    command.Connection.Open();
    command.Prepare();

    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataSet ds = new DataSet ();
    adapter.Fill(ds);

    command.Connection.Close();
    return ds;
}