如何使用Web服务在HTML页面中显示sql server数据的实际结果?

时间:2017-04-06 09:49:10

标签: sql-server web-services stored-procedures html-table

我使用以下商店程序。

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 Service中显示相同的输出。在此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 server的确切输出(不使用Serialize或更改代码)。

1 个答案:

答案 0 :(得分:4)

用select:

替换打印
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();
            }
        }