2.还要使用c#代码备份数据库
我的应用程序是一个独立的应用程序并使用vs2010。 这是我到目前为止所尝试的。无法确定抛出的错误。任何建议都会有所帮助。对此的任何帮助都会有很大的帮助
enter code here
int year = Time.Year;
int month = Time.Month;
int day = Time.Day;
int hour = Time.Hour;
int minute = Time.Minute;
int second = Time.Second;
int millisecond = Time.Millisecond;
//Save file to C:\ with the current date as a filename
string path;
path = "D:\\yourfoldername" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
StreamWriter file = new StreamWriter(path);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysqldump";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "", "localhost", "database");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
enter code here
答案 0 :(得分:1)
如果是整个数据库,那么:
$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql
如果是所有数据库,那么:
$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql
如果是DB中的特定表,那么:
$ mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql
您甚至可以使用gzip自动压缩输出(如果您的数据库非常大):
$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz
如果您想远程执行此操作并且您可以访问相关服务器,那么以下操作(假设MySQL服务器位于端口3306上):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql
要进口:
使用以下命令导入sql数据文件:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
在此示例中,使用vivek将'data.sql'文件导入'blog'数据库作为用户名:
$ mysql -u sat -p -h localhost blog < data.sql
如果您有专用数据库服务器,请将localhost主机名替换为实际服务器名称或IP地址,如下所示:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
或使用主机名,例如mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql
如果您不知道sql dump中包含数据库名称或数据库名称,您可以尝试以下内容:
$ mysql -u username -p -h 202.54.1.10 < data.sql
参考:http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
使用C#在MySQL中备份数据库
备份MySQL数据库
private void Backup()
{
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ExportToFile(file);
conn.Close();
}
}
}
}
恢复MySQL数据库
private void Restore()
{
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ImportFromFile(file);
conn.Close();
}
}
}
}
答案 1 :(得分:0)
您可以使用mysqldump
来完成$ mysqldump -u [uname] -p[pass] [dbname] > [backupfile.sql]
看看这个
http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/
http://www.mediacollege.com/computer/database/mysql/backup.html
答案 2 :(得分:0)
看看这个项目:
MySqlBackup.NET - MySQL Backup Solution for C#
您不必编写大量代码。我正在通过官方文档为您评论一个示例:
简单导出示例
using MySql.Data.MySqlClient;
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ExportToFile(file);
conn.Close();
}
}
}
我希望它对未来的读者有所帮助。
答案 3 :(得分:0)
使用C#在MySQL中备份数据库
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > " + textBackup.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);
textBackup.Text 实际上是带文件的路径,例如。 E:\ tmp.sql
如果您想要访问用户定义的路径,那么就像这样写一下----
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);
使用C#
恢复MySQL数据库Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < " + textRestore.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);
textRestore.Text 实际上是文件的路径,例如。 E:\ tmp.sql
如果您想要访问用户定义的路径,那么就像这样写一下----
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);