简易数据库查询系统

看了刘振岩先生编著的《基于.NET的Web程序设计》,摘录简易数据库查询系统的实现代码。具体功能是:用户
可以选取SQL Server数据库服务器的某个数据库中的某个数据表(通过两个DropDownList控件),然后将此数据表中的所有数据显示出来(通过DataGrid控件)。

amespace accessDataBase
{
 ///


 /// useDataBind 的摘要说明。
 ///

 public class useDataBind : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.DropDownList DropDownList2;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Label Label1;
  public SqlConnection myConnection;
  public string strSQL;
  public SqlDataAdapter myCommand;
  public DataSet ds=new DataSet();
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    Label1.Text="";
    try
    {
     DropDownList1_content();
     DropDownList2_content();
    }
    catch
    {
     Response.Write("");
    }
   }


  }
  protected void DropDownList1_content()
  {
   myConnection=new SqlConnection("server=ZHB//SQLSERVER;User ID=sa;Pwd=sa;database=master");
   myConnection.Open();
   strSQL="select name from sysdatabases where dbid>=5 order by dbid desc";
   myCommand=new SqlDataAdapter(strSQL,myConnection);
   myCommand.Fill(ds,"database");
   DropDownList1.DataSource=ds.Tables["database"].DefaultView;
   DropDownList1.DataValueField=ds.Tables["database"].Columns[0].ColumnName;
   DropDownList1.DataTextField=ds.Tables["database"].Columns[0].ColumnName;
   DropDownList1.DataBind();
  }
  protected void DropDownList2_content()
  {
 
   myConnection=new SqlConnection("server=ZHB//SQLSERVER;User ID=sa;Pwd=sa;database="+DropDownList1.SelectedItem.Value.ToString()+"");
   myConnection.Open();
   strSQL="select name from sysobjects where xtype='u' order by crdate desc";
   myCommand=new SqlDataAdapter(strSQL,myConnection);
   myCommand.Fill(ds,"datatable");
   if(ds.Tables["datatable"].Rows.Count!=0)
   {
    DropDownList2.Visible=true;
    DropDownList2.DataSource=ds.Tables["datatable"].DefaultView;
    DropDownList2.DataValueField=ds.Tables["datatable"].Columns[0].ColumnName;
    DropDownList2.DataTextField=ds.Tables["datatable"].Columns[0].ColumnName;
    DropDownList2.DataBind();

   }
   else
   {
    DropDownList2.Visible=false;
    Label1.Text=DropDownList1.SelectedItem.Value+"数据库中无用户数据表!";

   }

  }
  protected void DataGrid1_content()
  {
   myConnection=new SqlConnection("server=ZHB//SQLSERVER;User ID=sa;Pwd=sa;database="+DropDownList1.SelectedItem.Value.ToString()+"");
   myConnection.Open();
   strSQL="select * from "+DropDownList2.SelectedItem.Value.ToString();
   myCommand=new SqlDataAdapter(strSQL,myConnection);
   myCommand.Fill(ds,"data");
   Label1.Text=DropDownList1.SelectedItem.Value+"数据库中的数据表"+DropDownList2.SelectedItem.Value;
   if(ds.Tables["data"].Rows.Count!=0)
   {
    Label1.Text+="所有数据如下:";
    DataGrid1.Visible=true;
    DataGrid1.DataSource=ds.Tables["data"].DefaultView;
    DataGrid1.DataBind();

   }
   else
   {
    DataGrid1.Visible=false;
    Label1.Text="无数据!";

   }

 }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {   
   this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.DropDownList2.SelectedIndexChanged += new System.EventHandler(this.DropDownList2_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DropDownList2_content();
   if(ds.Tables["datatable"].Rows.Count!=0)
   {
    DataGrid1_content();
   }
   else
   {
    Label1.Text=DropDownList1.SelectedItem.Value+"数据库中无用户数据表!";
   }
 }

  private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DataGrid1_content();
  }
 }
}
 

你可能感兴趣的:(.NET)