我用C#写了个备份的程序,可以用来备份sql数据库,可是在备份远程(局域网)sql数据库的时候,备份文件是备份在服务器上的,怎么让他备份到本地呢?或者将服务器上的备份文件copy到本地?我是菜鸟,,请高手指教!!谢谢!!

解决方案 »

  1.   

    参考一下吧,好像只能备份到服务器上再copy到本地。
    http://topic.csdn.net/t/20031013/08/2348216.html
      

  2.   


    private   void   BackUpSQLServer() 
                    { 
                            string   str   =   strPath   +   "\\ "   +   DateTime.Now.Year.ToString()   +   DateTime.Now.Month.ToString()   +   DateTime.Now.Day.ToString(); 
                            string   strConnectstring   =   "Server= "   +   this.textBox2.Text.Trim()   +   ";Database=Master;User   ID= "   +   this.textBox4.Text.Trim()   +   ";Password= "   +   textBox4.Text.Trim()   +   "; "; 
                            SqlConnection   conn   =   new   SqlConnection(strConnectstring);                         SqlCommand   cmdBK   =   new   SqlCommand(); 
                            cmdBK.CommandType   =   CommandType.Text; 
                            cmdBK.Connection   =   conn; 
                            cmdBK.CommandText   =   @ "backup   database   "   +   textBox3.Text.Trim()   +   "   to   disk= ' "   +   str   +   " '   with   init ";                         try 
                            { 
                                    conn.Open(); 
                                    cmdBK.ExecuteNonQuery(); 
                                    this.richTextBox1.Text   =   "备份成功\n文件路径为: "   +   str   +   "\n操作时间:\n "   +   DateTime.Now.ToString();                                 this.richTextBox1.ForeColor   =   Color.Blue; 
                            } 
                            catch   (Exception   ex) 
                            { 
                                    this.richTextBox1.Text   =   "备份失败\n失败原因\n "   +   ex.ToString()   +   "\n操作时间\n "   +   DateTime.Now.ToString(); 
                                    this.richTextBox1.ForeColor   =   Color.Red; 
                            } 
                            finally 
                            { 
                                    conn.Close(); 
                                    conn.Dispose(); 
                            } 
                    }                 private   void   RestoreSQLServer() 
                    { 
                            string   strConnectstring   =   "Server= "   +   this.textBox2.Text.Trim()   +   ";Database=Master;User   ID= "   +   this.textBox4.Text.Trim()   +   ";Password= "   +   textBox5.Text.Trim()   +   "; "; 
                            SqlConnection   conn   =   new   SqlConnection(strConnectstring); 
                            conn.Open();                         //KILL   DataBase   Process   
                            SqlCommand   cmd   =   new   SqlCommand( "SELECT   spid   FROM   dbo.sysprocesses   ,dbo.sysdatabases   WHERE   dbo.sysprocesses.dbid=dbo.sysdatabases.dbid   AND   dbo.sysdatabases.Name= ' "   +   textBox3.Text.Trim()   +   " ' ",   conn); 
                            SqlDataReader   dr; 
                            dr   =   cmd.ExecuteReader(); 
                            ArrayList   list   =   new   ArrayList(); 
                            while   (dr.Read()) 
                            { 
                                    list.Add(dr.GetInt16(0)); 
                            } 
                            dr.Close(); 
                            for   (int   i   =   0;   i   <   list.Count;   i++) 
                            { 
                                    cmd   =   new   SqlCommand(string.Format( "KILL   {0} ",   list[i]),   conn); 
                                    cmd.ExecuteNonQuery(); 
                            }                         SqlCommand   cmdRT   =   new   SqlCommand(); 
                            cmdRT.CommandType   =   CommandType.Text; 
                            cmdRT.Connection   =   conn; 
                            cmdRT.CommandText   =   @ "restore   database   "   +   textBox3.Text.Trim()   +   "     from   disk= ' "   +   strPath   +   " ' ";                         try 
                            { 
                                    cmdRT.ExecuteNonQuery(); 
                                    this.richTextBox1.Text   =   "还原成功\n文件路径为: "   +   strPath   +   "\n操作时间:\n "   +   DateTime.Now.ToString(); 
                                    this.richTextBox1.ForeColor   =   Color.Blue; 
                            } 
                            catch   (Exception   ex) 
                            { 
                                    this.richTextBox1.Text   =   "还原失败\n失败原因\n "   +   ex.ToString()   +   "\n操作时间:\n "   +   DateTime.Now.ToString(); 
                                    this.richTextBox1.ForeColor   =   Color.Red; 
                            } 
                            finally 
                            { 
                                    conn.Close(); 
                            }   
                    } 
      

  3.   

    给你个备份数据库的代码,参考下把:
    private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    string strg = Application.StartupPath.ToString();
                    strg = strg.Substring(0, strg.LastIndexOf("\\"));
                    strg = strg.Substring(0, strg.LastIndexOf("\\"));
                    strg += @"\Data";
                    string sqltxt = @"BACKUP DATABASE db_MrCy TO Disk='" + strg + "\\" + txtpath.Text +".bak"+ "'";
                    SqlConnection conn = BaseClass.DBConn.CyCon();
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqltxt, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
                    {
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }        }
      

  4.   

    哦!我已经备份好了就是想知道怎么样把sql服务器上的备份文件copy到本地.......................