// ctor for bulk tables (bulk insert) internal TableParamChainer(Chainer prev, TableArgument bulkTable) : base(prev, bulkTable.Sql, DT.BulkTable) { CheckNullAndThrow(Arg(() => bulkTable, bulkTable)); bulkTable.TryThrow(); var root = GetRoot(); int ordinal = root.AllParams.Count; Param = new Variable(ordinal, bulkTable.Sql, DT.BulkTable, bulkTable, IdentifierType.Param); if (Param.Exception != null) { Param.Exception.Extra = Text.Free.TempTableParamExtra; TryThrow(Param.Exception); } root.TryAddParamOrThrow(Param, false); }
internal static Result ExecuteBulkInsert(Assembly client, DataTable data, TableArgument table, ConnectBy connectBy) { table.TryThrow(Text.Method.BulkInsertGo); if (data == null) { throw new QueryTalkException("Crud.GoBulkInsert", QueryTalkExceptionType.ArgumentNull, "data = null", Text.Method.BulkInsertGo) .SetObjectName(table.Sql); } if (data.Rows.Count == 0) { return(new Result(false, 0)); } try { ConnectionKey connKey = null; if (connectBy != null) { connKey = ((IConnectable)connectBy).ConnectionKey; } var connectionString = ConnectionManager.InvokeConnectionFunc(client, connKey).ConnectionString; using (SqlConnection cn = new SqlConnection(connectionString)) { cn.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(cn)) { bulkCopy.DestinationTableName = table.Sql; bulkCopy.WriteToServer(data); } } return(new Result(true, data.Rows.Count)); } catch (QueryTalkException ex) { ex.Method = Text.Method.BulkInsertGo; throw; } }