Obtain all database servers in the LAN, query SQL Server non-system databases, all tables, and all columns

Source: Internet
Author: User
        ///   <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 ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.