0

我有一个像

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;
}

知道发生了什么吗?

4

1 回答 1

1

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

这是您的源代码的副本,所需的更改最少。请注意,当您使用 a 时,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;
}
于 2012-05-15T18:57:37.867 回答