开发者

how to backup the database using mysql and c#.net

开发者 https://www.devze.com 2023-04-11 04:48 出处:网络
I have found this solution for back up my database (mysql) using c#.net string fname = txtFileName.Text;

I have found this solution for back up my database (mysql) using c#.net

string fname = txtFileName.Text;


if (fname == "")
{

 MessageBox.Show("Please Enter the File Name!");return;
}

try
{

      btnBackup.Enabled = false;
      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();

 // C:\Program Files\MySQL\MySQL Server 5.0\bin

   //tmestr = "C:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".bak";

    tmestr = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\" + fname + year + "-" + month + "-" + day + "-" + hour;// +".sql";

   StreamWriter file = new StreamWriter(tmestr);
  ProcessStartInfo proc = new ProcessStartInfo();

    string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname);
     proc.FileName = "mysqldump";

   proc.RedirectStandardInput = false;
   proc.RedirectStandardOutput = true;

   proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql";

   proc.UseShellExecute = false;
   Process p = Process.Start(proc);

   string res;
   res = p.StandardOutput.ReadToEnd();

  file.WriteLine(res);

   p.WaitForExit();

  file.Close();

  MessageBox.Show("DataBase Backup Has Been Completed Successfully!");btnBackup.Enabled = true;
}

catch (IOException ex)
{

  MessageBox.Show("Disk full or other IO error , unable to backup!");
}

txtFileName.Text = "";

which value i have to give in this text box "txtfilename.txt"

and what i have to give in this values @"-u{0} -p{1} -h{2} {3}", user, passwd1, Data_Source, dbname

I have found the mysqldump.exe file in this location

string location = "C:\\Program Files\\MySQL\\MySQL WorkBench 5.2CE\\";

and this is my conne开发者_开发问答ctionstring

string connestring = "server=localhost;user=root;database=access";

I am not sure which values i have to give in these places user, passwd1, Data_Source, dbname

would any one pls help on this guys

Many thanks..


First off, the location of mysqldump.exe that you should be using is within the same directory as mysql itself (C:\Program Files\MySQL\MySQL Server 5.5\bin for example), use that and no other copy.

There is no connection string.

In the parent directory (ie C:\Program Files\MySQL\MySQL Server 5.5) you'll find the configuration file my.ini where under the [client] heading you can set the connection settings (username/password etc). Of if you prefer, you specify the login information as arguments when starting the mysqldump process (a list of arguments is provided by MySQL).

An example, will dump everything in the databases you specify (data, structure, triggers, the lot, and will overwrite any tables when you then import it back again, use the command line arguments depending on what you want).

    public static void DumpStructure()
{
    Process sd = null;
    ProcessStartInfo r1 = new ProcessStartInfo(/* Full path to MySqlDump.exe */, "--databases exampleDatabase1 exampleDatabase2 --compress --routines --triggers --add-drop-database --add-drop-table --add-locks --extended-insert --password=YOURPASSWORD --port=8307 --user=YOURUSERNAME --disable-keys --quick --comments --complete-insert --result-file=DUMPEDOUTPUT.sql");

    r1.CreateNoWindow = true;
    r1.WorkingDirectory = /* WHERE MYSQL.EXE IS STORED */;
    r1.UseShellExecute = false;
    r1.WindowStyle = ProcessWindowStyle.Minimized;
    r1.RedirectStandardInput = false;

    sd = Process.Start(r1);
    sd.WaitForExit();

    if (!sd.HasExited) {
        sd.Close();
    }
    sd.Dispose();
    r1 = null;
    sd = null;  
}


You can just do a mysqldump if you want to backup your database:

I don't use Windows but this should more or less do the trick: Navigate to where your mysqldump is using the command prompt and execute this command:

mysqldump -u root -p --databases [my db name] > file.sql

When prompted for the password, enter your password.


I am not sure that I understand your question, but:

user: the user name for the account you are using to connect to mysql
passwd1: the associated password
Data_Source: the host name of the system that the mysql server is running on
dbname: the name of the database schema you are trying to back up
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号