2017-04-06 27 views
3

我正在使用下面的存儲過程。如何使用Web Service在HTML頁面中顯示sql server數據的實際結果?

declare @body varchar(max) 

set @body = cast((
select td = cast(iVoucher_Type_id as varchar(30)) + '</td><td>' + cast(cVoucher_Type as varchar(30)) + '</td><td>' + cast(dVoucher_date as varchar(30)) 
from (
     select 
     iVoucher_Type_id, 
     cVoucher_Type, 
     dVoucher_date 
     from master.dbo.Mast_Voucher_Type 
    ) as d 
for xml path('tr'), type) as varchar(max)) 

set @body = '<tr><th>Voucher Type ID</th><th>Voucher Type</th><th>Voucher Date</th></tr>' 
      + replace(replace(@body, '&lt;', '<'), '&gt;', '>') 

print @body 

在此其返回的結果類似下面的格式

<tr> 
    <th>Voucher Type ID</th> 
    <th>Voucher Type</th> 
    <th>Voucher Date</th> 
</tr> 
<tr> 
    <td>1</td> 
    <td>test 1</td> 
    <td>Mar 27 2016 4:08PM</td> 
</tr> 

我用下面的Web服務代碼。在這個如何顯示相同的輸出在Web服務。在此Web服務輸出不工作其顯示爲空[]。

[WebMethod()] 
     public string Getdataset(string strQuery, string strCon) 
     { 
      DataTable dt = new DataTable(); 
      using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection()) 
      { 
       conn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["BB_CONSTR"]; 
       using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) 
       { 
        cmd.CommandText = strQuery; 
        cmd.Connection = conn; 
        conn.Open(); 
        SqlDataAdapter da = new SqlDataAdapter(cmd); 
        da.Fill(dt); 
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
        Dictionary<string, object> row = default(Dictionary<string, object>); 
        foreach (DataRow dr in dt.Rows) 
        { 
         row = new Dictionary<string, object>(); 
         foreach (DataColumn col in dt.Columns) 
         { 
          row.Add(col.ColumnName, dr[col]); 
         } 
         rows.Add(row); 
        } 
        return serializer.Serialize(rows); 
       } 
      } 
     } 

需要web服務顯示SQL服務器的精確的輸出(無序列化或改變代碼)。

+0

你肯定渲染在服務器端的表是好主意嗎?通常Web服務只返回數據,代表應該在客戶端上完成 –

+0

是的..但我需要此頁特別如我問.. – Meivel

回答

4

與選擇更換打印:

declare @body varchar(max) 

set @body = cast((
select td = cast(iVoucher_Type_id as varchar(30)) + '</td><td>' + cast(cVoucher_Type as varchar(30)) + '</td><td>' + cast(dVoucher_date as varchar(30)) 
from (
     select 
     iVoucher_Type_id, 
     cVoucher_Type, 
     dVoucher_date 
     from master.dbo.Mast_Voucher_Type 
    ) as d 
for xml path('tr'), type) as varchar(max)) 

set @body = '<tr><th>Voucher Type ID</th><th>Voucher Type</th><th>Voucher Date</th></tr>' 
      + replace(replace(@body, '&lt;', '<'), '&gt;', '>') 

SELECT @body 

返回結果沒有任何系列化

[WebMethod()] 
     public string Getdataset(string strQuery, string strCon) 
     { 
      string connStr = System.Configuration.ConfigurationManager.AppSettings["BB_CONSTR"]; 
      using (SqlConnection conn = new SqlConnection(connStr)) 
      { 
       SqlCommand cmd = new SqlCommand() 
       cmd.CommandText = strQuery; 
       cmd.Connection = conn; 
       conn.Open(); 
       object result = cmd.ExecuteScalar(); 
       if (result == DBNull.Value) throw new ApplicationException("Oh no"); 
       return result.ToString(); 
      } 
     } 
+0

謝謝老兄...非常感謝.... !!!它正在工作。我已經嘗試了2天。你清除了我的懷疑......再次感謝...... – Meivel

相關問題