我是Mysql和C#的新手。我曾尝试编写一个函数来备份我的数据库。
这是我写的:
public static void Backup()
{
try
{
DateTime Time = DateTime.Now;
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;
string path;
path = "C:\\MySqlBackup" + year + "-" + month + "-" + day +
"-" + hour + "-" + minute + "-" + second + "-" + millisecond +".sql";
StreamWriter file = new StreamWriter(path);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3} > {4};",
uid, password, server, database, path);
psi.UseShellExecute = false;
Process process = Process.Start(psi);
Process process = Process.Start(psi);
string output;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
}
catch (IOException ex)
{
Console.Write("Error , unable to backup!");
}
}
问题是在备份文件中我只得到第一行:
-- MySQL dump 10.13 Distrib 5.1.51, for Win32 (ia32)
--
-- Host: localhost Database: try
-- ------------------------------------------------------
-- Server version 5.1.51-community
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
但所有表格和所有记录都不是备份。
我尝试在cmd上运行备份,但它运行正常。我找不到原因。
有人可以帮帮我吗?我会感激任何帮助。 感谢
答案 0 :(得分:1)
我想知道为什么你有这两行:
Process process = Process.Start(psi);
也许这与它有关?
答案 1 :(得分:1)
问题在于这一行:
string.Format(@"-u{0} -p{1} -h{2} {3} > {4};",uid, password, server, database, path);
只需将该行重写为:
string.Format(@"-u{0} -p{1} -h{2} {3}",uid, password, server, database, path);