2012-05-15 102 views
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; 
} 

任何想法是怎麼回事?

+0

@Siva可能是正確的:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.parametername.aspx – Yatrix

+0

是啊,這是有道理的,但修復它後,我仍然得到相同的結果 – Josh

回答

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; 
} 
相關問題