代码改变世界

C#对MySQL数据库的还原与备份,以及带参数形式不是"@"而是"?"

2010-11-18 14:38  三皮开发时  阅读(391)  评论(0编辑  收藏  举报

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace eFormPC.DBUtility
{
    
public class MySQLCmd
    {
        
/// <summary>
        
/// 执行Cmd命令
        
/// </summary>
        
/// <param name="workingDirectory">要启动的进程的目录</param>
        
/// <param name="command">要执行的命令</param>
        public static void StartCmd(String workingDirectory, String command)
        {
            Process p 
= new Process();
            p.StartInfo.FileName 
= "cmd.exe";
            p.StartInfo.WorkingDirectory 
= workingDirectory;
            p.StartInfo.UseShellExecute 
= false;
            p.StartInfo.RedirectStandardInput 
= true;
            p.StartInfo.RedirectStandardOutput 
= true;
            p.StartInfo.RedirectStandardError 
= true;
            p.StartInfo.CreateNoWindow 
= true;
            p.Start();
            p.StandardInput.WriteLine(command);
            p.StandardInput.WriteLine(
"exit");
        }

    }
}

 

 

 

代码
 //MySQL备份
        private void button8_Click(object sender, EventArgs e)
        {
            
try
            {
                
//String command = "mysqldump --quick --host=localhost --default- character-set=gb2312 --lock-tables --verbose  --force --port=端口号 --user= 用户名 --password=密码 数据库名 -r 备份到的地址";

                
//构建执行的命令
                StringBuilder sbcommand = new StringBuilder();

                StringBuilder sbfileName 
= new StringBuilder();
                sbfileName.AppendFormat(
"{0}", DateTime.Now.ToString()).Replace("-""").Replace(":""").Replace(" """);
                String fileName 
= sbfileName.ToString();

                SaveFileDialog saveFileDialog 
= new SaveFileDialog();
                saveFileDialog.AddExtension 
= false;
                saveFileDialog.CheckFileExists 
= false;
                saveFileDialog.CheckPathExists 
= false;
                saveFileDialog.FileName 
= fileName;

                
if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    String directory 
= saveFileDialog.FileName;
                    sbcommand.AppendFormat(
"mysqldump   --character-sets-dir=utf-8  --user=root --password=1 tabletpc -r \"{0}\""@"C:\123.sql");
                    String command 
= sbcommand.ToString();

                    
//获取mysqldump.exe所在路径
                    
//String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
                    String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\";
                    MySQLCmd.StartCmd(appDirecroty, command);
                    MessageBox.Show(
@"数据库已成功备份到 " + directory + " 文件中""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            
catch (Exception ex)
            {
                MessageBox.Show(
"数据库备份失败!");
                
            }
        }

 

 

 

代码
 //还原
        private void button9_Click(object sender, EventArgs e)
        {
            
try
            {
                StringBuilder sbcommand 
= new StringBuilder();

                OpenFileDialog openFileDialog 
= new OpenFileDialog();

                
if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    String directory 
= openFileDialog.FileName;

                    
//在文件路径后面加上""避免空格出现异常
                    sbcommand.AppendFormat("mysql    --user=root --password=1 tabletpc <c:\\123.sql");
                    String command 
= sbcommand.ToString();

                    
//获取mysql.exe所在路径
                    String appDirecroty = @"C:\Program Files\MySQL\MySQL Server 5.0\bin\";

                    DialogResult result 
= MessageBox.Show("您是否真的想覆盖以前的数据库吗?那么以前的数据库数据将丢失!!!""警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    
if (result == DialogResult.Yes)
                    {
                        MySQLCmd.StartCmd(appDirecroty, command);
                        MessageBox.Show(
"数据库还原成功!");
                    }


                }

            }
            
catch (Exception ex)
            {
                MessageBox.Show(
"数据库还原失败!");
            }

        }

 

 

 另,MySQL带参数关键符号是"?"而不是"@" ,eg:


MySqlConnection conn=new MySqlConnection(连接字符串);
MySqlCommand cmd=conn.CreateCommand();
cmd.CommandText="insert into Student(Name, Uid) values(?name, ?uid);"
conn.Open();
cmd.Parameters.Add( "?name", MySql.Data.MySqlClient.MySqlDbType.VarChar).Value = "Name";
cmd.Parameters.Add( "?uid", MySql.Data.MySqlClient.MySqlDbType.VarChar).Value = "Uid";
cmd.ExecuteNonQuery();
conn.Close();