public static void Error(this ISqlLog log, Exception ex, string message) { message = message ?? DefaultExceptionMsg; log.LogMessage(message); ex = ex.InnerException; while (ex != null) { log.LogMessage(ex.Message); ex = ex.InnerException; } log.LogMessage(Environment.NewLine); }
protected virtual void CreateTables(ISqlLog log, DatabaseScriptEntry script, IDbContext ctx) { var cmd = ctx.Connection.CreateCommand(); if (script.SequenceScripts != null && script.SequenceScripts.Length > 0) { foreach (var item in script.SequenceScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.SchemaScripts != null && script.SchemaScripts.Length > 0) { foreach (var item in script.SchemaScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.TableScripts != null && script.TableScripts.Length > 0) { foreach (var item in script.TableScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.PKConstraintScripts != null && script.PKConstraintScripts.Length > 0) { foreach (var item in script.PKConstraintScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.FKConstraintScripts != null && script.FKConstraintScripts.Length > 0) { foreach (var item in script.FKConstraintScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.UniquleConstraintScripts != null && script.UniquleConstraintScripts.Length > 0) { foreach (var item in script.UniquleConstraintScripts) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } if (script.CheckConstraintScript != null && script.CheckConstraintScript.Length > 0) { foreach (var item in script.CheckConstraintScript) { cmd.CommandText = item; log.LogMessage(item); cmd.ExecuteNonQuery(); } } }
IEnumerable <int> ExecuteBatch(string commandText, NamedParameter[] parameters, IEnumerable <object[]> paramSets) { DataTable dataTable = new DataTable(); for (int i = 0, n = parameters.Length; i < n; i++) { var qp = parameters[i]; dataTable.Columns.Add(qp.Name, NLite.Reflection.TypeHelper.GetNonNullableType(qp.Type)); } log.LogMessage("-- Start SQL Batching --"); log.LogMessage(""); log.LogCommand(commandText, parameters, null); foreach (var paramValues in paramSets) { dataTable.Rows.Add(paramValues); log.LogParameters(parameters, paramValues); log.LogMessage(""); } var count = dataTable.Rows.Count; var result = new int[count]; if (count > 0) { int n; try { var dataAdapter = this.dbContext.dbConfiguration.DbProviderFactory.CreateDataAdapter(); //dataAdapter.UpdateBatchSize = batchSize; var cmd = this.GetCommand(commandText, parameters, null); for (int i = 0, m = parameters.Length; i < m; i++) { cmd.Parameters[i].SourceColumn = parameters[i].Name; } dataAdapter.InsertCommand = cmd;//系统会根据InsertCommand自动生成相关的DeleteCommand等 dataAdapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; n = dataAdapter.Update(dataTable); } catch (ConnectionException) { throw; } catch (DBConcurrencyException) { throw; } catch (Exception ex) { var exceptionContext = new DbExceptionContextInfo { Sql = commandText, SqlException = new PersistenceException(ex.Message, ex), }; throw ExceptionHelper.Convert(log, exceptionConverter, exceptionContext); } for (int i = 0; i < count; i++) { result[i] = i < n ? 1 : 0; } dataTable.Rows.Clear(); } log.LogMessage("-- End SQL Batching --"); log.LogMessage(""); return(result); }