Asp.net 在網頁編寫C#代碼示例-- 一個簡單的web MsSql 命令執行環境


在給一個客戶做的系統上,因為要對數據庫進行查看,但之前都是用TeamView來連接到客戶的服務器進行數據庫操作的
但最近客戶那邊的TeamView好像更改過密碼導致我無法正常連接,而巧了客戶的網官因為有事沒有上班所以也法獲取新的密碼。
因為業務原因急需查看數據庫,所以就寫了一個簡單的SQl命令並部署到客戶的服務器來通過Web執行Sql命令
將ConnectonString更改為自己的數據庫連接並保存為**.aspx即可
<!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>

 


有圖有真相:
 
為了安全,將下面的配置添加到web.config文件並在服務品上添加相應的用戶名和密碼用於訪問該文件
如果要訪問mssql.aspx文件則必須要用”WWW_mssql“ 賬號進行登陸否則無法訪問
<locationpath="mssql.aspx">
<system.web>
<authorization>
<allowusers=".\WWW_mssql"/>
<denyusers="*"/>
</authorization>
</system.web>
</location>
如果任務完成請將該文件刪除,防止出現安全問題
 
至於為何要在asp.net頁面中直接編寫C#代碼?主要是簡單,只是一個文件而已 不需要重新編譯Dll 且不會對現有的系統有任何影響。
 
參考(如何在Asp.net頁面中直接編寫C#代碼 ):






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM