/// <Summary> /// Obtain the names of all database servers in the LAN. /// </Summary> /// <Returns> Server Name Array </Returns> Public List < String > Getsqlservernames () {datatable datasources =Sqlclientfactory. instance. createdatasourceenumerator (). getdatasources (); datacolumn Column = Datasources. Columns [ " InstanceName " ]; Datacolumn column2 = Datasources. Columns [ " Servername " ]; Datarowcollection rows = Datasources. Rows; List < String > Serverlist = New List < String > (); String Array = String . Empty; For ( Int I = 0 ; I <rows. Count; I ++ ){ String Str2 = rows [I] [column2] As String ; String STR = rows [I] [column] As String ; If (STR = Null ) | (Str. Length = 0 ) | ( " MSSQLServer " = Str) {Array = Str2 ;} Else {Array = Str2 + @" \ " + STR;} serverlist. Add (array);} serverlist. Sort (); Return Serverlist ;} /// <Summary> /// Query non-system databases in SQL /// </Summary> /// <Param name = "connection"> </param> /// <Returns> </returns> Public List < String > Databaselist ( String Connection) {list < String > Getcatalist = New List < String > (); String Optional stirng = " Select name from SYS. databases where database_id> 4 " ; Sqlconnection connect = New Sqlconnection (connection); sqlcommand cmd = New Sqlcommand (begin stirng, connect ); Try { If (Connect. State =Connectionstate. Closed) {connect. open (); idatareader Dr = Cmd. executereader (); getcatalist. Clear (); While (Dr. Read () {getcatalist. Add (Dr [ " Name " ]. Tostring ();} Dr. Close ();}} Catch (Sqlexception e ){ // MessageBox. Show (E. Message ); } Finally { If (Connect! = Null & Connect. State = Connectionstate. Open) {connect. Dispose ();}} Return Getcatalist ;} /// <Summary> /// Retrieve column names /// </Summary> /// <Param name = "connection"> </param> /// <Returns> </returns> Public List < String > Gettables ( String Connection) {list < String > Tablelist =New List < String > (); Sqlconnection objconnetion = New Sqlconnection (connection ); Try { If (Objconnetion. State = Connectionstate. Closed) {objconnetion. open (); datatable objtable = Objconnetion. getschema ( " Tables " ); Foreach (Datarow row In Objtable. Rows) {tablelist. Add (row [ 2 ]. Tostring ());}}} Catch {} Finally { If (Objconnetion! = Null & Objconnetion. State = Connectionstate. Closed) {objconnetion. Dispose ();}} Return Tablelist ;} /// <Summary> /// Obtain Fields /// </Summary> /// <Param name = "connection"> </param> /// <Param name = "tablename"> </param> /// <Returns> </returns> Public List < String > Getcolumnfield ( String Connection, String Tablename) {list < String > Columnlist = New List < String > (); Sqlconnection objconnetion = New Sqlconnection (connection ); Try { If (Objconnetion. State = Connectionstate. Closed) {objconnetion. open ();} sqlcommand cmd = New Sqlcommand ( " Select name from syscolumns where id = object_id (' " + Tablename + " ') " , Objconnetion); sqldatareader objreader = Cmd. executereader (); While (Objreader. Read () {columnlist. Add (objreader [ 0 ]. Tostring ());}} Catch {} Objconnetion. Close (); Return Columnlist ;}