czhtr 发表于 2015-7-2 02:24:03

一个简单的在线 SQL Server 管理工具

  
  
  几个月前因为虚拟主机服务商提供的在线 SQL Sever 管理工具总是出错,我就自己写了这个在线 SQL 管理小工具,但是因为是自己用,写得很简单。因此这个小工具是非常不完善的。使用它需要你具备一定的写 SQL语句的 能力。
  希望它能为您带来一点点便利,或者能给你增加一点点知识。

  本文作者:willmove
作者主页:http://www.amuhouse.com

页面代码如下:







    Query Database
   
   



   
      
            
               
                  
                        
                           
                              首页
                              
                                    查看数据库
                                        查看用户表
                              
                                    查看存储过程
                                        执行SQL语句
                        
                  
               
               
                  
                        
                           
                           
                              
                                    
                                       
                                          没有数据
                                       
                                       
                                          
                                          
                                          
                                          
                                       
                                    
                                    
                                    
                              
                              
                                    
                                    
                                       
                                          
                                       
                                    
                                    
                                    
                              
                              
                                    
                                    
                              
                              
                              
                              
                              
                              
                                    
                                       
                                    
                              
                               
                              
                              
                              
                              
                                 
                               
                              
                               
                                    
                                 
                                    
                                    
                              
                              
                               
                               
                           
                        
                  
               
   


  后台代码如下:


using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class Query : System.Web.UI.Page
{
    public string myConnectionString = ConfigurationManager.ConnectionStrings["classifiedsConnection"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
      //Response.Write(User.Identity.Name);
    }
    protected void ShowStoredProcedureLinkButton_Click(object sender, EventArgs e)
    {
      SetActivePanel(ViewStoredProcedurePanel);
    }
    protected void ShowTablesLinkButton_Click(object sender, EventArgs e)
    {
      SetActivePanel(ViewTablesPanel);
    }
    protected void RunSqlLinkButton_Click(object sender, EventArgs e)
    {
      SetActivePanel(RunSqlPanel);
    }
    protected void ShowDatabaseLinkButton_Click(object sender, EventArgs e)
    {
      //DataBaseGridView.Visible = true;
      SetActivePanel(ViewDatabasePanel);
    }
    protected void QueryButton_Click(object sender, EventArgs e)
    {
      this.MessageLabel.Text = string.Empty;

      string sql = this.SqlTextBox.Text;
      if (amuhouseRadioButton.Checked)
      {
            myConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
      }

      SqlConnection conn = new SqlConnection(myConnectionString);
      SqlCommand cmd = new SqlCommand(sql, conn);
      conn.Open();
      if (this.QueryCheckBox.Checked)
      {
            try
            {
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                  this.MessageLabel.Text += dr.ToString() + "\t|\t";
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                this.MessageLabel.Text = ex.Message;
            }
      }
      else
      {
            try
            {
                cmd.ExecuteNonQuery();
                this.MessageLabel.Text = "Succeed!";
            }
            catch (SqlException ex)
            {
                this.MessageLabel.Text = ex.Message;
            }
      }
      conn.Close();
    }
    protected void GetDataButton_Click(object sender, EventArgs e)
    {
      if (amuhouseRadioButton.Checked)
      {
            myConnectionString =   ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;
      }

      this.MessageLabel.Text = string.Empty;

      string sql = this.SqlTextBox.Text;
      SqlConnection conn = new SqlConnection(myConnectionString);
      SqlDataAdapter da = new SqlDataAdapter(sql, conn);
      DataSet ds = new DataSet();
      conn.Open();
      try
      {
            da.Fill(ds, "table");
            SqlDataGridView.DataSource = ds;
            SqlDataGridView.DataBind();
      }
      catch (SqlException ex)
      {
            MessageLabel.Text = ex.Message;
      }

      conn.Close();
    }
    protected void SetActivePanel(Panel panel)
    {
      if (panel == null)
            return;
      //EmailPanel.Visible = false;
      ViewDatabasePanel.Visible = false;
      ViewStoredProcedurePanel.Visible = false;
      ViewTablesPanel.Visible = false;
      RunSqlPanel.Visible = false;

      panel.Visible = true;
      Page.SetFocus(panel);
    }
   
}
页: [1]
查看完整版本: 一个简单的在线 SQL Server 管理工具