备份数据库 程序代码

#region 

备份数据库

        public bool BackUPDB(string strDbName, string strFileName)
        {
            SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();
            try
            {
                svr.Connect(ServerName, UserName, Password);
                SQLDMO.Backup bak = new SQLDMO.BackupClass();
                bak.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                bak.Initialize = true;
                bak.Files = strFileName;
                bak.Database = strDbName;
                bak.SQLBackup(svr);
                return true;
            }
            catch (Exception err)
            {
                throw (new Exception("备份数据库失败" + err.Message));
            }
            finally
            {
                svr.DisConnect();
            }
        }
        #endregion

ServerName, UserName, Password 分别为服务器名称,用户名,密码


至于用saveFileDialog保存,应该是
if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (sm.BackUPDB(数据库名称, saveFileDialog.FileName))
                {
                    MessageBox.Show("数据库备份成功!", "提示!", MessageBoxButtons.OK);
                }
            }
 

你可能感兴趣的:(程序开发)