private IDbCommand CreateCommand(DbMetaLog dblog, IDbConnection conn) { string fields = ""; string vals = ""; DbParameter[] dbParameters = new DbParameter[dblog.FiledValues.Count]; int i = 0; foreach (string key in dblog.FiledValues.Keys) { fields += string.Format("{0},", key); vals += string.Format("@{0},", key); dbParameters[i] = this.CreateDbParameter(dblog.DbDialect, "@" + key, dblog.FiledValues[key]); i++; } fields = fields.Trim(','); vals = vals.Trim(','); string commandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", dblog.DestTable, fields, vals); IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; this.AttachParameters(cmd, dbParameters); return(cmd); }
private IDbCommand CreateCommand(DbMetaLog dblog, IDbConnection conn) { string fields = ""; string vals = ""; DbParameter[] dbParameters = new DbParameter[dblog.FiledValues.Count]; int i = 0; foreach (string key in dblog.FiledValues.Keys) { fields += string.Format("{0},", key); vals += string.Format("@{0},", key); dbParameters[i] = this.CreateDbParameter(dblog.DbDialect, "@" + key, dblog.FiledValues[key]); i++; } fields = fields.Trim(','); vals = vals.Trim(','); string commandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", dblog.DestTable, fields, vals); IDbCommand cmd = conn.CreateCommand(); cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; this.AttachParameters(cmd, dbParameters); return cmd; }
/// <summary> /// 把日志信息输出到数据库的方法。 /// </summary> /// <param name="metaLog">日志数据封送对象</param> public virtual void Append(MetaLog metaLog) { if (metaLog == null) { return; } DbMetaLog dblog = metaLog as DbMetaLog; if (dblog.FiledValues == null || dblog.FiledValues.Count == 0) { return; } try { string connstr = ConfigurationManager.ConnectionStrings["logging"].ConnectionString; using (IDbConnection conn = this.CreateConnection(dblog.DbDialect, connstr)) { conn.Open(); IDbCommand cmd = this.CreateCommand(dblog, conn); cmd.ExecuteNonQuery(); } } catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("LightFramework.Tracing.DBLogAppender", ex.ToString()); } }