AltınSoft Makale Arşivi

Web Hosting Hizmetleri

Anasayfa | Toplu SMS | Video | Web Hosting | Makale Ara | Anketler | Sitene Ekle | RSS Kaynağı

Arama


Gelişmiş Arama

c# ve vb.net mysql backup restore işlemleri

c# ve vb.net mysql backup restore işlemleri

Kategori  Kategori : Asp.NET
Yorumlar  Yorum Sayısı : 0
Okunma  Okunma : 239
Tarih  Tarih : 28 Ekim 2009 21:42

Örnek 1 :

 

MySql_Backup.bat



@ECHO off
cls
set DBchoice=%1%
set User=%2%
set Password=%3%
set pathchoice=%4%

@REM Remove double quotes from the path
@REM SET pathchoice=%pathchoice:"=%
@REM SET pathchoice=%pathchoice:"=%

mysqldump --add-drop-table -B %DBchoice% -u %User% --password=%Password% > %pathchoice%


MySql_Restore.bat




@ECHO off
cls

set User=%1%
set Password=%2%
set DBchoice=%3%
set pathchoice=%4%
set hostIP=%5%
set toolPath=%6%

@REM Remove double quotes from the path
@REM SET pathchoice=%pathchoice:"=%
@REM SET pathchoice=%pathchoice:"=%

%toolPath%\mysql -u %User% -h%hostIP% --password=%Password% %DBchoice% < %pathchoice%
 
Create the following method to execute the batch files with proper  parameters

 

C#




/// /// Author : Himasagar Kutikuppala
///A utility method that runs the batch file with supplied arguments.
///
/// /// /// protected bool ExecuteBatchFile(string batchFileName, string[] argumentsToBatchFile)
{
string argumentsString = string.Empty;
try
{
//Add up all arguments as string with space separator between the arguments
if (argumentsToBatchFile != null)
{
for (int count = 0; count < argumentsToBatchFile.Length; count++)
{
argumentsString += " ";
argumentsString += argumentsToBatchFile[count];
//argumentsString += "\"";
}
}

//Create process start information
System.Diagnostics.ProcessStartInfo DBProcessStartInfo = new System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString);

//Redirect the output to standard window
DBProcessStartInfo.RedirectStandardOutput = true;

//The output display window need not be falshed onto the front.
DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
DBProcessStartInfo.UseShellExecute = false;

//Create the process and run it
System.Diagnostics.Process dbProcess;
dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo);

//Catch the output text from the console so that if error happens, the output text can be logged.
System.IO.StreamReader standardOutput = dbProcess.StandardOutput;

/* Wait as long as the DB Backup or Restore or Repair is going on.
Ping once in every 2 seconds to check whether process is completed. */
while (!dbProcess.HasExited)
dbProcess.WaitForExit(2000);

if (dbProcess.HasExited)
{
string consoleOutputText = standardOutput.ReadToEnd();
//TODO - log consoleOutputText to the log file.

}

return true;
}
// Catch the SQL exception and throw the customized exception made out of that
catch (SqlException ex)
{

ExceptionManager.Publish(ex);
throw SQLExceptionClassHelper.GetCustomMsSqlException(ex.Number);
}
// Catch all general exceptions
catch (Exception ex)
{
ExceptionManager.Publish(ex);
throw new CustomizedException(ARCExceptionManager.ErrorCodeConstants.generalError, ex.Message);
}
}
 VB.NET
 
  1. ''' <summary&RT;
  2. ''' Author : Himasagar Kutikuppala
  3. '''A utility method that runs the batch file with supplied arguments.
  4. ''' </summary&RT;
  5. ''' <param name="batchFileName"&RT;Name of the batch file that should be run</param&RT;
  6. ''' <param name="argumentsToBatchFile"&RT;Arguments to the batch file</param&RT;
  7. ''' <returns&RT;Status of running the batch file</returns&RT;
  8. Protected Function ExecuteBatchFile(ByVal batchFileName As String, ByVal argumentsToBatchFile As String()) As Boolean
  9.     Dim argumentsString As String = String.Empty
  10.     Try
  11.         'Add up all arguments as string with space separator between the arguments
  12.         If argumentsToBatchFile IsNot Nothing Then
  13.             For count As Integer = 0 To argumentsToBatchFile.Length - 1
  14.                 argumentsString += " "
  15.                     'argumentsString += "\"";
  16.                 argumentsString += argumentsToBatchFile(count)
  17.             Next
  18.         End If
  19.        
  20.         'Create process start information
  21.         Dim DBProcessStartInfo As New System.Diagnostics.ProcessStartInfo(batchFileName, argumentsString)
  22.        
  23.         'Redirect the output to standard window
  24.         DBProcessStartInfo.RedirectStandardOutput = True
  25.        
  26.         'The output display window need not be falshed onto the front.
  27.         DBProcessStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
  28.         DBProcessStartInfo.UseShellExecute = False
  29.        
  30.         'Create the process and run it
  31.         Dim dbProcess As System.Diagnostics.Process
  32.         dbProcess = System.Diagnostics.Process.Start(DBProcessStartInfo)
  33.        
  34.         'Catch the output text from the console so that if error happens, the output text can be logged.
  35.         Dim standardOutput As System.IO.StreamReader = dbProcess.StandardOutput
  36.        
  37.         ' Wait as long as the DB Backup or Restore or Repair is going on.
  38. ' Ping once in every 2 seconds to check whether process is completed.
  39.        
  40.         While Not dbProcess.HasExited
  41.             dbProcess.WaitForExit(2000)
  42.         End While
  43.        
  44.         If dbProcess.HasExited Then
  45.                 'TODO - log consoleOutputText to the log file.
  46.                
  47.             Dim consoleOutputText As String = standardOutput.ReadToEnd()
  48.         End If
  49.        
  50.         Return True
  51.     Catch ex As SqlException
  52.         ' Catch the SQL exception and throw the customized exception made out of that
  53.        
  54.         ExceptionManager.Publish(ex)
  55.         Throw SQLExceptionClassHelper.GetCustomMsSqlException(ex.Number)
  56.     Catch ex As Exception
  57.         ' Catch all general exceptions
  58.         ExceptionManager.Publish(ex)
  59.         Throw New CustomizedException(ARCExceptionManager.ErrorCodeConstants.generalError, ex.Message)
  60.     End Try
  61. End Function
 
Örnek 2 :
string cmdStr = "BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + TempFolder + "\\" + DatabaseName + "_Backup.bak" + "' WITH NOFORMAT, INIT, NAME = N'" + DatabaseName + "-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 ";
 
 
Örnek 3 :
process.run("mysqldump.exe","--user=ADMIN --password=PASS db_try > c:/backup.sql"); 

but it doesnt work

and i also try to restore the database and i do this in the c:\> prompt and it works:

mysql -u ADMIN -pPASS db_try< c:/backup.sql

and when i try to do it with process.run("mysql.exe","-u ADMIN -pPASS db_try< c:/backup.sql");

 

Yazdırılabilir Sayfa Yazdırılabilir Sayfa | Word'e Aktar Word'e Aktar | Tavsiye Et Tavsiye Et | Yorum Yaz Yorum Yaz

Asp.NET

En Çok Okunan Makaleler

Seçtiklerimiz

Anket

AltınSoft Makale Arşivini beğendinizmi ?




Tüm Anketler

© 2004-2009 Tüm Hakları Saklıdır
RSS Kaynağı

AltınSoft Bilişim Teknolojileri

Altyapı: MyDesign Haber Sistemi