.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

how to take backup from mysql using c#

Posted By: Naim Khan     Posted Date: January 29, 2011    Points:2   Category :Windows Application
hello friends,
i have very big problem with backup process... i m sending you file please check it and tell me how can take full backup with Store Process.. from this code just taking only tables backup its not taking full back of my Data Base.. so please check code and reply me ASAP...

///code
// backup process
public void back_up()
{
try
{
this.Cursor = Cursors.WaitCursor;
DateTime backupTime = DateTime.Now;
int year = backupTime.Year;
int month = backupTime.Month;
int day = backupTime.Day;
int hour = backupTime.Hour;
int minute = backupTime.Minute;
int second = backupTime.Second;
int ms = backupTime.Millisecond;
String tmestr = backupTime.ToString();
ProcessStartInfo procInfo = new ProcessStartInfo(System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\MySQL\\MySQL Server 5.1\\bin\\mysqldump", @" --user=root --password=psr --database "+LoginForm.company+"");
procInfo.UseShellExecute = false;
procInfo.RedirectStandardError = true;
procInfo.CreateNoWindow = false;
procInfo.RedirectStandardOutput = true;
Process p = Process.Start(procInfo);
string sResult = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
string newpath = "D:\\LeaveBackUp";
System.IO.Directory.CreateDirectory(newpath);
if (!System.IO.File.Exists(newpath))
{
System.IO.StreamWriter writer = new StreamWriter(@"D:\LeaveBackUp\leave " + year + "-" + month + "-" + day + "-" + hour);
writer.Write(sResult);
writer.Close();
MessageBox.Show("BackUp SuccessFully Completed ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Cursor = Cursors.Arrow;
}
else
{
MessageBox.Show("Folder Not Created", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
// now this code
// end
i will really appreciate you...

thanks in Advance

Responses
Author: Amit Mehra             
Posted Date: January 30, 2011     Points: 5   

Hi,

try this code to take mysql database back up using C# code

[DllImport("kernel32.dll")] 
static extern int GetShortPathName(string longPath, StringBuilder buffer, int bufferSize);



public void Batch()
{
StringBuilder buffer = new StringBuilder(256);
GetShortPathName(Path.GetDirectoryName(MySqlEXEPath), buffer, buffer.Capacity);
string shortfilename = buffer.ToString();

string BatchFile = @"c:\temp\Batch.bat";
StreamWriter sw = new StreamWriter(BatchFile);
sw.WriteLine("cd " + shortfilename);
sw.WriteLine(@"mysql -uroot -pXXX DB_NAME < C:\TEMP\FILE.SQL");
sw.Flush();
sw.Close();

//IMPORTANT, DO NOT FORGOT THIS
Environment.CurrentDirectory = @"c:\windows\";

Process.Start(BatchFile);

}


ref: http://forums.mysql.com/read.php?47,167716,205819#msg-205819

Thanks,
Amit
Please "Mark As Answer " if this post help you.


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend