Thursday, October 21, 2010

How to write a program in vb.net in order to backup and restore the mysql database?

I am developing a project in vb.net and i am using MySQL database as a backend. I want to backup and restore the mysql database through vb.net. How to write a program in vb.netHow to write a program in vb.net in order to backup and restore the mysql database?
Neofact... is correct BUT Beware... IF this is a large database on a large site with lots of traffic, dumping a database will lock tables a few at a time. This means that it is possible that a table will be locked for long enough that your site timesout for a while...



There are two options you can add that will prevent locking, but locking the tables ensures data integrity, and while without locking you will get all the data, you may miss rows that were updated between when it started dumping the table and it ended...



These options are --single-transaction, and something else.



But, if you don't have large tables, (100MB or larger) it probably isn't a big deal.How to write a program in vb.net in order to backup and restore the mysql database?
you have to use following code in your vb.net program.



Public Sub backupDB()

Dim portfolioPath As String = My.Application.Info.DirectoryPath

FileCopy(portfolioPath %26amp; ';\InformatiSources\portfolioDB.mdb';, ';C:\Program Files\PortfolioGen2.0\DBBACKUP.mdb';)

MsgBox(';Backup Successful';)

End Sub









Public Sub restoreDB()

Dim portfolioPath As String = My.Application.Info.DirectoryPath

If MessageBox.Show(';Restoring the database will erase any changes you have made since you last backup. Are you sure you want to do this?';, _

';Confirm Delete';, _

MessageBoxButtons.OKCancel, _

MessageBoxIcon.Question, _

MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then

'Restore the database from a backup copy.



FileCopy(';C:\Program Files\PortfolioGen2.0\DBBACKUP.mdb';, portfolioPath %26amp; ';\Information Sources\portfolioDB.mdb';)



MsgBox(';Database Restoration Successful';)

End If



End Sub
the only thing you have to add to your code is



shell(';C:\Program Files\mysql\mysql server 4.1\bin\mysqldump --opt --password=pass -user=root --database test -r c:\test.sql';)



the -r let you specify a dir for your backup archive, like this, c:\test.sql



or..

mysqldump.exe -u root -ppassword --databases klapper %26gt; c:\temp\DumpKlapper.sql

No comments:

Post a Comment