📄 databaseobjectexplore.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
namespace TreeReportApp
{
/// <summary>
/// DatabaseObjectExplore 的摘要说明。
/// </summary>
public class DatabaseObjectExplore : System.Windows.Forms.Form
{
public string objectname="";
public string objecttype="";
private System.Windows.Forms.Panel panel1;
public System.Windows.Forms.ListView listView_database;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Splitter splitter2;
private System.Windows.Forms.ListView listView_objects;
private System.Windows.Forms.Button button_ok;
private System.Windows.Forms.Button button_cancel;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DatabaseObjectExplore()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DatabaseObjectExplore));
this.panel1 = new System.Windows.Forms.Panel();
this.button_ok = new System.Windows.Forms.Button();
this.button_cancel = new System.Windows.Forms.Button();
this.listView_database = new System.Windows.Forms.ListView();
this.splitter1 = new System.Windows.Forms.Splitter();
this.splitter2 = new System.Windows.Forms.Splitter();
this.listView_objects = new System.Windows.Forms.ListView();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.button_ok);
this.panel1.Controls.Add(this.button_cancel);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(0, 309);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(560, 48);
this.panel1.TabIndex = 0;
//
// button_ok
//
this.button_ok.Image = ((System.Drawing.Image)(resources.GetObject("button_ok.Image")));
this.button_ok.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button_ok.Location = new System.Drawing.Point(121, 12);
this.button_ok.Name = "button_ok";
this.button_ok.Size = new System.Drawing.Size(96, 24);
this.button_ok.TabIndex = 0;
this.button_ok.Text = "确 认";
this.button_ok.Click += new System.EventHandler(this.button_ok_Click);
//
// button_cancel
//
this.button_cancel.Image = ((System.Drawing.Image)(resources.GetObject("button_cancel.Image")));
this.button_cancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button_cancel.Location = new System.Drawing.Point(337, 12);
this.button_cancel.Name = "button_cancel";
this.button_cancel.Size = new System.Drawing.Size(96, 24);
this.button_cancel.TabIndex = 0;
this.button_cancel.Text = "取 消";
this.button_cancel.Click += new System.EventHandler(this.button_cancel_Click);
//
// listView_database
//
this.listView_database.Dock = System.Windows.Forms.DockStyle.Left;
this.listView_database.HideSelection = false;
this.listView_database.Location = new System.Drawing.Point(0, 0);
this.listView_database.MultiSelect = false;
this.listView_database.Name = "listView_database";
this.listView_database.Size = new System.Drawing.Size(168, 309);
this.listView_database.TabIndex = 4;
this.listView_database.SelectedIndexChanged += new System.EventHandler(this.listView_database_SelectedIndexChanged);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(168, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 309);
this.splitter1.TabIndex = 5;
this.splitter1.TabStop = false;
//
// splitter2
//
this.splitter2.Location = new System.Drawing.Point(171, 0);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(3, 309);
this.splitter2.TabIndex = 6;
this.splitter2.TabStop = false;
//
// listView_objects
//
this.listView_objects.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView_objects.Location = new System.Drawing.Point(174, 0);
this.listView_objects.MultiSelect = false;
this.listView_objects.Name = "listView_objects";
this.listView_objects.Size = new System.Drawing.Size(386, 309);
this.listView_objects.TabIndex = 7;
this.listView_objects.DoubleClick += new System.EventHandler(this.listView_objects_DoubleClick);
//
// DatabaseObjectExplore
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(560, 357);
this.Controls.Add(this.listView_objects);
this.Controls.Add(this.splitter2);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.listView_database);
this.Controls.Add(this.panel1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "DatabaseObjectExplore";
this.ShowInTaskbar = false;
this.Text = "数据库表或视图浏览";
this.Load += new System.EventHandler(this.DatabaseObjectExplore_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void DatabaseObjectExplore_Load(object sender, System.EventArgs e)
{
listView_database.Columns.Add("数据库列表",300,HorizontalAlignment.Left);
listView_database.View = View.Details;
listView_objects.Columns.Add("数据库对象名",200,HorizontalAlignment.Left);
listView_objects.Columns.Add("拥有者",150,HorizontalAlignment.Left);
listView_objects.Columns.Add("类型",80,HorizontalAlignment.Left);
listView_objects.Columns.Add("数据库",80,HorizontalAlignment.Left);
OleDbConnection conn = new OleDbConnection(TreeConnection.connstr);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT name FROM master.dbo.sysdatabases WHERE dbid>4 order by dbid desc",conn);
//OleDbCommand cmd = new OleDbCommand("sp_databases",conn);
OleDbDataReader rd = cmd.ExecuteReader();
ListViewItem item=null;
while(rd.Read())
{
item = listView_database.Items.Add(""+rd["name"],1);
}
rd.Close();
conn.Close();
if (item != null)
item.Selected = true;
}
private void listView_database_SelectedIndexChanged(object sender, System.EventArgs e)
{
listView_objects.Items.Clear();
listView_objects.View = View.Details;
if(listView_database.SelectedItems.Count==0)
return;
OleDbConnection conn = new OleDbConnection(TreeConnection.connstr);
conn.Open();
try
{
ListViewItem dbitem = listView_database.SelectedItems[0];
OleDbCommand cmd = new OleDbCommand("SELECT TABLE_CATALOG AS dbname,TABLE_SCHEMA AS owername,TABLE_NAME AS tablename,TABLE_TYPE as tabletype FROM "+ dbitem.Text+ ".INFORMATION_SCHEMA.TABLES",conn);
//OleDbCommand cmd = new OleDbCommand("sp_tables null,null,TreeDB",conn);
OleDbDataReader rd = cmd.ExecuteReader();
while(rd.Read())
{
ListViewItem item = listView_objects.Items.Add(""+rd["tablename"],2);
item.SubItems.Add(""+rd["owername"]);
item.SubItems.Add(""+rd["tabletype"]);
item.SubItems.Add(""+rd["dbname"]);
}
rd.Close();
}
catch (Exception er)
{
MessageBox.Show(this,er.Message);
}
conn.Close();
}
private void listView_objects_DoubleClick(object sender, System.EventArgs e)
{
if(listView_objects.SelectedItems.Count==0)
return;
ListViewItem item = listView_objects.SelectedItems[0];
objectname= ""+item.SubItems[3].Text+"."+item.SubItems[1].Text+"."+item.Text;
if ("BASE TABLE".Equals(item.SubItems[2].Text))
objecttype="table";
else
objecttype="view";
this.DialogResult = DialogResult.OK;
this.Close();
}
private void button_ok_Click(object sender, System.EventArgs e)
{
if(listView_objects.SelectedItems.Count==0)
return;
ListViewItem item = listView_objects.SelectedItems[0];
objectname= ""+item.SubItems[3].Text+"."+item.SubItems[1].Text+"."+item.Text;
if ("BASE TABLE".Equals(item.SubItems[2].Text))
objecttype="table";
else
objecttype="view";
this.DialogResult = DialogResult.OK;
this.Close();
}
private void button_cancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -