ExecuteSql.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteSQL.aspx.cs" Inherits="ExcuteSQL" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>ExecuteSql</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 534px; height: 143px">
12 <tr>
13 <td style=" 1665px">
14 要执行的sql语句(添加、更新、删除)
15 </td>
16 <tr>
17 <td style=" 308px">
18 <asp:TextBox ID="txtSql" runat="server" Width="502px" Text="添加、更新、删除"></asp:TextBox>
19 </td>
20 </tr>
21 </tr>
22 <tr>
23 <td style=" 1665px">
24 <asp:Button ID="btnSql" runat="server" Text="执行" OnClick="btnSql_Click" />
25 </td>
26 </tr>
27 </table>
28 </div>
29 </form>
30 </body>
31 </html>
32
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteSQL.aspx.cs" Inherits="ExcuteSQL" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>ExecuteSql</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 534px; height: 143px">
12 <tr>
13 <td style=" 1665px">
14 要执行的sql语句(添加、更新、删除)
15 </td>
16 <tr>
17 <td style=" 308px">
18 <asp:TextBox ID="txtSql" runat="server" Width="502px" Text="添加、更新、删除"></asp:TextBox>
19 </td>
20 </tr>
21 </tr>
22 <tr>
23 <td style=" 1665px">
24 <asp:Button ID="btnSql" runat="server" Text="执行" OnClick="btnSql_Click" />
25 </td>
26 </tr>
27 </table>
28 </div>
29 </form>
30 </body>
31 </html>
32
ExcuteSQL .aspx.cs(示例)
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class ExcuteSQL : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnSql_Click(object sender, EventArgs e)
21 {
22 //获取要执行的Sql
23 string sql = txtSql.Text;
24
25
26 //定义对象的资源保存范围,一旦using范围结束,将释放对方的所占资源
27 using (SqlConnection conn = new SqlConnection(SqlHelper .ConnectionStringLocalTransaction ))
28 {
29 //打开连接
30 conn.Open();
31 //调用执行命令,最好一项没有参数,设置为null
32 SqlHelper.ExecuteNonQuery(conn ,CommandType .Text ,sql,null);
33
34 Response.Write("<font color='red'>操作成功。</Font>");
35 }
36 }
37 }
38
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Collections;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;
12
13 public partial class ExcuteSQL : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnSql_Click(object sender, EventArgs e)
21 {
22 //获取要执行的Sql
23 string sql = txtSql.Text;
24
25
26 //定义对象的资源保存范围,一旦using范围结束,将释放对方的所占资源
27 using (SqlConnection conn = new SqlConnection(SqlHelper .ConnectionStringLocalTransaction ))
28 {
29 //打开连接
30 conn.Open();
31 //调用执行命令,最好一项没有参数,设置为null
32 SqlHelper.ExecuteNonQuery(conn ,CommandType .Text ,sql,null);
33
34 Response.Write("<font color='red'>操作成功。</Font>");
35 }
36 }
37 }
38