Display SQL Server table data in a browser

<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

   void page_load(Object sender, EventArgs e) {
   
       if(!IsPostBack) {
           Bind("");
       }
   }
   
   public void Bind(string table) {
       SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
       SqlDataAdapter da = new SqlDataAdapter("select name from sysobjects where xtype='U' and name<>'dtproperties'", con);
       DataSet ds = new DataSet();
       da.Fill(ds, "xxx");
       dg.DataSource = ds.Tables[0].DefaultView;
       dg.DataBind();
   
       //list columns in one table
       if(table!="") {
           SqlDataAdapter da2 = new SqlDataAdapter("SELECT syscolumns.name AS [Fields in Items Database], syscolumns.type, syscolumns.length, syscolumns.isnullable FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.name = '" + table + "' ORDER BY syscolumns.colid", con);
           DataSet ds2 = new DataSet();
           da2.Fill(ds2, "xxx");
           dg2.DataSource = ds2.Tables[0].DefaultView;
           dg2.DataBind();
       }
   }
   
   void dg_SelectedIndexChanged(Object sender, EventArgs e) {
       Bind(dg.SelectedItem.Text);
   }

</script>
<html>
<head>
</head>
<body>
   <form runat="server">
       <table border="0" cellpadding="5" cellspacing="5">
           <tbody>
               <tr>
                   <td valign="top" bgcolor="tan">
                       <asp:RadioButtonList id="dg" runat="server" DataValueField="Name" DataTextField="Name" AutoPostBack="True" OnSelectedIndexChanged="dg_SelectedIndexChanged"></asp:RadioButtonList>
                   </td>
                   <td valign="top">
                       <asp:DataGrid id="dg2" Cellpadding="2" BackColor="LightGoldenrodYellow" runat="server" GridLines="None" BorderWidth="1px" BorderColor="Tan" ForeColor="Black">
                           <FooterStyle backcolor="Tan"></FooterStyle>
                           <HeaderStyle font-bold="True" backcolor="Tan"></HeaderStyle>
                           <PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue" backcolor="PaleGoldenrod"></PagerStyle>
                           <SelectedItemStyle forecolor="GhostWhite" backcolor="DarkSlateBlue"></SelectedItemStyle>
                           <AlternatingItemStyle backcolor="PaleGoldenrod"></AlternatingItemStyle>
                       </asp:DataGrid>
                   </td>
               </tr>
           </tbody>
       </table>
   </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值