Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>exec mssql command-HTL</title> </head> <%@ Page Language="C#" enableViewState="true" %> <%@ Import namespace="System" %> <%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.SqlClient" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { if(txt_sql.Value.Length>0){ string _ConnectionString=System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; if(string.IsNullOrEmpty(_ConnectionString)) { Response.Write("ConnectionString is null<br>"); Response.End(); } Response.Write("Sql Command:<br>"+txt_sql.Value); using (SqlConnection connection = new SqlConnection(_ConnectionString)) { using (SqlCommand cmd = new SqlCommand()) { try{ cmd.Connection = connection; cmd.CommandText = txt_sql.Value; cmd.Connection.Open(); // exec select if(txt_sql.Value.ToString().ToLower().Contains("select ")) { using(SqlDataAdapter sda=new SqlDataAdapter(cmd)) { DataTable dtable = new DataTable(); sda.Fill(dtable); GridView1.DataSource=dtable; GridView1.DataBind(); } } //exec update,insert,delete,other else cmd.ExecuteNonQuery(); Response.Write("<br>sql Command Success"); } catch (Exception e1) { Response.Write(e1.Message); } finally{ connection.Close(); } }//end using sqlcommand }//end using SqlConnection }//end if }//end Button1_Click </script> <body> <center> <h1 style="color:red;">at before executing Sql command , please backup database </h1> </center> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" ></asp:GridView><br> <textarea id="txt_sql" runat="server" style="width:80%;height:200px;"></textarea> <br> <asp:Button ID="btnAdd" runat="server" Text="Exec Sql" OnClick="Button1_Click" /> </form> </body> </html>
<locationpath="mssql.aspx"> <system.web> <authorization> <allowusers=".\WWW_mssql"/> <denyusers="*"/> </authorization> </system.web> </location>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。