/// <summary> ///get all database names for the target server/// </summary> /// <param name= "ServerName" ></param> /// <param name= "UserName" ></param> /// <param name= "password" ></param> Public voidGetdatabasenamelist (stringServerName,stringUserName,stringpassword) {SQLDMO. Application Sqlapplication=NewSQLDMO. ApplicationClass (); SQLDMO. SQL Server SQL Server=NewSQLDMO. Sqlserverclass (); Sqlserver.connect (serverName, userName, password); //connecting to a server foreach(SQLDMO. Database Databbaseinchsqlserver.databases) {if(Databbase.name! =NULL) { This. DATABASETREEVIEW.NODES.ADD (Databbase.name); Getdatabasetablelist (serverName, userName, password, databbase.name); } } } /// <summary> ///loading tables in the database/// </summary> /// <param name= "ServerName" >Server name</param> /// <param name= "UserName" >User name</param> /// <param name= "password" >Password</param> /// <param name= "DataBaseName" >Database name</param> Private voidGetdatabasetablelist (stringServerName,stringUserName,stringPasswordstringdataBaseName) {SQLDMO. SQL Server=NewSQLDMO. Sqlserverclass (); //Connect to ServerServer.connect (serverName, userName, password); //for all database traversal, get the specified database for(inti =0; i < Server.Databases.Count; i++) { //determines whether the current database is a specified database if(Server.Databases.Item (i +1,"dbo"). Name = =dataBaseName) { //get the specified databaseSqldmo._database db = Server.Databases.Item (i +1,"dbo"); //get all tables in the specified database for(intj =0; J < Db. Tables.count; J + +) { This. Databasetreeview.nodes[i]. Nodes.Add (db. Tables.item (j +1,"dbo"). Name); } } } } /// <summary> ///get all column names in the table/// </summary> /// <param name= "ServerName" >Server name</param> /// <param name= "UserName" >User name</param> /// <param name= "password" >Password</param> /// <param name= "TableName" >Table name</param> /// <param name= "DataBaseName" >Database name</param> /// <returns></returns> Public stringGetrowlistfromtable (stringServerName,stringUserName,stringPasswordstringTableName,stringdataBaseName) { stringresult =string. Empty; stringConnectionString =string. Empty; ConnectionString+="server="+ServerName; ConnectionString+="; Pwd="+password; ConnectionString+="; Uid="+UserName; ConnectionString+=";D atabase="+DataBaseName; stringCommandString =string. Empty; CommandString+="select name from syscolumns where id=object_id ('"; CommandString+=TableName; CommandString+="')"; SqlConnection SqlConnection=NewSqlConnection (connectionString); SqlCommand SqlCommand=NewSqlCommand (CommandString, sqlConnection); SqlDataAdapter DataAdapter=NewSqlDataAdapter (CommandString, sqlConnection); DataSet DataSet=NewDataSet (); DataAdapter.Fill (DataSet); DataTable DataTable= dataset.tables[0]; //datatable datatable = Sqlconnection.getschema ("Tables"); foreach(DataRow rowinchdatatable.rows) {result+ = row[0]. ToString () +"-"; } if(Result! =NULL) { returnresult; } Else { return "0"; } }
C # Get implementation code for all database names, table names, column names in the destination server