本文整理汇总了C#中System.Data.SqlClient.SqlParameter.Any方法的典型用法代码示例。如果您正苦于以下问题:C# SqlParameter.Any方法的具体用法?C# SqlParameter.Any怎么用?C# SqlParameter.Any使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlParameter
的用法示例。
在下文中一共展示了SqlParameter.Any方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TransferData
/// <summary>
/// Transfer data into a destination table.
/// </summary>
/// <param name="tableName">
/// The name of the table that will be written to.
/// If a query is not provided this will also be the name of the table that data is read from.</param>
/// <param name="sqlBulkCopyOptions">Options used by <see cref="SqlBulkCopy"/>.</param>
/// <param name="query">An optional query that will be used as the source of data.</param>
/// <param name="queryParams">Optional parameters that are used by the source data query.</param>
/// <param name="sqlTransaction">Optional <see cref="SqlTransaction"/> object that will be used to construct the <see cref="SqlBulkCopy"/>.</param>
/// <param name="sqlBulkCopyCustomizer">Optional delegate that can customize the <see cref="SqlBulkCopy"/> object.</param>
/// <param name="smartColumnMapping">
/// Passing true will cause source columns to be mapped to destination columns by name and computed columns to be ignored.
/// A value of false will result in ordinal based mapping of all columns. Defaults to true.
/// </param>
public void TransferData(string tableName, string query = null, SqlParameter[] queryParams = null, SqlBulkCopyOptions sqlBulkCopyOptions = SqlBulkCopyOptions.Default, SqlTransaction sqlTransaction = null, Action<SqlBulkCopy> sqlBulkCopyCustomizer = null, bool smartColumnMapping = true)
{
if (tableName == null)
throw new ArgumentNullException(nameof(tableName));
if (!tableName.Contains("["))
tableName = tableName.Split('.').Select(n => "[" + n + "]").Join(".");
using (var source = new SqlConnection(_sourceConnString))
using (var dest = new SqlConnection(_destConnString))
using (var bcp = new SqlBulkCopy(dest, sqlBulkCopyOptions, null))
using (var sourceCmd = source.CreateCommand())
using (source.Connect())
using (dest.Connect())
{
query = query.IsNullOrWhiteSpace() ? $"SELECT * FROM {tableName}" : query;
bcp.EnableStreaming = true;
bcp.DestinationTableName = tableName;
bcp.BulkCopyTimeout = (int)TransferTimeout.TotalSeconds;
sourceCmd.CommandType = CommandType.Text;
sourceCmd.CommandText = query;
sourceCmd.CommandTimeout = (int)TransferTimeout.TotalSeconds;
if (queryParams != null && queryParams.Any())
sourceCmd.Parameters.AddRange(queryParams);
using (var reader = sourceCmd.ExecuteReader())
{
if (smartColumnMapping)
{
var schema = reader.GetSchemaTable();
MapColumns(schema, bcp, dest);
}
if (sqlBulkCopyCustomizer != null)
sqlBulkCopyCustomizer(bcp);
bcp.WriteToServer(reader);
}
}
}