最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net Web页执行SQL程序代码
时间:2022-07-02 10:50:07 编辑:袖梨 来源:一聚教程网
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageSQL.aspx.cs" Inherits="manage_ManageSQL" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml" >
网页执行SQL语句程序manageSql.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;
///
/// Author:walkingp
/// Web Site:http://www.51obj.cn/
/// E-mail:[email protected]
///
public partial class manage_ManageSQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTableName();
}
}
#region SqlConnection
///
/// 初始化SqlConnection
///
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = "server=.;DataBase=model;uid=sa;pwd=;";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
///
/// 执行Sql
///
/// Sql
///
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
///
/// 执行Sql(overload)
///
/// Sql
/// SqlParameter
///
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
///
/// 获取DataTable
///
/// Sql
///
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
#endregion
///
/// 获取数据表结构
///
protected void GetTableName()
{
DataTable dt = Connection.GetSchema("Tables", null);
Connection.Close();
grdTable.DataSource = dt;
grdTable.DataBind();
}
///
/// 执行操作
///
///
///
protected void btnExeSql_Click(object sender, EventArgs e)
{
string sql = txtSQL.Text.Trim().ToLower();
int intExeNum;
try
{
if (sql.Substring(0, 6).IndexOf("select") != -1)
{
DataTable dt = GetDataSet(sql);
grdSQL.DataSource = dt;
grdSQL.DataBind();
lblExeNum.Text = "返回记录条数:" + dt.Rows.Count + "";
grdSQL.Visible = true;
}
else if (sql.Substring(0, 6).IndexOf("delete") != -1 || sql.Substring(0, 6).IndexOf("update") != -1 || sql.Substring(0, 8).IndexOf("truncate") != -1)
{
intExeNum = ExecuteCommand(sql);
lblExeNum.Text = "影响行数:" + intExeNum + "";
grdSQL.Visible = false;
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(typeof(string), "", "document.write("
抱歉,系统发生了错误……错误信息:" + ex.Message.Replace(""", "'") + "
")", true);}
}
///
/// 执行按钮可用
///
///
///
protected void txtSQL_TextChanged(object sender, EventArgs e)
{
btnExeSql.Enabled = true;
}
}
相关文章
- 时空中的绘旅人天宇之间怎么玩 绘旅人天宇之间活动玩法介绍 12-25
- QQ2024年度报告怎么看 2024qq年度报告玩法介绍 12-25
- 归龙潮珠砂什么时候up 归龙潮红缘绮梦卡池介绍 12-25
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25