做项目时写的数据库操作类。SqlHelper.cs(四)
ExecuteScalar.aspx(示例)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteScalar.aspx.cs" Inherits="ExcuteScalar" %>
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>ExecuteScalar</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style="width: 431px">
12 <tr>
13 <td style="width: 180px">
14 要执行的SQL:</td>
15 </tr>
16 <tr>
17 <td style="width: 180px">
18 <asp:TextBox ID="txtsql" runat="server" Width="283px" Text="select name from Table where id=2"></asp:TextBox>
19 </td>
20 </tr>
21 <tr>
22 <td style="width: 180px">
23 <asp:Button ID="btnExecute" runat="server" Text="执行" OnClick="btnExecute_Click" />
24 </td>
25 </tr>
26 <tr>
27 <td style="width: 180px">
28 返回的结果是:</td>
29 </tr>
30 <tr>
31 <td style="width: 180px">
32 <asp:TextBox ID="txtScalar" runat="server"></asp:TextBox>
33 </td>
34 </tr>
35 </table>
36 </div>
37 </form>
38 </body>
39 </html>
40
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExcuteScalar.aspx.cs" Inherits="ExcuteScalar" %>
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>ExecuteScalar</title>
7 </head>
8 <body>
9 <form id="form1" runat="server">
10 <div>
11 <table style="width: 431px">
12 <tr>
13 <td style="width: 180px">
14 要执行的SQL:</td>
15 </tr>
16 <tr>
17 <td style="width: 180px">
18 <asp:TextBox ID="txtsql" runat="server" Width="283px" Text="select name from Table where id=2"></asp:TextBox>
19 </td>
20 </tr>
21 <tr>
22 <td style="width: 180px">
23 <asp:Button ID="btnExecute" runat="server" Text="执行" OnClick="btnExecute_Click" />
24 </td>
25 </tr>
26 <tr>
27 <td style="width: 180px">
28 返回的结果是:</td>
29 </tr>
30 <tr>
31 <td style="width: 180px">
32 <asp:TextBox ID="txtScalar" runat="server"></asp:TextBox>
33 </td>
34 </tr>
35 </table>
36 </div>
37 </form>
38 </body>
39 </html>
40
ExecuteScalar.aspx.cs(示例)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ExcuteScalar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExecute_Click(object sender, EventArgs e)
{
//获取要执行的命令
string sql = txtsql.Text;
using (SqlConnection conn = new SqlConnection (SqlHelper .ConnectionStringLocalTransaction ))
{
//打开连接
conn.Open();
//返回结果为object类型
object myObject = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, null);
//显示执行结果
txtScalar.Text = myObject.ToString();
Response.Write("<font color=red>操作完成!请检查数据库!</font>");
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ExcuteScalar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExecute_Click(object sender, EventArgs e)
{
//获取要执行的命令
string sql = txtsql.Text;
using (SqlConnection conn = new SqlConnection (SqlHelper .ConnectionStringLocalTransaction ))
{
//打开连接
conn.Open();
//返回结果为object类型
object myObject = SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, null);
//显示执行结果
txtScalar.Text = myObject.ToString();
Response.Write("<font color=red>操作完成!请检查数据库!</font>");
}
}
}