Using system.drawing; using system.Collections; using system.bomponentmodel; using system.data; using system.data .sqlclient;
Namespace database management Automation {//
Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
/// summary> private void InitializeComponent () {this.tabList = new System.Windows.Forms.ComboBox (); this.tabPty = new System.Windows.Forms.ListView (); this.columnHeader1 = new System. Windows.Forms.ColumnHeader (); this.columnHeader2 = new System.Windows.Forms.ColumnHeader (); this.columnHeader3 = new System.Windows.Forms.ColumnHeader (); this.columnHeader6 = new System.Windows.Forms.ColumnHeader (); this.columnHeader4 = new System.Windows.Forms.ColumnHeader (); this.columnHeader5 = new System.Windows.Forms.ColumnHeader (); this.panel1 = new System.Windows.Forms.Panel (); this. IDSTR = new system.windows.forms.textbox (); this.qry = new system.windows.Forms.Button (); this.button1 = new system.windows.Forms.Button (); this.sqlConnection1 = new system. Data.SqlClient.SqlConnection (); this.listView1 = new System.Windows.Forms.ListView (); this.columnHeader7 = new System.Windows.Forms.ColumnHeader (); this.columnHeader8 = new System.Windows.Forms.ColumnHeader (); T his.ColID = new System.Windows.Forms.ColumnHeader (); this.listView2 = new System.Windows.Forms.ListView (); this.columnHeader9 = new System.Windows.Forms.ColumnHeader (); this.columnHeader10 = new System.Windows.Forms.ColumnHeader (); this.columnHeader11 = new System.Windows.Forms.ColumnHeader (); this.button2 = new System.Windows.Forms.Button (); this.panel1.SuspendLayout (); this. SuspendLayout (); // // Tablist // this.tablist.Anchor = ((System.Windows.Forms.Anchorstyles) (((System.Windows.Forms.Anchorstyles.top | System.Windows.Forms.Anchorstyles.Left) | System.windows.Forms.Anchorstyles.right)))))))))))))))))))))))
This.tablist.location = new system.drawing.point (8, 8); this.tablist.name = "TabList"; this.tablist.size = new system.drawing.size (512, 20); this.tablist. TabIndex = 0; this.tabList.SelectedIndexChanged = new System.EventHandler (this.tabList_SelectedValueChanged); // // tabPty // this.tabPty.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System. Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.tabPty.Columns.AddRange (new System.Windows.Forms.ColumnHeader [] {this.columnHeader1, this.columnHeader2, this.columnHeader3, this.columnHeader6, this.columnHeader4, this.columnHeader5}); this.tabPty.GridLines = true; this.tabPty. Location = new system.drawing.point (8, 32); this.tabpty.name = "tabpty"; this.tabpty.size = New system.drawing.size (176, 208); this.tabpty.tabindex = 1; this.tabpty.view = system.windows.forms.view.details; /// ColumnHeader1 // this.columnheader1.text = " Column name "; // // columnhead2 // this.columnheader2.text =" data type "; this.columnheader2.width = 48; /// columnheader3 // this.columnheader3.text =" column width "; ColumnHeader3.Width = 36; // // columnheader6 // this.columnheader6.text = "colid"; // // columnheader4 // this.columnheader4.text = "can be empty"; /// ColumnHeader5 // / c .columnheader5.text = "Is the primary key";
///////////////////////////Ww.windows.forms.Anchorstyles ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.Left) | System.Windows .Form.Anchorstyles.right))); this.Panel1.controls.add (this.button2); this.Panel1.controls.add (this.button1); this.panel1.controls.add (this.qry); this.q .panel1.controls.add (this.idstr); this.Panel1.Location = new system.drawing.point (8, 248); this.Panel1.name = "Panel1"; this.pal1.size = new system.drawing .Size (512, 80); this.Panel1.tabindex = 2; //// ipStr // this.idstr.Location = new system.drawing.point (16, 8); this.idstr.name = "idstr" This.idstr.readonly = true; this.idstr.size = new system.drawing.size (128, 21); this.idstr.TabINDEX = 0; this.idstr.Text = "textbox1"; // // qry // this.qry.location = new system.drawing.point (152, 8); this.qry.name = "QRY"; this.qry.size = new system.drawing.size (72, 24); this. QRY.TabINDEX = 1; this.qry.text = "Query"; this.QRY.Click = new system.eventhandler (this.qry_click); // // button1 // this.button1.location = new system.drawing.point (16, 48); this.button1. Name = "button1"; this.button1.size = new system.drawing.size (208, 24); this.button1.tabindex = 2; this.button1.text = "associated table (as a parent table)"; button1.Click = new System.EventHandler (this.button1_Click); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id = DHZ; packet size = 4096; integrated security = SSPI; data source = DHZ; pers " "
ist security info = False; initial catalog = SXQGZA "; // // listView1 // this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Top | System.Windows .Forms.AnchorStyles.Right))); this.listView1.Columns.AddRange (new System.Windows.Forms.ColumnHeader [] {this.columnHeader7, this.columnHeader8, this.ColID}); this.listView1.Location = new System.drawing.point (208, 32); this.listview1.name = "listview1"; this.listview1.size = new system.drawing.size (312, 184); this.listview1.tabindex = 3; this.listView1 .View = system.windows.forms.view.details; // // ColumnHeader7 // this.columnheader7.text = "colid"; // // columnheader8 // this.columnheader8.text = "column name"; // // colid // this.colid.text = "colid2"; // // listview2 // this.listview2.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.top | System.windows.f orms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); this.listView2.Columns.AddRange (new System.Windows.Forms.ColumnHeader [] {this.columnHeader9, this.columnHeader10, this.columnHeader11 }); This.listview2.location = new system.drawing.point (208, 216); this.listview2.name = "listview2"; this.listview2.size = new system.drawing.size (312, 24); this .listview2.tabindex = 4; this.listview2.view = system.windows.forms.view.details; // // columnheader9 // this.columnheader9.text = "colid";
// // columnheader10 // this.columnheader10.text = "Table name"; //// Columnheader11 // this.columnheader11.text = "coliD2"; // // button2 // this.button2.anchor = System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms. Anchorstyles.right))))))))); this.Button2.Location = new system.drawing.point (264, 8); this.button2.name = "button2"; this.button2.size = new system.drawing.size (240, 64); this.button2.tabindex = 3; this.button2.text = "View the corresponding field"; this.button2.click = new system.eventhandler (this.button2_click); //// Form1 // this. AutoScaleBasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (528, 333); this.controls.add (this.listview1); this.controls.add (this.Panel1 This.controls.add (this.tabpty); this.controls.add (this.tablist); this.controls.add (this.listview2); t His.name = "form1"; this.text = "form1"; this.load = new system.eventhandler (this.form1_load); this.Panel1.ResumeLayout (false); this.ResumeLayout (false);} #ENDREGION
///
private void Form1_Load (object sender, System.EventArgs e) {SqlCommand cmd = new SqlCommand ( "select id, name from sysobjects where xtype = 'u'", this.sqlConnection1); SqlDataAdapter da = new SqlDataAdapter (); da.SelectCommand = CMD; DataSet DS = New Dataset ("mgmt"); DA. Fill (DS, "Tabid"); this.tablist.dataSource = DS; this.tablist.Valuemember = "tabid.id"; this.tablist.displaymember = "Tabid.Name";} private void Tablist_selectedValueChanged (Object sender, system.eventargs e) {this.idstr.text = this.tablist.selectedValue.toTString ();
}
Private void QRY_CLICK (Object Sender, System.EventArgs E) {string spc = this.idstr.text; string cmdstr = "SELECT A.NAME, B.NAME AS TYPE, A.LENGTH, A. INULLABLE, A.COLID, A .status "; cmdstr =" from syscolumns a, systypes b "; cmdstr =" where a.id=@id and b.xusertype = a.xtype "; sqlcommand cmd = new SQLCommand (cmdstr, this.sqlconnection1); cmd. Parameters.Add ("@ ID", SPC); this.tabpty.Items.clear ();
THIS.SQLCONNECTION1.CLOSE (); this.sqlconnection1.open (); sqldatareader DR = cmd.executeReader (); while (Dr.Read ()) {string colna = DR ["name"]. Tostring (); // Column String ColType = DR ["TYPE"]. ToString (); // SQLTYPE INT LEN = Convert.TOINT32 (DR ["Length"]. Tostring ()); // Column wide bool bnull = DR ["isnull" ] .Tostring () == "1"? True: false; // can be empty int colid = convert.Toint32 (DR ["colid"]. TOSTRING ()); BOOL BPK = DR ["status"]. TOSTRING () == "128" True: false; string [] {= new string [] {colna, colType, len.tostring (), colid.tostring (), bnull.tostring (), bpk.tostring ()} This.Tabpty.Items.Add (New ListViewItem ());} Dr.close (); this.sqlConnection1.close ();
private void button1_Click (object sender, System.EventArgs e) {this.listView1.Items.Clear (); this.listView2 .Items.Clear (); string str = "select r.fkey1 as TabColID, o.name as refTabName, R.RKEYINDID AS REFTABCOLID "; STR =" from sysreference r, sysobjects o "; str =" where rkeyid = @ id and o.id = r.fkeyid "; sqlcommand cmd = new sqlcommand (); cmd.connection = this. SqlConnection1; cmd.commandtext = str; cmd.parameters.add ("@ id", this.idstr.text); cmd.connection.open (); sqldatareader DR = cmd.executeReader (); while (Dr.Read () ) {String C1 = DR [0] .tostring (); string c2 = dr [1] .tostring (); string c3 = dr [2] .tostring (); listviewItem lvi = new listviewItem (new string " , C2, C3}); this.listview1.items.add (lvi);} Dr.close (); cmd.connection.close (); str = "SELECT R.FKEY1 AS TOBCOLID, O.NAME AS REFTABNAME, R .rkeyindid as rebabcolid "; str =" from sysreferences r, sysobjects o "; str =" where fkeyid = @ id and o.id = r.rkeiid "; Cmd.COMMANDText = str; cmd.connection.open (); dr = cmd.executeReader (); while (Dr.READ ()) {string c1 = dr [0] .tostring (); string c2 = DR [ 1] .tostring (); String C3 = DR [2] .tostring (); listviewItem lvi = new listviewItem (new string [] {C1, C2, C3}); this.listview2.Items.Add (LVI); DR.Close (); cmd.connection.close ();
Private void button2_click (object sender, system.eventargs e) {INT KC = 0; if (this.listview1 .Items.count> 0) {kc = this.listview1.items.count; for (int i = 0; i < Kc; i ) {listview1.Items [i]; string color = lvi.subitems [0] .text; string tabname = lvi.subitems [1] .text; string coliD2 = lvi.subitems [2] .Text; lvi.subitems [0] .Text = this.getcolname (tabname, colid); Foreach (listviewItem TC in this.tabpty.items) {if (tc.subitems [3] .text == colid2) {LVI. Subitems [2] .text = tc.subitems [0] .Text;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} {kc = this.listview2.cent; for (int i = 0; i }}} Private string getColName (string tabName, string colid) {string cmdstr = "select name from syscolumns"; cmdstr = "where id = (select id from sysobjects where xtype = 'u' and name = @ tab) and colid = @colid "; sqlcommand cmd = new sqlcommand (cmdstr, this.s.sqlConnection1); cmd.parameters.add (" @ Tab ", tabname); cmd.parameters.add (" @ colid ", colid); cmdstr =" cmd.connection.close (); cmd.connection.open (); SqlDataReader DR = cmd.executeReader (); string str = ""; if (Dr.Read ()) {str = DR [0] .tostring () Dr.Close (); cmd.connection.close (); return str;}}}}