做项目代码 练习代码 记录错误使用:
读取INI代码 做的不对 练习 记录错误使用:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace GHGD.UI
{
    public partial class FrmBackUp : Form
    {
        public FrmBackUp()
        {
            InitializeComponent();
        }
        #region "声明变量"
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">节点名称[如[TypeName]]</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">键</param>
        /// <param name="def">值</param>
        /// <param name="retval">stringbulider对象</param>
        /// <param name="size">字节大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
        //private string strFilePath = Application.StartupPath + "\\" + "FileConfig.ini";//获取INI文件路径
        string strBUIni = Application.StartupPath + "\\" + "BackUpIni.ini";
        #endregion
        /// <summary>
        /// 自定义读取INI文件中的内容方法
        /// </summary>
        /// <param name="Section">键</param>
        /// <param name="key">值</param>
        /// <returns></returns>
        private string ContentValue(string Section, string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            int i = GetPrivateProfileString(Section, key, "", temp, 1024, strBUIni);
            return temp.ToString();
        }
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.strBUIni);
            return temp.ToString();
        }  
        BUIni buIni = new BUIni();
        
        private void FrmBackUp_Load(object sender, EventArgs e)
        {
            // FrmBackUp窗体 初始化INI数据
            //string strBUIni = Application.StartupPath + "\\" + "BackUpIni.ini";
            MessageBox.Show(strBUIni);
            if (File.Exists(strBUIni))
            {
                MessageBox.Show("if");
                txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
                MessageBox.Show(txt_ServerName.Text);
                txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
                txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
                txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
                txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath"); 
            }
            else {
                MessageBox.Show("else");
                // 加载 INI config.ini配置文件 第二模块 FileConfig 系统设置
                //string str = buIni.IniReadValue("FileConfig", "Mode");
                //MessageBox.Show("Mode: " + str);
                FileInfo fileInfo = new FileInfo(strBUIni);
                if ((!fileInfo.Exists))
                {
                    //文件不存在,建立文件 System.Text.Encoding.Default ,Encoding.GetEncoding("GB2312"),UnicodeEncoding.GetEncoding("GBK")
                    // Encoding.GetEncoding("GB2312") 这里设置为是插入的汉字做防乱码设置
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strBUIni, false, Encoding.GetEncoding("GB2312"));
                    try
                    {
                        sw.Write("#备份文件设置");
                        sw.Close();
                    }
                    catch
                    {
                        throw (new ApplicationException("Ini文件不存在"));
                    }
                }
                //[BackUpIni]
                //ServerName = 
                //DatabaseName = 
                //UserName = 
                //Password = 
                //BackUpPath = 
                strBUIni = fileInfo.FullName;
                //txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
                //txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
                //txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
                //txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
                //txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath");    
   
                txt_ServerName.Text =   IniReadValue("BackUpIni", "ServerName");
                txt_DatabaseName.Text = IniReadValue("BackUpIni", "DatabaseName");
                txt_UserName.Text =     IniReadValue("BackUpIni", "UserName");
                txt_Password.Text =     IniReadValue("BackUpIni", "Password");
                txt_BackUpPath.Text =   IniReadValue("BackUpIni", "BackUpPath");
            }
            //// tabPage6 初始化选项卡
            ////MessageBox.Show(strFilePath);
            //if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
            //{
            //    MessageBox.Show("if");
            //    txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
            //    MessageBox.Show(txt_ServerName.Text);
            //    txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
            //    txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
            //    txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
            //    txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath"); 
            //}
            //else
            //{
            //    MessageBox.Show("else");
            //    // 加载 INI config.ini配置文件 第二模块 FileConfig 系统设置
            //    //string str = buIni.IniReadValue("FileConfig", "Mode");
            //    //MessageBox.Show("Mode: " + str);
            //    FileInfo fileInfo = new FileInfo(strBUIni);
            //    if ((!fileInfo.Exists))
            //    {
            //        //文件不存在,建立文件 System.Text.Encoding.Default ,Encoding.GetEncoding("GB2312"),UnicodeEncoding.GetEncoding("GBK")
            //        // Encoding.GetEncoding("GB2312") 这里设置为是插入的汉字做防乱码设置
            //        System.IO.StreamWriter sw = new System.IO.StreamWriter(strBUIni, false, Encoding.GetEncoding("GB2312"));
            //        try
            //        {
            //            sw.Write("#备份文件设置");
            //            sw.Close();
            //        }
            //        catch
            //        {
            //            throw (new ApplicationException("Ini文件不存在"));
            //        }
            //    }
            //    //[BackUpIni]
            //    //ServerName = 
            //    //DatabaseName = 
            //    //UserName = 
            //    //Password = 
            //    //BackUpPath = 
            //    strBUIni = fileInfo.FullName;
            //    //txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
            //    //txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
            //    //txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
            //    //txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
            //    //txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath");    
            //    txt_ServerName.Text = IniReadValue("BackUpIni", "ServerName");
            //    txt_DatabaseName.Text = IniReadValue("BackUpIni", "DatabaseName");
            //    txt_UserName.Text = IniReadValue("BackUpIni", "UserName");
            //    txt_Password.Text = IniReadValue("BackUpIni", "Password");
            //    txt_BackUpPath.Text = IniReadValue("BackUpIni", "BackUpPath");
            //}
        }
        private void btn_BackUp_ok_Click(object sender, EventArgs e)
        {
            //根据INI文件名设置要写入INI文件的节点名称
            //此处的节点名称完全可以根据实际需要进行配置
            //strSec = Path.GetFileNameWithoutExtension(strFilePath); // 获取文件名称去掉扩展名
            //ServerName =
            //DatabaseName =
            //UserName =
            //Password =
            //BackUpPath = 
            //BUIni.WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text, strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strBUIni);
//MessageBox.Show("保存成功!");
            try
            {
                //根据INI文件名设置要写入INI文件的节点名称
                //此处的节点名称完全可以根据实际需要进行配置
                
                MessageBox.Show(strBUIni);
                 WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strBUIni);
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            this.Close();
        }
    }
}
------------------------------------------------------------------------------------------------------------------
成功了,把代码帖出来给
大家看看,跟我刚开始帖出来的差不多,是需要杀掉进程的,我之前调用的存储过程,可能有点问题,现在改成sql了
/// <summary>
        /// 数据库备份
        /// </summary>
        public static int DbBackup(string serverip, string username, string psw, string path)
        {
            int ret;
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(serverip, username, psw);
                oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.Database = "BookLibrarySystem";
                oBackup.Files = path + "\\BookLibrarySystem.bak"; ;
                oBackup.BackupSetName = "BookLibrarySystem";
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);
                ret = 1;
            }
            catch
            {
                ret = 0;
                throw;
            }
            finally
            {
                oSQLServer.DisConnect();
            }
            return ret;
        }
        /// <summary>
        /// 数据库恢复
        /// </summary>
        public static int DbRestore(string serverip, string username, string psw, string path)
        {
            int ret;
            SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                exepro(serverip, username, psw);
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(serverip, username, psw);
                oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                oRestore.Database = "BookLibrarySystem";
                oRestore.Files = path + "\\BookLibrarySystem.bak";
                oRestore.FileNumber = 1;
                oRestore.ReplaceDatabase = true;
                oRestore.SQLRestore(oSQLServer);
                ret = 1;
            }
            catch
            {
                ret = 0;
                throw;
            }
            finally
            {
                oSQLServer.DisConnect();
            }
            return ret;
        }
        /**/
        /// <summary>
        /// 杀死当前库的所有进程
        /// </summary>
        /// <returns></returns>
        private static bool exepro(string serverip, string username, string psw)
        {
SqlConnection conn1 = new SqlConnection("server=" + serverip + ";uid=" + username + ";pwd=" + psw + ";database=master");
            string cmdTxt = "use master;";
            cmdTxt += " declare  @sql nvarchar(500) declare @spid int set @sql='declare getspid  cursor for ";
            cmdTxt += " select spid from sysprocesses where dbid=db_id('''+@dbname+''')' exec (@sql) ";
            cmdTxt += " open getspid fetch next from getspid into @spid  while @@fetch_status <>-1 begin ";
            cmdTxt += " exec('kill '+@spid) fetch next from getspid into @spid end close getspid deallocate getspid ";
            //SqlCommand cmd = new SqlCommand("p_killspid", conn1);
            SqlCommand cmd = new SqlCommand(cmdTxt, conn1);
            cmd.Parameters.Add(new SqlParameter("@dbname", "BookLibrarySystem"));
            cmd.CommandType = CommandType.Text;
            try
            {
                conn1.Open();
                SqlDataReader 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), conn1);
                    cmd.ExecuteNonQuery();
                }
                conn1.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                conn1.Close();
            }
        }
	-------------------------------------------------------------------------------
	[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#10701
  Message=[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
  Source=Microsoft SQL-DMO (ODBC SQLState: 42000)
  ErrorCode=-2147218303
  StackTrace:
       在 SQLDMO.BackupClass.SQLBackup(_SQLServer ServerObject)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
----------------------------------------------------------- 下面是正则表达式
private void txt_TimeLimit_Leave(object sender, EventArgs e)
        {
             if (Isshenfenzheng(txt_TimeLimit.Text.Trim()) == true)
             {
                 if (txt_TimeLimit.Text.Trim().Length <= 14)
                 {
                 }
                 else
                 {
                     MessageBox.Show("长度必须小于" + "位");
                 }
             }
             else
             {
                 MessageBox.Show("输入的字符不符合规则");
                 this.txt_TimeLimit.Focus();
                 return ;
             }
        }
   public class RegularExpression
    {
        public bool Isshenfenzheng(string sfz)
        {
            // ^(\d+|(\d+\.\d+))\d*$
            // ^([1-9]+|(\d+\.\d+))\d*$
            // ^([1-9]\d+|(\d+\.\d+))*$
            // @"^(0|([1-9]\d*)|((\d+\.\d+))\d*)$"
            //  @"^([1-9]\d+|(\d+\.\d+))*$"
            // @"^(0|([1-9]\d*)|((\d+\.\d{1,2}))*)$"
            // @"^(0|([1-9]\d*)|((\d+\.\d+))\d*)$"
            bool s = System.Text.RegularExpressions.Regex.IsMatch(sfz, @"^(0|([1-9]\d*)|((\d+\.\d{1,2}))*)$");
            return s;
        }
    }
    ------------------------------------------------ 鼠标按下事件 触发
       //private void txt_TimeLimit_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    //阻止从键盘输入键
        //    e.Handled = true;
        //    if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
        //    {
        //        if ((e.KeyChar == (char)8)) { e.Handled = false; return; }
        //        else
        //        {
        //            int len = txt_TimeLimit.Text.Length;
        //            if (len < 9)
        //            {
        //                if (len == 0 && e.KeyChar != '0')
        //                {
        //                    e.Handled = false; return;
        //                }
        //                else if (len == 0)
        //                {
        //                    MessageBox.Show("编号不能以0开头!"); return;
        //                }
        //                e.Handled = false; return;
        //            }
        //            else
        //            {
        //                MessageBox.Show("编号最多只能输入" + len + "位数字!");
        //            }
        //        }
        //    }
        //    else
        //    {
        //        MessageBox.Show("编号只能输入数字!");
        //    }
        //}
        //protected bool Isshenfenzheng(string sfz)
        //{
        //    // "^(0|([1-9]\d*)|((\d+\.\d+))\d*)$"  // 允许多位小数点
        //    bool s = System.Text.RegularExpressions.Regex.IsMatch(sfz, @"^(0|([1-9]\d*)|((\d+\.\d{1,2}))*)$");
        //    return s;
        //}
	----------------------------------------------------------------------操作打开文件对话框 txt_CLSku路径  txt_PLSku路径
	 //private void button2_Click_1(object sender, EventArgs e)
        //{
        //    String fileName;
        //    OpenFileDialog ofd = new OpenFileDialog();
        //    ofd.Filter = "文本文件(*.txt)|*.txt";
        //    ofd.ShowDialog();
        //    fileName = ofd.FileName;
        //    txt_CLSku.Text = fileName;
        //}
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    String fileName;  
        //    OpenFileDialog ofd = new OpenFileDialog();
        //    ofd.Filter = "文本文件(*.txt)|*.txt";
        //    ofd.ShowDialog();
        //    fileName = ofd.FileName;
        //    txt_PLSku.Text = fileName;
        //}
-------------------------------------------------------------------------------------------------
读取INI文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace GHGD.UI
{
    public partial class FrmBackUp : Form
    {
        public FrmBackUp()
        {
            InitializeComponent();
        }
        #region "声明变量"
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">节点名称[如[TypeName]]</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">键</param>
        /// <param name="def">值</param>
        /// <param name="retval">stringbulider对象</param>
        /// <param name="size">字节大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
        //private string strFilePath = Application.StartupPath + "\\" + "FileConfig.ini";//获取INI文件路径
        string strBUIni = Application.StartupPath + "\\" + "BackUpIni.ini";
        #endregion
        /// <summary>
        /// 自定义读取INI文件中的内容方法
        /// </summary>
        /// <param name="Section">键</param>
        /// <param name="key">值</param>
        /// <returns></returns>
        private string ContentValue(string Section, string key)
        {
            StringBuilder temp = new StringBuilder(1024);
            int i = GetPrivateProfileString(Section, key, "", temp, 1024, strBUIni);
            return temp.ToString();
        }
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.strBUIni);
            return temp.ToString();
        }  
        BUIni buIni = new BUIni();
        
        private void FrmBackUp_Load(object sender, EventArgs e)
        {
            // FrmBackUp窗体 初始化INI数据
            //string strBUIni = Application.StartupPath + "\\" + "BackUpIni.ini";
            MessageBox.Show(strBUIni);
            if (File.Exists(strBUIni))
            {
                MessageBox.Show("if");
                txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
                MessageBox.Show(txt_ServerName.Text);
                txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
                txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
                txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
                txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath"); 
            }
            else {
                MessageBox.Show("else");
                // 加载 INI config.ini配置文件 第二模块 FileConfig 系统设置
                //string str = buIni.IniReadValue("FileConfig", "Mode");
                //MessageBox.Show("Mode: " + str);
                FileInfo fileInfo = new FileInfo(strBUIni);
                if ((!fileInfo.Exists))
                {
                    //文件不存在,建立文件 System.Text.Encoding.Default ,Encoding.GetEncoding("GB2312"),UnicodeEncoding.GetEncoding("GBK")
                    // Encoding.GetEncoding("GB2312") 这里设置为是插入的汉字做防乱码设置
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strBUIni, false, Encoding.GetEncoding("GB2312"));
                    try
                    {
                        sw.Write("#备份文件设置");
                        sw.Close();
                    }
                    catch
                    {
                        throw (new ApplicationException("Ini文件不存在"));
                    }
                }
                //[BackUpIni]
                //ServerName = 
                //DatabaseName = 
                //UserName = 
                //Password = 
                //BackUpPath = 
                strBUIni = fileInfo.FullName;
                //txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
                //txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
                //txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
                //txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
                //txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath");    
   
                txt_ServerName.Text =   IniReadValue("BackUpIni", "ServerName");
                txt_DatabaseName.Text = IniReadValue("BackUpIni", "DatabaseName");
                txt_UserName.Text =     IniReadValue("BackUpIni", "UserName");
                txt_Password.Text =     IniReadValue("BackUpIni", "Password");
                txt_BackUpPath.Text =   IniReadValue("BackUpIni", "BackUpPath");
            }
            //// tabPage6 初始化选项卡
            ////MessageBox.Show(strFilePath);
            //if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
            //{
            //    MessageBox.Show("if");
            //    txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
            //    MessageBox.Show(txt_ServerName.Text);
            //    txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
            //    txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
            //    txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
            //    txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath"); 
            //}
            //else
            //{
            //    MessageBox.Show("else");
            //    // 加载 INI config.ini配置文件 第二模块 FileConfig 系统设置
            //    //string str = buIni.IniReadValue("FileConfig", "Mode");
            //    //MessageBox.Show("Mode: " + str);
            //    FileInfo fileInfo = new FileInfo(strBUIni);
            //    if ((!fileInfo.Exists))
            //    {
            //        //文件不存在,建立文件 System.Text.Encoding.Default ,Encoding.GetEncoding("GB2312"),UnicodeEncoding.GetEncoding("GBK")
            //        // Encoding.GetEncoding("GB2312") 这里设置为是插入的汉字做防乱码设置
            //        System.IO.StreamWriter sw = new System.IO.StreamWriter(strBUIni, false, Encoding.GetEncoding("GB2312"));
            //        try
            //        {
            //            sw.Write("#备份文件设置");
            //            sw.Close();
            //        }
            //        catch
            //        {
            //            throw (new ApplicationException("Ini文件不存在"));
            //        }
            //    }
            //    //[BackUpIni]
            //    //ServerName = 
            //    //DatabaseName = 
            //    //UserName = 
            //    //Password = 
            //    //BackUpPath = 
            //    strBUIni = fileInfo.FullName;
            //    //txt_ServerName.Text = buIni.IniReadValue("BackUpIni", "ServerName");
            //    //txt_DatabaseName.Text = buIni.IniReadValue("BackUpIni", "DatabaseName");
            //    //txt_UserName.Text = buIni.IniReadValue("BackUpIni", "UserName");
            //    //txt_Password.Text = buIni.IniReadValue("BackUpIni", "Password");
            //    //txt_BackUpPath.Text = buIni.IniReadValue("BackUpIni", "BackUpPath");    
            //    txt_ServerName.Text = IniReadValue("BackUpIni", "ServerName");
            //    txt_DatabaseName.Text = IniReadValue("BackUpIni", "DatabaseName");
            //    txt_UserName.Text = IniReadValue("BackUpIni", "UserName");
            //    txt_Password.Text = IniReadValue("BackUpIni", "Password");
            //    txt_BackUpPath.Text = IniReadValue("BackUpIni", "BackUpPath");
            //}
        }
        private void btn_BackUp_ok_Click(object sender, EventArgs e)
        {
            //根据INI文件名设置要写入INI文件的节点名称
            //此处的节点名称完全可以根据实际需要进行配置
            //strSec = Path.GetFileNameWithoutExtension(strFilePath); // 获取文件名称去掉扩展名
            //ServerName =
            //DatabaseName =
            //UserName =
            //Password =
            //BackUpPath = 
            //BUIni.WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text, strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strBUIni);
            //BUIni.WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strBUIni);
//MessageBox.Show("保存成功!");
            try
            {
                //根据INI文件名设置要写入INI文件的节点名称
                //此处的节点名称完全可以根据实际需要进行配置
                
                MessageBox.Show(strBUIni);
                 WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strBUIni);
                 WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strBUIni);
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            this.Close();
        }
    }
}
-----------------------------------------------------------------读取文件报错
-----------------------------------------------------------------192.168.0.111\\SQL2005 报错
[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#700
  Message=[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()).
  Source=Microsoft SQL-DMO (ODBC SQLState: 08001)
  ErrorCode=-2147221504
  StackTrace:
       在 SQLDMO.SQLServerClass.Connect(Object ServerName, Object Login, Object Password)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
-----------------------------------------------------------------ADMIN-PC\\MGX2005 报错
[Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server 不存在或访问被拒绝
[Microsoft][ODBC SQL Server Driver][DBMSLPCN]ConnectionOpen (Connect()).
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#700
  Message=[Microsoft][ODBC SQL Server Driver][DBMSLPCN]SQL Server 不存在或访问被拒绝
[Microsoft][ODBC SQL Server Driver][DBMSLPCN]ConnectionOpen (Connect()).
  Source=Microsoft SQL-DMO (ODBC SQLState: 08001)
  ErrorCode=-2147221504
  StackTrace:
       在 SQLDMO.SQLServerClass.Connect(Object ServerName, Object Login, Object Password)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
-----------------------------------------------------------------192.168.0.111\SQL2005 报错
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#10701
  Message=[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
  Source=Microsoft SQL-DMO (ODBC SQLState: 42000)
  ErrorCode=-2147218303
  StackTrace:
       在 SQLDMO.BackupClass.SQLBackup(_SQLServer ServerObject)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
-----------------------------------------------------------------ADMIN-PC 报错
  [Microsoft][ODBC SQL Server Driver][SQL Server]用户 'sa' 登录失败。
  未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#700
  Message=[Microsoft][ODBC SQL Server Driver][SQL Server]用户 'sa' 登录失败。
  Source=Microsoft SQL-DMO (ODBC SQLState: 28000)
  ErrorCode=-2147203048
  StackTrace:
       在 SQLDMO.SQLServerClass.Connect(Object ServerName, Object Login, Object Password)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
-----------------------------------------------------------------192.168.0.111 报错
[Microsoft][ODBC SQL Server Driver][DBNETLIB]无效的连接。
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()).
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#700
  Message=[Microsoft][ODBC SQL Server Driver][DBNETLIB]无效的连接。
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()).
  Source=Microsoft SQL-DMO (ODBC SQLState: 08001)
  ErrorCode=-2147221504
  StackTrace:
       在 SQLDMO.SQLServerClass.Connect(Object ServerName, Object Login, Object Password)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
-----------------------------------------------------------------LANTRACK-950EF1\SQL2005
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#10701
  Message=[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open backup device 'C:\Users\admin\Desktop\GHGD_bak3.bak'. Operating system error 3(系统找不到指定的路径。).
[Microsoft][ODBC SQL Server Driver][SQL Server]BACKUP DATABASE is terminating abnormally.
  Source=Microsoft SQL-DMO (ODBC SQLState: 42000)
  ErrorCode=-2147218303
  StackTrace:
       在 SQLDMO.BackupClass.SQLBackup(_SQLServer ServerObject)
       在 GHGD.UI.FrmBackUpFile.DbBackup(String serverip, String username, String psw, String dbName, String path, String strBackUpFileName) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 238
       在 GHGD.UI.FrmBackUpFile.btn_BackUp_File_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 144
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
---------------------------------------------------------使用自己的方式做的恢复 报错
[Microsoft][ODBC SQL Server Driver][SQL Server]Exclusive access could not be obtained because the database is in use.
[Microsoft][ODBC SQL Server Driver][SQL Server]RESTORE DATABASE is terminating abnormally.
未处理 System.Runtime.InteropServices.COMException
  HelpLink=SQLDMO80.hlp#16901
  Message=[Microsoft][ODBC SQL Server Driver][SQL Server]Exclusive access could not be obtained because the database is in use.
[Microsoft][ODBC SQL Server Driver][SQL Server]RESTORE DATABASE is terminating abnormally.
  Source=Microsoft SQL-DMO (ODBC SQLState: 42000)
  ErrorCode=-2147218403
  StackTrace:
       在 SQLDMO.RestoreClass.SQLRestore(_SQLServer ServerObject)
       在 GHGD.UI.FrmBackUpFile.btn_HuanYuan_BackUpRestore_Click(Object sender, EventArgs e) 位置 D:\GHGD\UI\FrmBackUpFile.cs:行号 208
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 GHGD.UI.Program.Main() 位置 D:\GHGD\UI\Program.cs:行号 87
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
  
  
//[ConnectionInfo]
//ServerName=192.168.0.111
// 192.168.0.111\sql2005
//UserName=sa
//Password=111
//DBName=GHDG
//BackUpPath=123
// ADMIN-PC\MGX2005
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号