Ejemplo n.º 1
0
        public bool CheckScriptGrammar(string sSql, out string sMessage)
        {
            bool flag2;

            sMessage = string.Empty;
            SQLServer server = new SQLServerClass();
            bool      flag   = false;

            try
            {
                server.Connect(this.Server, this.UserID, this.Password);
                flag = true;
                server.ExecuteImmediate(string.Format("SET PARSEONLY ON;{0}", sSql), SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
                flag2 = true;
            }
            catch (Exception exception)
            {
                sMessage = exception.Message;
                flag2    = false;
            }
            finally
            {
                if (flag)
                {
                    server.ExecuteImmediate("SET PARSEONLY OFF; ", SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
                    server.DisConnect();
                }
            }
            return(flag2);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void ExecuteSql(string sSql)
        {
            SQLServer server = new SQLServerClass();

            try
            {
                server.Connect(this.Server, this.UserID, this.Password);
                server.ExecuteImmediate(sSql, SQLDMO_EXEC_TYPE.SQLDMOExec_Default, null);
            }
            catch (Exception exception)
            {
                Log.WriteLog("执行脚本:ExecuteSql(string sSql)", exception.Message);
            }
            finally
            {
                server.DisConnect();
            }
        }
Ejemplo n.º 4
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);
        }