做项目时写的数据库操作类。SqlHelper.cs(五)

本文提供了一个ASP.NET网页示例,演示如何通过C#代码实现SQL语句的执行,包括添加、更新和删除操作。用户可以在界面上输入SQL语句并点击按钮执行。
ContractedBlock.gifExpandedBlockStart.gifExecuteSql.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="width: 534px; height: 143px">
12                 <tr>
13                     <td style="width: 1665px">
14                         要执行的sql语句(添加、更新、删除)
15                     </td>
16                     <tr>
17                         <td style="width: 308px">
18                             <asp:TextBox ID="txtSql" runat="server" Width="502px" Text="添加、更新、删除"></asp:TextBox>
19                         </td>
20                     </tr>
21                 </tr>
22                 <tr>
23                     <td style="width: 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 

 

 

 

 

 

 

 

ContractedBlock.gifExpandedBlockStart.gifExcuteSQL .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 

 

转载于:https://www.cnblogs.com/daiweixm/archive/2009/07/12/1522146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值