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, '<', '<'), '>', '>')
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服務器的精確的輸出(無序列化或改變代碼)。
你肯定渲染在服務器端的表是好主意嗎?通常Web服務只返回數據,代表應該在客戶端上完成 –
是的..但我需要此頁特別如我問.. – Meivel