Using sqldmo to manipulate MS-SQL Databases

Source: Internet
Author: User
Sqldmo (SQL distributed management objects, SQL distributed management object) is a group of COM components developed by Ms.

// Retrieve Server LIST

Private Arraylist getserverlist ()

{

System. Collections. arraylist alservers =   New System. Collections. arraylist ();

Sqldmo. Application sqlapp =   New Sqldmo. applicationclass ();

Try

{

Sqldmo. namelist serverlist = Sqlapp. listavailablesqlservers ();

For ( Int I =   1 ; I <= Serverlist. Count; I ++ )

{

Alservers. Add (serverlist. Item (I ));

}  

}  

Catch (Exception E)

{

Throw(NewException ("An error occurred while retrieving the database server list:"+E. Message ));

}  

Finally  

{

Sqlapp. Quit ();

}  

Return Alservers;

}







// Obtain the Database List on the server

Private   Bool Getdblist ( String Strservername, String Strusername, String Strpwd)

{

Bool Bolreturn =   False ;

Arraylist aldbs =   New Arraylist ();

Sqldmo. Application sqlapp =   New Sqldmo. applicationclass ();

Sqldmo. svr SVR =   New Sqldmo. sqlserverclass ();

Try

{

SVR. Connect (strservername, strusername, strpwd );

Foreach (Sqldmo. Database DB In SVR. databases)

{

If(Db. Name! =Null)

Aldbs. Add (db. Name );

}

This . Cmbdb. datasource = Aldbs;

Bolreturn =   True ;

}

Catch (Exception E)

{

Throw(NewException ("Database Connection error:"+E. Message ));

}  

Finally  

{

SVR. Disconnect ();

Sqlapp. Quit ();

}

Return Bolreturn;

}  








// Database Backup

Private   Void Dbbackup ( String Savepath, String Database)

{

Sqldmo. svr SVR =   New Sqldmo. sqlserverclass ();

Try

{

SVR. Connect ( This . Server, This . Userid, This . Password );

Sqldmo. Backup Bak =   New Sqldmo. backupclass ();


Sqldmo. backupsink_percentcompleteeventhandler pceh =   New Sqldmo. backupsink_percentcompleteeventhandler (setbarvisible );

Bak. percentcomplete + = Pceh;


Bak. Action = Sqldmo. sqldmo_backup_type.sqldmobackup_database;


Bak. initialize =   True ;

Bak. Files = Savepath +   @" \ 1.bak " ;

Bak. Database = Database;

Bak. sqlbackup (SVR );

}

Catch (System. Exception XX)

{

Throw(NewException ("Database Backup failed" +XX. Message ));

}

Finally

{

SVR. Disconnect ();

}

}

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.