Get all database names, table names, column names on the target server

Source: Internet
Author: User

1         /// <summary>2         ///get all database names for the target server3         /// </summary>4         /// <param name= "ServerName" ></param>5         /// <param name= "UserName" ></param>6         /// <param name= "password" ></param>7          Public voidGetdatabasenamelist (stringServerName,stringUserName,stringpassword)8         {9SQLDMO. Application Sqlapplication =NewSQLDMO. ApplicationClass ();TenSQLDMO. SQL Server SQL Server =NewSQLDMO. Sqlserverclass (); One              ASqlserver.connect (serverName, userName, password);//connecting to a server -  -             foreach(SQLDMO. Database Databbaseinchsqlserver.databases) the             { -                 if(Databbase.name! =NULL) -                 { -                      This. DATABASETREEVIEW.NODES.ADD (databbase.name); +  - getdatabasetablelist (serverName, userName, password, databbase.name); +                 } A             } at         } -  -  -         /// <summary> -         ///loading tables in the database -         /// </summary> in         /// <param name= "ServerName" >Server name</param> -         /// <param name= "UserName" >User name</param> to         /// <param name= "password" >Password</param> +         /// <param name= "DataBaseName" >Database name</param> -         Private voidGetdatabasetablelist (stringServerName,stringUserName,stringPasswordstringdataBaseName) the         { *SQLDMO. SQL Server =NewSQLDMO. Sqlserverclass (); $ Panax Notoginseng             //Connect to Server - Server.connect (serverName, userName, password); the  +             //for all database traversal, get the specified database A              for(inti =0; i < Server.Databases.Count; i++) the             { +                 //determines whether the current database is a specified database -                 if(Server.Databases.Item (i +1,"dbo"). Name = =dataBaseName) $                 { $                     //get the specified database -Sqldmo._database db = Server.Databases.Item (i +1,"dbo"); -  the                     //get all tables in the specified database -                      for(intj =0; J < Db. Tables.count; J + +)Wuyi                     { the                          This. Databasetreeview.nodes[i]. Nodes.Add (db. Tables.item (j +1,"dbo"). Name); -                     } Wu                 } -             } About         } $  -  -     /// <summary> -         ///get all column names in the table A         /// </summary> +         /// <param name= "ServerName" >Server name</param> the         /// <param name= "UserName" >User name</param> -         /// <param name= "password" >Password</param> $         /// <param name= "TableName" >Table name</param> the         /// <param name= "DataBaseName" >Database name</param> the         /// <returns></returns> the          Public stringGetrowlistfromtable (stringServerName,stringUserName,stringPasswordstringTableName,stringdataBaseName) the         { -             stringresult =string. Empty; in  the             stringConnectionString =string. Empty; theConnectionString + ="server="+ServerName; AboutConnectionString + ="; Pwd="+password; theConnectionString + ="; Uid="+UserName; theConnectionString + =";D atabase="+DataBaseName; the  +             stringCommandString =string. Empty; -CommandString + ="select name from syscolumns where id=object_id ('"; theCommandString + =TableName;BayiCommandString + ="')"; the              the  -SqlConnection SqlConnection =NewSqlConnection (connectionString); -SqlCommand SqlCommand =NewSqlCommand (CommandString, sqlConnection); the  theSqlDataAdapter dataAdapter =NewSqlDataAdapter (CommandString, sqlConnection); the  theDataSet DataSet =NewDataSet (); - DataAdapter.Fill (dataSet); the  theDataTable datatable = dataset.tables[0]; the 94             //datatable datatable = Sqlconnection.getschema ("Tables"); the             foreach(DataRow rowinchdatatable.rows) the             { theResult + = row[0]. ToString () +"-";98             } About  -             if(Result! =NULL)101             {102                 returnresult; 103             }104             Else the             {106                 return "0";107             }108}

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.