예제 #1
0
        public List <string> SearchScript(List <string> lstFiles, string sKeywords)
        {
            List <string> list = new List <string>();

            string[] strArray = sKeywords.Replace(",", ",").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (strArray.Length != 0)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(string.Format("/**************************************[{0}]开始查找脚本文件**************************************/\r\n", DateTime.Now.ToString()));
                builder.Append(string.Format("关键字为{0},匹配的脚本文件为\r\n", sKeywords));
                FileManagement management = new FileManagement();
                string         str        = string.Empty;
                foreach (string str2 in lstFiles)
                {
                    str = management.ReadFile(str2);
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (str.Contains(strArray[i]))
                        {
                            builder.Append(string.Format("{0}、{1}\r\n", list.Count + 1, str2));
                            list.Add(str2);
                            break;
                        }
                    }
                }
                builder.Append(string.Format("/**************************************[{0}]结束查找脚本文件**************************************/\r\n\r\n", DateTime.Now.ToString()));
                Log.WriteLog(builder.ToString());
            }
            return(list);
        }
예제 #2
0
        public bool CheckUseDatabase(List <string> lstFiles, bool blnYesOrNo)
        {
            bool           flag       = true;
            int            num        = 0;
            FileManagement management = new FileManagement();
            string         sSql       = string.Empty;

            foreach (string str2 in lstFiles)
            {
                if (".sql" == Path.GetExtension(str2).ToLower())
                {
                    sSql = management.ReadFile(str2);
                    if (blnYesOrNo)
                    {
                        if (this.CheckUseDatabase(sSql))
                        {
                            num++;
                            flag = false;
                            Log.WriteLog(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”指定了数据库名\r\n", num, DateTime.Now.ToString(), str2));
                        }
                    }
                    else if (!this.CheckUseDatabase(sSql))
                    {
                        num++;
                        flag = false;
                        Log.WriteLog(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”未指定数据库名\r\n", num, DateTime.Now.ToString(), str2));
                    }
                }
            }
            return(flag);
        }
예제 #3
0
        public int BatchExecuteSql(List <string> lstDatabases, List <string> lstFiles)
        {
            Exception      exception;
            FileManagement management = new FileManagement();
            int            num        = 0;
            StringBuilder  builder    = new StringBuilder();

            builder.Append(string.Format("/**************************************[{0}]开始批量执行脚本**************************************/\r\n", DateTime.Now.ToString()));
            SQLServer server = new SQLServerClass();

            try
            {
                server.Connect(this.Server, this.UserID, this.Password);
                string command = string.Empty;
                foreach (string str2 in lstDatabases)
                {
                    builder.Append(string.Format("\r\n[{0}] 数据库[{1}]准备升级\r\n", DateTime.Now.ToString(), str2));
                    foreach (string str3 in lstFiles)
                    {
                        try
                        {
                            command = string.Format("USE {0}\r\n{1}", str2, management.ReadFile(str3));
                            server.ExecuteImmediate(command, SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
                        }
                        catch (Exception exception1)
                        {
                            exception = exception1;
                            num++;
                            builder.Append(string.Format("\r\n{0}、[{1}]:{2} 脚本文件异常\r\n异常信息为:{3}\r\n", new object[] { num, DateTime.Now.ToString(), str3, exception.Message }));
                        }
                    }
                    builder.Append(string.Format("\r\n[{0}] 数据库[{1}]升级完毕\r\n", DateTime.Now.ToString(), str2));
                }
                builder.Append(string.Format("\r\n/**************************************[{0}]结束批量执行脚本**************************************/\r\n\r\n", DateTime.Now.ToString()));
                Log.WriteLog(builder.ToString());
            }
            catch (Exception exception2)
            {
                exception = exception2;
                num       = -1;
                Log.WriteLog("批量执行数据库升级脚本:BatchExecuteSql(List<string> lstDatabases, List<string> lstFiles) ", exception.Message);
                return(num);
            }
            finally
            {
                server.DisConnect();
                GC.Collect();
            }
            return(num);
        }
예제 #4
0
        public List <string> GetDataBases(List <string> lstFiles)
        {
            List <string>  list       = new List <string>();
            FileManagement management = new FileManagement();
            string         sSql       = string.Empty;

            foreach (string str2 in lstFiles)
            {
                if (".sql" == Path.GetExtension(str2).ToLower())
                {
                    List <string> list2;
                    sSql = management.ReadFile(str2);
                    this.CheckUseDatabase(sSql, out list2);
                    foreach (string str3 in list2)
                    {
                        if (!list.Contains(str3.ToLower()))
                        {
                            list.Add(str3.ToLower());
                        }
                    }
                }
            }
            return(list);
        }
예제 #5
0
파일: Log.cs 프로젝트: findersky/SqlTool
 public static void OpenLog()
 {
     FileManagement.OpenFile(LogFilePath, LogFileName);
 }
예제 #6
0
        public int BatchAnalysisScript(List <string> lstFiles, bool blnCheckDataBase, bool blnYesOrNo)
        {
            Exception      exception;
            FileManagement management = new FileManagement();
            string         sSql       = string.Empty;
            int            num        = 0;
            StringBuilder  builder    = new StringBuilder();

            builder.Append(string.Format("/**************************************[{0}]开始批量分析脚本**************************************/\r\n", DateTime.Now.ToString()));
            SQLServer server = new SQLServerClass();

            try
            {
                server.Connect(this.Server, this.UserID, this.Password);
                foreach (string str2 in lstFiles)
                {
                    if (Path.GetExtension(str2) != ".sql")
                    {
                        num++;
                        builder.Append(string.Format("\r\n{0}、[{1}]:{2} 非脚本文件\r\n", num, DateTime.Now.ToString(), str2));
                    }
                    else
                    {
                        try
                        {
                            sSql = management.ReadFile(str2);
                            if (blnCheckDataBase)
                            {
                                if (blnYesOrNo)
                                {
                                    if (this.CheckUseDatabase(sSql))
                                    {
                                        num++;
                                        builder.Append(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”指定了数据库名\r\n", num, DateTime.Now.ToString(), str2));
                                    }
                                }
                                else if (!this.CheckUseDatabase(sSql))
                                {
                                    num++;
                                    builder.Append(string.Format("\r\n{0}、[{1}] 脚本文件“{2}”未指定数据库名\r\n", num, DateTime.Now.ToString(), str2));
                                }
                            }
                            server.ExecuteImmediate(string.Format("SET PARSEONLY ON;{0}", sSql), SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
                        }
                        catch (Exception exception1)
                        {
                            exception = exception1;
                            num++;
                            builder.Append(string.Format("\r\n{0}、[{1}]:{2} 脚本文件异常\r\n异常信息为:{3}\r\n", new object[] { num, DateTime.Now.ToString(), str2, exception.Message }));
                        }
                        finally
                        {
                            server.ExecuteImmediate("SET PARSEONLY OFF; ", SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
                        }
                    }
                }
                builder.Append(string.Format("\r\n/**************************************[{0}]结束批量分析脚本**************************************/\r\n\r\n", DateTime.Now.ToString()));
                Log.WriteLog(builder.ToString());
            }
            catch (Exception exception2)
            {
                exception = exception2;
                num       = -1;
                Log.WriteLog("批量分析脚本:BatchAnalysisScript(string lstFiles, bool blnCheckDataBase, bool blnYesOrNo)", exception.Message);
                return(num);
            }
            finally
            {
                server.DisConnect();
                GC.Collect();
            }
            return(num);
        }