.net备份数据库mysql_如何使用mysql和c#.net备份数据库

bd96500e110b49cbb3cd949968f18be7.png

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 connectionstring

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值