我有一个像
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;
}
知道发生了什么吗?