過濾表名 & 拼接字符串

 

/// <summary>
        /// 分析sql語句中的表名
        /// </summary>                
        /// <param name="sql">sql語句</param>
        /// <returns>表名</returns>
        public string AnalyseTableName(string sql)
        {
            string tblname = "";
            try
            { 
                Regex reg = new Regex(@"(?in)\b(INTO|FROM|Update)\b(?![^\[\]]*\])\s+(?<table>\[[^\[\]]+\]|\S+)");
                MatchCollection mc = reg.Matches(sql);
                foreach (Match m in mc)
                {
                    tblname = m.Groups["table"].Value.Replace("[", "").Replace("]", "");

                    if (m.Groups["table"].Value.Contains("("))
                    {
                        tblname = m.Groups["table"].Value.Substring(0, m.Groups["table"].Value.IndexOf("("));
                    }
                    else
                    {
                        tblname = m.Groups["table"].Value;
                    } 
                } 
            }
            catch (Exception ex)
            {
                if (UseLog())
                {
                    log.Error(ex.Message.ToString());
                }
                return "";
            }
            finally
            {
                this.Close();
            }
            return tblname;
        }

 

//分析管理員增刪改操做 
        public void RecordOperation(string sql, SqlParameter[] parms, string BaseMsg)
        { 
            string UserId = "";
            string UserName = "";
            string UserType = "";
            
            #region 判斷操做人身份
            if (Session["SysAccountID"] != null)//大後臺系統管理員
            {
                UserId = Session["SysAccountID"].ToString();
                UserName = Session["SysAccountName"].ToString();
                UserType = "1";
            }
            else if (Session["UserRight"] != null)
            {
                string UserRight = Session["UserRight"].ToString();

                if (UserRight[0].ToString() == "1")  //單位初始化用戶
                {
                    UserId = Session["DepartID"].ToString();
                    UserName = GetDepartName(Session["DepartID"].ToString());
                    UserType = "2";
                }
                else if (UserRight[1].ToString() == "1") //系統管理員
                {
                    UserId = Session["UserID"].ToString();
                    UserName = Session["UserName"].ToString();
                    UserType = "3";
                }
            }
            #endregion

            if (UserName.Length > 0)
            {
                string DetailMsg = UserName;
                //BaseMsg不爲空表示直接傳入操做日誌,不須要再分析
                if (BaseMsg != "")
                {
                    DetailMsg += BaseMsg;
                    InsertRecord(UserId, UserName, DetailMsg, UserType);
                }
                else
                {
                    //須要查詢數據庫分析sql來肯定操做日誌內容
                    string tblname = AnalyseTableName(sql.Trim());
                    DataTable dt = GetRecordingMsg(tblname, sql.Trim().Substring(0, 6));
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        BaseMsg = dt.Rows[0]["BaseMsg"].ToString();
                        string ColumnName = dt.Rows[0]["ColumnName"].ToString();
                        string ColumnValue = "";
                        if (ColumnName != null && ColumnName != "")
                        {
                            //若是當前是刪除操做
                            if (sql.Trim().Substring(0, 6).ToLower() == "delete")
                            {
                                //查出delete語句中where子句的起始位置
                                int strIndex = sql.ToLower().LastIndexOf("where", sql.Length, sql.Length);
                                //拼接尋找主要信息的sql
                                string tempsql = " select " + ColumnName + " From " + tblname + " " + sql.Substring(strIndex, sql.Length - strIndex);
                                ColumnValue = db.QueryValue(tempsql, parms);
                            }
                            else
                            {
                                //插入或更新操做
                                foreach (SqlParameter p in parms)
                                {
                                    if (p.ParameterName.ToLower() == "@" + ColumnName.ToLower())
                                    {
                                        ColumnValue = p.Value.ToString();
                                        break;
                                    }
                                }
                            }
                        }
                        DetailMsg += BaseMsg.Replace("<detail>", ColumnValue); //替換當前更新記錄的重點信息
                        #region 插入操做日誌
                        InsertRecord(UserId, UserName, DetailMsg, UserType);
                        #endregion
                    }
                }
            }
            
        }
相關文章
相關標籤/搜索