歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

C#批量附加指定目錄下的所有數據庫文件到數據庫中

應用場合:因為經常更換操作系統,所以D盤存放數據庫文件目錄的數據庫每次都要一個一個的附加到MSSQL中,因此設計程序批量附加省時間也方便自己和大家。

程序不足:沒有去研究跟實現NDF日志文件附加和多個日志文件的數據庫附加。

程序源碼:


        /// <summary>
        /// 循環查找指定目錄下要附加的數據庫文件和對應的日志文件,連接本地數據庫並執行數據庫附加命令
        /// </summary>
        private void AttachFolderDB()
        {
            string strFileFolder = "";
            FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
            myFolderBrowserDialog.ShowDialog();
            if (myFolderBrowserDialog.SelectedPath != "")
            {
                strFileFolder = myFolderBrowserDialog.SelectedPath;
            }
            //查找所有的MDF文件列表
            string[] arrAttachFilePath = null;
            if (strFileFolder != "")
            {
                DirectoryInfo dir = new DirectoryInfo(strFileFolder);
                //判斷目錄下是否存在主數據庫文件
                FileInfo[] finfo = dir.GetFiles("*.mdf");
                if (finfo.Length > 0)
                {
                    arrAttachFilePath = new string[finfo.Length];
                    if (finfo.Length > 0)
                    {
                        int i = 0;
                        foreach (FileInfo f in finfo)
                        {
                            arrAttachFilePath[i] = f.FullName;
                            i = i + 1;
                        }
                    }
                }

            }
            //循環附加數據庫
            if (arrAttachFilePath != null)
            {
                for (int i = 0; i < arrAttachFilePath.Length; i++)
                {
                    string strFile = arrAttachFilePath[i].ToString();
                    string strMdfFilePath = arrAttachFilePath[i].ToString();//mdf路徑
                    string strLogFilePath = "";//日志文件路徑
                    string strLdfFilePath = "";//日志文件路徑
                    string strDataFileName = strMdfFilePath.Substring(strMdfFilePath.LastIndexOf("\\") + 1, strMdfFilePath.Length - strMdfFilePath.LastIndexOf("\\") - 1);
                    strDataFileName = strDataFileName.Remove(strDataFileName.LastIndexOf("."));

                    string logIndex = "_Data";
                    int n = strDataFileName.IndexOf(logIndex);

                    if (n == -1)
                    {
                        strLogFilePath = strMdfFilePath.Remove(strMdfFilePath.LastIndexOf("\\")) + "\\" + strDataFileName + "_log.ldf";
                        strLdfFilePath = strMdfFilePath.Remove(strMdfFilePath.LastIndexOf("\\")) + "\\" + strDataFileName + ".ldf";
                    }
                    else
                    {
                        strDataFileName = strDataFileName.Remove(strDataFileName.LastIndexOf("_"));
                        strLogFilePath = strMdfFilePath.Remove(strMdfFilePath.LastIndexOf("\\")) + "\\" + strDataFileName + "_log.ldf";
                        strLdfFilePath = strMdfFilePath.Remove(strMdfFilePath.LastIndexOf("\\")) + "\\" + strDataFileName + ".ldf";
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.Append("sp_attach_db @dbname='" + strDataFileName + "',@filename1='" + strMdfFilePath + "'");
                    if (System.IO.File.Exists(strLogFilePath))
                    {
                        sb.Append(",@filename2='" + strLogFilePath + "'");
                        AttachDataBase(sb.ToString());
                    }
                    else if (System.IO.File.Exists(strLdfFilePath))
                    {
                        sb.Append(",@filename2='" + strLdfFilePath + "'");
                        AttachDataBase(sb.ToString());
                    }
                    else
                    {
                        Console.WriteLine("數據庫文件" + strMdfFilePath + "缺少必備的日志文件!");
                    }
                }
            }
        }
        /// <summary>
        /// 連接數據庫並執行附加對應的數據庫文件命令
        /// </summary>
        /// <param name="strSql">附加數據庫命令字符串</param>
        /// <returns></returns>
        private bool AttachDataBase(string strSql)
        {
            SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=master;Integrated Security=True");
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = strSql;
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                //如果數據庫中存在名為要添加的數據庫時則拋出異常
                Console.WriteLine("附加數據庫時異常:" + ex.Message);
                return false;
            }
            finally
            {
                con.Close();
            }
        }

Copyright © Linux教程網 All Rights Reserved