ExecuteSqlParm.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteSQLParm.aspx.cs" Inherits="ExcuteSQLParm" %>
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>执行带参数的SQL</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 470px; height: 159px">
12 <tr>
13 <td>
14 参数名:<asp:TextBox ID="txtparm" runat="server" Width="256px" Text="id"></asp:TextBox>
15 </td>
16 </tr>
17 <tr>
18 <td>
19 参数值:<asp:TextBox ID="txtvalue" runat="server" Width="252px" Text="1"></asp:TextBox>
20 </td>
21 </tr>
22 <tr>
23 <td>
24 要执行的SQL:
25 </td>
26 </tr>
27 <tr>
28 <td>
29 <asp:TextBox ID="txtsql" runat="server" Width="442px" Text="update Table set name='daiwei' where id=@id"></asp:TextBox>
30 </td>
31 </tr>
32 <tr>
33 <td>
34 <asp:Button ID="btnExecute" runat="server" Text="执行" OnClick="btnExecute_Click" />
35 </td>
36 </tr>
37 </table>
38 </div>
39 </form>
40 </body>
41 </html>
42
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteSQLParm.aspx.cs" Inherits="ExcuteSQLParm" %>
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>执行带参数的SQL</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style=" 470px; height: 159px">
12 <tr>
13 <td>
14 参数名:<asp:TextBox ID="txtparm" runat="server" Width="256px" Text="id"></asp:TextBox>
15 </td>
16 </tr>
17 <tr>
18 <td>
19 参数值:<asp:TextBox ID="txtvalue" runat="server" Width="252px" Text="1"></asp:TextBox>
20 </td>
21 </tr>
22 <tr>
23 <td>
24 要执行的SQL:
25 </td>
26 </tr>
27 <tr>
28 <td>
29 <asp:TextBox ID="txtsql" runat="server" Width="442px" Text="update Table set name='daiwei' where id=@id"></asp:TextBox>
30 </td>
31 </tr>
32 <tr>
33 <td>
34 <asp:Button ID="btnExecute" runat="server" Text="执行" OnClick="btnExecute_Click" />
35 </td>
36 </tr>
37 </table>
38 </div>
39 </form>
40 </body>
41 </html>
42
ExcuteSQLParm .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 ExcuteSQLParm : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19 protected void btnExecute_Click(object sender, EventArgs e)
20 {
21 //初始化参数
22 SqlParameter myParm = new SqlParameter();
23
24 //获取参数的名字
25 myParm.ParameterName = txtparm.Text;
26
27 //设置变量的类型和长度
28 myParm.SqlDbType = SqlDbType.NVarChar;
29 myParm.Size = 20;
30
31 //获取参数的值
32 myParm.Value = txtparm.Text;
33
34 string sql = txtsql.Text ;
35
36 using (SqlConnection conn = new SqlConnection (SqlHelper .ConnectionStringLocalTransaction ))
37 {
38 //打开连接
39 conn.Open();
40
41 //执行方法
42 SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, myParm);
43 Response.Write("<font color=red>操作完成!请检查数据库!</font>");
44 }
45 }
46 }
47
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 ExcuteSQLParm : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19 protected void btnExecute_Click(object sender, EventArgs e)
20 {
21 //初始化参数
22 SqlParameter myParm = new SqlParameter();
23
24 //获取参数的名字
25 myParm.ParameterName = txtparm.Text;
26
27 //设置变量的类型和长度
28 myParm.SqlDbType = SqlDbType.NVarChar;
29 myParm.Size = 20;
30
31 //获取参数的值
32 myParm.Value = txtparm.Text;
33
34 string sql = txtsql.Text ;
35
36 using (SqlConnection conn = new SqlConnection (SqlHelper .ConnectionStringLocalTransaction ))
37 {
38 //打开连接
39 conn.Open();
40
41 //执行方法
42 SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, myParm);
43 Response.Write("<font color=red>操作完成!请检查数据库!</font>");
44 }
45 }
46 }
47