本文整理汇总了C#中Simple.Data.Ado.AdoAdapter类的典型用法代码示例。如果您正苦于以下问题:C# AdoAdapter类的具体用法?C# AdoAdapter怎么用?C# AdoAdapter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AdoAdapter类属于Simple.Data.Ado命名空间,在下文中一共展示了AdoAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Update
public int Update(AdoAdapter adapter, string tableName, IList<IDictionary<string, object>> data, IDbTransaction transaction, IList<string> keyFields)
{
int count = 0;
if (data == null) throw new ArgumentNullException("data");
if (data.Count < 2) throw new ArgumentException("UpdateMany requires more than one record.");
if (keyFields.Count == 0) throw new NotSupportedException("Adapter does not support key-based update for this object.");
if (!AllRowsHaveSameKeys(data)) throw new SimpleDataException("Records have different structures. Bulk updates are only valid on consistent records.");
var table = adapter.GetSchema().FindTable(tableName);
var exampleRow = new Dictionary<string, object>(data.First(), HomogenizedEqualityComparer.DefaultInstance);
var commandBuilder = new UpdateHelper(adapter.GetSchema()).GetUpdateCommand(tableName, exampleRow,
ExpressionHelper.CriteriaDictionaryToExpression(
tableName, GetCriteria(keyFields, exampleRow)));
using (var connectionScope = ConnectionScope.Create(transaction, adapter.CreateConnection))
using (var command = commandBuilder.GetRepeatableCommand(connectionScope.Connection))
{
var propertyToParameterMap = CreatePropertyToParameterMap(data, table, command);
foreach (var row in data)
{
foreach (var kvp in row)
{
propertyToParameterMap[kvp.Key].Value = kvp.Value ?? DBNull.Value;
}
count += command.ExecuteNonQuery();
}
}
return count;
}
示例2: Insert
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction = null, bool resultRequired = false)
{
var table = adapter.GetSchema().FindTable(tableName);
var dataDictionary = BuildDataDictionary(adapter, data, table);
string columnList = dataDictionary.Keys.Select(c => c.QuotedName).Aggregate((agg, next) => agg + "," + next);
string valueList = dataDictionary.Keys.Select(s => "?").Aggregate((agg, next) => agg + "," + next);
var insertSql = new StringBuilder();
bool identityInsert = adapter.AdoOptions != null && adapter.AdoOptions.IdentityInsert;
if (identityInsert)
{
insertSql.AppendFormat("SET IDENTITY_INSERT {0} ON; ", table.QualifiedName);
}
insertSql.AppendFormat("INSERT INTO {0} ({1})", table.QualifiedName, columnList);
if (resultRequired)
{
insertSql.Append(" OUTPUT INSERTED.*");
}
insertSql.AppendFormat(" VALUES ({0})", valueList);
if (identityInsert)
{
insertSql.AppendFormat("; SET IDENTITY_INSERT {0} OFF; ", table.QualifiedName);
}
if (resultRequired)
{
return ExecuteSingletonQuery(adapter, insertSql.ToString(), dataDictionary.Keys,
dataDictionary.Values, transaction);
}
Execute(adapter, insertSql.ToString(), dataDictionary.Keys, dataDictionary.Values, transaction);
return null;
}
示例3: Insert
public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction)
{
var table = adapter.GetSchema().FindTable(tableName);
var columns = table.Columns.Where(c => !c.IsIdentity).ToList();
string columnList = string.Join(",", columns.Select(c => c.QuotedName));
string valueList = string.Join(",", columns.Select(c => "?"));
string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";
var identityFunction = adapter.GetIdentityFunction();
if (!string.IsNullOrWhiteSpace(identityFunction))
{
var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);
if (identityColumn != null)
{
var selectSql = "select * from " + table.QualifiedName + " where " + identityColumn.QuotedName +
" = " + identityFunction;
if (adapter.ProviderSupportsCompoundStatements)
{
return InsertRowsWithCompoundStatement(adapter, data, transaction, table, columns, selectSql, insertSql);
}
return InsertRowsWithSeparateStatements(adapter, data, transaction, table, columns, insertSql, selectSql);
}
}
InsertRowsWithoutFetchBack(adapter, data, table, columns, insertSql);
return null;
}
示例4: Insert
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction = null,
bool resultRequired = false)
{
var table = adapter.GetSchema().FindTable(tableName);
var insertData = data.Where(p => table.HasColumn(p.Key)).Select((kv, idx) => new InsertColumn
{
Name = kv.Key,
ParameterName = "@p" + idx,
Value = kv.Value,
Column = (FbColumn) table.FindColumn(kv.Key)
}).ToArray();
if (transaction == null)
{
using (var connection = adapter.ConnectionProvider.CreateConnection())
{
connection.Open();
return CreateAndExecuteInsertCommand(connection, table, insertData, resultRequired);
}
}
else
{
return CreateAndExecuteInsertCommand(transaction.Connection, table, insertData, resultRequired, transaction);
}
}
示例5: Insert
public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction)
{
var table = DatabaseSchema.Get(adapter.ConnectionProvider, adapter.ProviderHelper).FindTable(tableName);
if (table == null) throw new SimpleDataException(String.Format("Table '{0}' not found", tableName));
var insertData = data.Select(row => row.Where(p => table.HasColumn(p.Key) && !table.FindColumn(p.Key).IsIdentity).ToDictionary());
var insertColumns = insertData.First().Keys.Select(table.FindColumn).ToArray();
var columnsSql = insertColumns.Select(s => s.QuotedName).Aggregate((agg, next) => String.Concat(agg, ",", next));
var valuesSql = insertColumns.Select((val, idx) => ":p" + idx.ToString()).Aggregate((agg, next) => String.Concat(agg, ",", next));
var insertSql = string.Format("INSERT INTO {0} ({1}) VALUES({2}) RETURNING *;", table.QualifiedName, columnsSql, valuesSql);
if (transaction != null)
{
using(var cmd = transaction.Connection.CreateCommand())
{
cmd.Transaction = transaction;
cmd.CommandText = insertSql;
return insertData.Select(row => ExecuteInsert(cmd, insertColumns, row.Values.ToArray())).ToList();
}
}
using (var conn = adapter.ConnectionProvider.CreateConnection())
{
conn.Open();
using(var cmd = conn.CreateCommand())
{
cmd.CommandText = insertSql;
return insertData.Select(row => ExecuteInsert(cmd, insertColumns, row.Values.ToArray())).ToList();
}
}
}
示例6: Insert
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction = null,
bool resultRequired = false)
{
var table = adapter.GetSchema().FindTable(tableName);
var dataDictionary = BuildDataDictionary(adapter, data, table);
string columnList = dataDictionary.Keys.Select(c => c.QuotedName).Aggregate((agg, next) => agg + "," + next);
string valueList = dataDictionary.Keys.Select(s => "?").Aggregate((agg, next) => agg + "," + next);
var insertSql = new StringBuilder();
insertSql.AppendFormat("INSERT INTO {0} ({1}) VALUES ({2});", table.QualifiedName, columnList, valueList);
if (resultRequired)
{
var identityColumn = table.Columns.FirstOrDefault(c => c.IsIdentity);
if (identityColumn != null)
{
insertSql.AppendFormat(" SELECT * FROM {0} WHERE {1} = LAST_INSERT_ID();", table.QualifiedName,
identityColumn.QuotedName);
return ExecuteSingletonQuery(adapter, insertSql.ToString(), dataDictionary.Keys,
dataDictionary.Values, transaction);
}
}
Execute(adapter, insertSql.ToString(), dataDictionary.Keys, dataDictionary.Values, transaction);
return null;
}
示例7: InsertRowsWithSeparateStatements
private static IEnumerable<IDictionary<string, object>> InsertRowsWithSeparateStatements(AdoAdapter adapter, IEnumerable<IDictionary<string, object>> data,
IDbTransaction transaction, Table table, List<Column> columns,
string insertSql, string selectSql)
{
if (transaction != null)
{
var insertCommand = new CommandHelper(adapter.SchemaProvider).Create(transaction.Connection, insertSql);
var selectCommand = transaction.Connection.CreateCommand();
selectCommand.CommandText = selectSql;
insertCommand.Transaction = transaction;
selectCommand.Transaction = transaction;
return data.Select(row => InsertRow(row, columns, table, insertCommand, selectCommand)).ToList();
}
using (var connection = adapter.CreateConnection())
{
using (var insertCommand = new CommandHelper(adapter.SchemaProvider).Create(connection, insertSql))
using (var selectCommand = connection.CreateCommand())
{
selectCommand.CommandText = selectSql;
connection.Open();
return data.Select(row => InsertRow(row, columns, table, insertCommand, selectCommand)).ToList();
}
}
}
示例8: Insert
public IDictionary<string, object> Insert(AdoAdapter adapter, string tableName, IDictionary<string, object> data, IDbTransaction transaction, bool returnRequired)
{
var s = DatabaseSchema.Get(adapter.ConnectionProvider, new ProviderHelper());
var table = s.FindTable(tableName);
var tuples = InitializeInsertion(table);
foreach (var d in data)
tuples[d.Key.Homogenize()].InsertedValue = d.Value;
Func<IDbCommand> command =
() =>
{
var c = transaction != null
? transaction.Connection.CreateCommand()
: adapter.CreateConnection().CreateCommand();
return c;
};
IDbCommand cmd;
using (cmd = ConstructCommand(tuples, table.QualifiedName, command))
{
cmd.WriteTrace();
cmd.Connection.TryOpen();
cmd.ExecuteNonQuery();
var returnData = new DbDictionary();
foreach (var it in tuples.Values)
returnData.Add(it.SimpleDataColumn, NormalizeReturningValue((IDbDataParameter)cmd.Parameters[it.ReturningParameterName]));
data = returnData;
}
return data;
}
示例9: BulkInserterHelper
public BulkInserterHelper(AdoAdapter adapter, IEnumerable<IDictionary<string, object>> data, Table table, List<Column> columns)
{
Adapter = adapter;
Data = data;
_table = table;
_columns = columns;
}
示例10: Insert
public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction, Func<IDictionary<string,object>, Exception, bool> onError, bool resultRequired)
{
var table = adapter.GetSchema().FindTable(tableName);
var columns = table.Columns.Where(c => !c.IsIdentity).ToList();
string columnList = string.Join(",", columns.Select(c => c.QuotedName));
string valueList = string.Join(",", columns.Select(c => "?"));
string insertSql = "insert into " + table.QualifiedName + " (" + columnList + ") values (" + valueList + ")";
var helper = transaction == null
? new BulkInserterHelper(adapter, data, table, columns)
: new BulkInserterTransactionHelper(adapter, data, table, columns, transaction);
if (resultRequired)
{
var identityColumn = table.Columns.FirstOrDefault(col => col.IsIdentity);
if (identityColumn != null)
{
var identityFunction = adapter.GetIdentityFunction();
if (!string.IsNullOrWhiteSpace(identityFunction))
{
return InsertRowsAndReturn(adapter, identityFunction, helper, insertSql, table, onError);
}
}
}
helper.InsertRowsWithoutFetchBack(insertSql, onError);
return null;
}
示例11: QueryBuilder
public QueryBuilder(AdoAdapter adoAdapter, int bulkIndex)
{
_adoAdapter = adoAdapter;
_bulkIndex = bulkIndex;
_schema = _adoAdapter.GetSchema();
_commandBuilder = new CommandBuilder(_schema, _bulkIndex);
_simpleReferenceFormatter = new SimpleReferenceFormatter(_schema, _commandBuilder);
}
示例12: QueryBuilderBase
protected QueryBuilderBase(AdoAdapter adapter, int bulkIndex, IFunctionNameConverter functionNameConverter)
{
_adoAdapter = adapter;
_bulkIndex = bulkIndex;
_schema = _adoAdapter.GetSchema();
_commandBuilder = new CommandBuilder(_schema, _bulkIndex);
_simpleReferenceFormatter = new SimpleReferenceFormatter(_schema, _commandBuilder, functionNameConverter);
}
示例13: AdoAdapterAggregator
public AdoAdapterAggregator(AdoAdapter adapter, DbTransaction transaction)
{
if (adapter == null) throw new ArgumentNullException("adapter");
_adapter = adapter;
if (transaction != null)
{
_transaction = transaction;
_connection = transaction.Connection;
}
}
示例14: GetDbCommand
public IDbCommand GetDbCommand(AdoAdapter adapter, IDbConnection connection, IEnumerable<object> parameterValues)
{
var command = connection.CreateCommand(adapter.AdoOptions);
command.CommandText = _commandText;
foreach (var parameter in CreateParameters(adapter.GetSchema(), command, parameterValues))
{
command.Parameters.Add(parameter);
}
return command;
}
示例15: BuildDataDictionary
private static Dictionary<Column, object> BuildDataDictionary(AdoAdapter adapter,
IDictionary<string, object> data, Table table)
{
Func<string, bool> columnFilter = key =>
table.HasColumn(key) &&
(table.FindColumn(key).IsWriteable ||
((adapter.AdoOptions != null && adapter.AdoOptions.IdentityInsert) && table.FindColumn(key).IsIdentity));
return data.Where(kvp => columnFilter(kvp.Key))
.ToDictionary(kvp => table.FindColumn(kvp.Key), kvp => kvp.Value);
}