zoukankan      html  css  js  c++  java
  • Web页面执行SQL语句(附源码下载)

    通过页面来管理数据库,进行常见的增删查改。

    页面:

     1<div style="background:#A4B6D7;padding:4px;">您的当前位置:系统管理&raquo;&raquo;执行SQL</div>
     2<div style="vertical-align:top; margin-top:10px; margin-left:10px;">
     3    <asp:TextBox ID="txtSQL" runat="server" TextMode="MultiLine" Height="80px" Width="90%" BorderStyle="Inset" OnTextChanged="txtSQL_TextChanged"></asp:TextBox>
     4    <br />
     5    <asp:Button ID="btnExeSql" runat="server" CssClass="button" Text="执行SQL" OnClick="btnExeSql_Click" />
     6    <asp:Label ID="lblExeNum" runat="server"></asp:Label>
     7<href="javascript:void(0)" onclick="Open(document.getElementById('table').innerHTML)">查看表结构</a>
     8    <div id="table" style="display:none;">
     9    <asp:GridView ID="grdTable" runat="server" Font-Size="12px" Width="100%">
    10        <RowStyle HorizontalAlign="Center" CssClass="tItem" />
    11        <PagerStyle CssClass="tPage" />
    12        <HeaderStyle CssClass="tHeader" />
    13        <AlternatingRowStyle CssClass="tAlter" />
    14        <SelectedRowStyle BackColor="#F1F5FB" />
    15    </asp:GridView>
    16    </div>
    17    <hr style="border-collapse:collapse; 90%; text-align:left;" />
    18</div>
    19    <asp:GridView ID="grdSQL" runat="server" Width="100%" Visible="False">
    20        <RowStyle HorizontalAlign="Center" CssClass="tItem" />
    21        <PagerStyle CssClass="tPage" />
    22        <HeaderStyle CssClass="tHeader" />
    23        <AlternatingRowStyle CssClass="tAlter" />
    24        <SelectedRowStyle BackColor="#F1F5FB" />
    25    </asp:GridView>
    26<script type="text/javascript">
    27var Osel=document.aspnetForm;
    28Osel.onsubmit=function()
    29{
    30    if(Osel.<%=txtSQL.ClientID %>.value=="")
    31    {
    32        alert("输入不可为空");
    33        Osel.<%=txtSQL.ClientID %>.focus();
    34        return false;
    35    }

    36    else if(Osel.<%=txtSQL.ClientID %>.value.indexOf("update")!=-1 || Osel.<%=txtSQL.ClientID %>.value.indexOf("delete")!=-1 || Osel.<%=txtSQL.ClientID %>.value.indexOf("truncate")!=-1)
    37    {
    38        if(confirm("即将执行的操作带有一定的风险,是否继续?"))
    39            return true;
    40        else 
    41            return false;
    42    }

    43}

    44
    45function Open(value) 
    46
    47  var TestWin=open('','','toolbar=no, scrollbars=yes, menubar=no, location=no, resizable=no'); 
    48  TestWin.document.title="数据库表结构";
    49  TestWin.document.write(value); 
    50}

    51
    </script>
    52
     1    protected void Page_Load(object sender, EventArgs e)
     2    {
     3        if (!IsPostBack)
     4        {
     5            GetTableName();
     6        }

     7    }

     8
     9    /// <summary>
    10    /// 获取数据表结构
    11    /// </summary>

    12    protected void GetTableName()
    13    {
    14        DataTable dt = DBHelper.Connection.GetSchema("Tables"null);
    15        DBHelper.Connection.Close();
    16        grdTable.DataSource = dt;
    17        grdTable.DataBind();
    18    }

    19
    20    /// <summary>
    21    /// 执行操作
    22    /// </summary>
    23    /// <param name="sender"></param>
    24    /// <param name="e"></param>

    25    protected void btnExeSql_Click(object sender, EventArgs e)
    26    {
    27        string sql = txtSQL.Text.Trim().ToLower();
    28        int intExeNum;
    29
    30        try
    31        {
    32            if (sql.Substring(06).IndexOf("select"!= -1)
    33            {
    34                DataTable dt = DBHelper.GetDataSet(sql);
    35                grdSQL.DataSource = dt;
    36                grdSQL.DataBind();
    37                lblExeNum.Text = "返回记录条数:<strong>" + dt.Rows.Count + "</strong>";
    38                grdSQL.Visible = true;
    39            }

    40            else if (sql.Substring(06).IndexOf("delete"!= -1 || sql.Substring(06).IndexOf("update"!= -1 || sql.Substring(08).IndexOf("truncate"!= -1)
    41            {
    42                intExeNum = DBHelper.ExecuteCommand(sql);
    43                lblExeNum.Text = "影响行数:<strong>" + intExeNum + "</strong>";
    44                grdSQL.Visible = false;
    45            }

    46        }

    47        catch (Exception ex)
    48        {
    49            ClientScript.RegisterStartupScript(typeof(string), """document.write(\"<h4 style=\'font-size:14px;color:#c00;padding-left:20px;\'>抱歉,系统发生了错误……错误信息:" + ex.Message.Replace("\"","'"+ "</h4>\")", true);
    50        }

    51    }

    52
    53    /// <summary>
    54    /// 执行按钮可用
    55    /// </summary>
    56    /// <param name="sender"></param>
    57    /// <param name="e"></param>

    58    protected void txtSQL_TextChanged(object sender, EventArgs e)
    59    {
    60        btnExeSql.Enabled = true;
    61    }

    62

     运行截图

     

     

     下载源码:点击下载(下载地址已修正)

  • 相关阅读:
    PAT (Advanced Level) 1060. Are They Equal (25)
    PAT (Advanced Level) 1059. Prime Factors (25)
    PAT (Advanced Level) 1058. A+B in Hogwarts (20)
    PAT (Advanced Level) 1057. Stack (30)
    PAT (Advanced Level) 1056. Mice and Rice (25)
    PAT (Advanced Level) 1055. The World's Richest (25)
    PAT (Advanced Level) 1054. The Dominant Color (20)
    PAT (Advanced Level) 1053. Path of Equal Weight (30)
    PAT (Advanced Level) 1052. Linked List Sorting (25)
    PAT (Advanced Level) 1051. Pop Sequence (25)
  • 原文地址:https://www.cnblogs.com/walkingp/p/1524645.html
Copyright © 2011-2022 走看看