ExcuteReader.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteReader.aspx.cs" Inherits="ExcuteReader" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" >
6 <head runat="server">
7 <title>ExcuteReader</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <table style ="414px">
13 <tr>
14 <td>
15 要执行的SQL:
16 </td>
17 </tr>
18 <tr>
19 <td>
20 <asp:TextBox ID="txtSql" runat ="server" Width="255px" Text ="Select * from Table"></asp:TextBox>
21 </td>
22 </tr>
23 <tr>
24 <td>
25 <asp:Button ID="btnExecuteReader" runat ="server" Text ="执行" OnClick="btnExecuteReader_Click" />
26 </td>
27 </tr>
28 <tr>
29 <td>
30 执行结果:
31 </td>
32 </tr>
33 <tr>
34 <td style="height: 151px">
35 <asp:DataList ID="dtst" runat ="server" Width ="400px" CellPadding="4" ForeColor="#333333">
36 <ItemTemplate >
37 <%-- 编号:<% Eval("au_id"); %>
38 <br />
39 姓:<% Eval("au_lname"); %><br />
40 名:<% Eval("au_fname"); %>--%>
41 编号:<%# Eval("au_id") %> | 姓名:<%# Eval ("au_lname") %>
42 </ItemTemplate>
43
44 <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
45 <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
46 <AlternatingItemStyle BackColor="White" />
47 <ItemStyle BackColor="#EFF3FB" />
48 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
49 </asp:DataList>
50 </td>
51 </tr>
52 </table>
53 </div>
54 </form>
55 </body>
56 </html>
57
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteReader.aspx.cs" Inherits="ExcuteReader" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml" >
6 <head runat="server">
7 <title>ExcuteReader</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <table style ="414px">
13 <tr>
14 <td>
15 要执行的SQL:
16 </td>
17 </tr>
18 <tr>
19 <td>
20 <asp:TextBox ID="txtSql" runat ="server" Width="255px" Text ="Select * from Table"></asp:TextBox>
21 </td>
22 </tr>
23 <tr>
24 <td>
25 <asp:Button ID="btnExecuteReader" runat ="server" Text ="执行" OnClick="btnExecuteReader_Click" />
26 </td>
27 </tr>
28 <tr>
29 <td>
30 执行结果:
31 </td>
32 </tr>
33 <tr>
34 <td style="height: 151px">
35 <asp:DataList ID="dtst" runat ="server" Width ="400px" CellPadding="4" ForeColor="#333333">
36 <ItemTemplate >
37 <%-- 编号:<% Eval("au_id"); %>
38 <br />
39 姓:<% Eval("au_lname"); %><br />
40 名:<% Eval("au_fname"); %>--%>
41 编号:<%# Eval("au_id") %> | 姓名:<%# Eval ("au_lname") %>
42 </ItemTemplate>
43
44 <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
45 <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
46 <AlternatingItemStyle BackColor="White" />
47 <ItemStyle BackColor="#EFF3FB" />
48 <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
49 </asp:DataList>
50 </td>
51 </tr>
52 </table>
53 </div>
54 </form>
55 </body>
56 </html>
57
ExcuteReader.apx.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 ExcuteReader : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnExecuteReader_Click(object sender, EventArgs e)
21 {
22 //获取要执行的命令
23 string sql = txtSql.Text;
24
25 using (SqlConnection conn = new SqlConnection (SqlHelper.ConnectionStringLocalTransaction ))
26 {
27 SqlDataReader myDr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, null);
28
29 //将结果赋予DataList,作为其数据源
30 dtst.DataSource = myDr;
31
32 //绑定数据源
33 dtst.DataBind();
34 }
35 }
36 }
37
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 ExcuteReader : System.Web.UI.Page
14 {
15 protected void Page_Load(object sender, EventArgs e)
16 {
17
18 }
19
20 protected void btnExecuteReader_Click(object sender, EventArgs e)
21 {
22 //获取要执行的命令
23 string sql = txtSql.Text;
24
25 using (SqlConnection conn = new SqlConnection (SqlHelper.ConnectionStringLocalTransaction ))
26 {
27 SqlDataReader myDr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql, null);
28
29 //将结果赋予DataList,作为其数据源
30 dtst.DataSource = myDr;
31
32 //绑定数据源
33 dtst.DataBind();
34 }
35 }
36 }
37