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; width: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 ExpandedBlockStart.gifContractedBlock.gif < script  type ="text/javascript" >
27var Osel=document.aspnetForm;
28Osel.onsubmit=function()
29ExpandedSubBlockStart.gifContractedSubBlock.gif{
30    if(Osel.<%=txtSQL.ClientID %>.value=="")
31ExpandedSubBlockStart.gifContractedSubBlock.gif    {
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)
37ExpandedSubBlockStart.gifContractedSubBlock.gif    {
38        if(confirm("即将执行的操作带有一定的风险,是否继续?"))
39            return true;
40        else 
41            return false;
42    }

43}

44
45function Open(value) 
46ExpandedSubBlockStart.gifContractedSubBlock.gif
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 ExpandedBlockStart.gifContractedBlock.gif     {
 3        if (!IsPostBack)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 5            GetTableName();
 6        }

 7    }

 8
 9 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
10    /// 获取数据表结构
11    /// </summary>

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

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

25      protected   void  btnExeSql_Click( object  sender, EventArgs e)
26 ExpandedBlockStart.gifContractedBlock.gif     {
27        string sql = txtSQL.Text.Trim().ToLower();
28        int intExeNum;
29
30        try
31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
32            if (sql.Substring(06).IndexOf("select"!= -1)
33ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
41ExpandedSubBlockStart.gifContractedSubBlock.gif            {
42                intExeNum = DBHelper.ExecuteCommand(sql);
43                lblExeNum.Text = "影响行数:<strong>" + intExeNum + "</strong>";
44                grdSQL.Visible = false;
45            }

46        }

47        catch (Exception ex)
48ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
54    /// 执行按钮可用
55    /// </summary>
56    /// <param name="sender"></param>
57    /// <param name="e"></param>

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

62

 运行截图

 

 

 

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

转载于:https://www.cnblogs.com/walkingp/archive/2009/07/16/1524645.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值