1目标要更新的表 DestTableName;
3创建一个存储过程
B)insert into DestTableName(Field1,Field2,...) select Field1,.. from @typeTableName
示例程序实现:
using
System;
System.Diagnostics;
System.Data;
System.Data.sqlClient;
com.DataAccess;
namespace ConsoleAppInsertTest
{
class Program
{
static string connectionString = sqlHelper.ConnectionStringLocalTransaction; // 数据库连接字符串
int count 1000000 ; 插入的条数 void Main( [] args)
{
long commonInsertRunTime = CommonInsert();
Console.WriteLine(string.Format("普通方式插入{1}条数据所用的时间是{0}毫秒",commonInsertRunTime,count)); long sqlBulkcopyInsertRunTime sqlBulkcopyInsert();
Console.WriteLine( .Format( " 使用sqlBulkcopy插入{1}条数据所用的时间是{0}毫秒 " ,sqlBulkcopyInsertRunTime,count));
TVPInsertRunTime TVPInsert();
Console.WriteLine( 使用表值方式(TVP)插入{1}条数据所用的时间是{0}毫秒 /// <summary>
普通调用存储过程插入数据
</summary> <returns></returns>
private CommonInsert()
{
Stopwatch stopwatch new Stopwatch();
stopwatch.Start();
passportKey;
for ( i 0 ; i < count; i ++ )
{
passportKey Guid.NewGuid().ToString();
sqlParameter[] sqlParameter { sqlParameter( @passport ssportKey) };
sqlHelper.ExecuteNonQuery(connectionString,CommandType.StoredProcedure, CreatePassport sqlParameter);
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
使用sqlBulkcopy方式插入数据
<param name="dataTable"></param> sqlBulkcopyInsert()
{
Stopwatch stopwatch Stopwatch();
stopwatch.Start();
DataTable dataTable GetTableSchema();
Guid.NewGuid().ToString();
DaTarow daTarow dataTable.NewRow();
daTarow[ ] passportKey;
dataTable.Rows.Add(daTarow);
}
sqlBulkcopy sqlBulkcopy sqlBulkcopy(connectionString);
sqlBulkcopy.DestinationTableName Passport ;
sqlBulkcopy.BatchSize dataTable.Rows.Count;
sqlConnection sqlConnection sqlConnection(connectionString);
sqlConnection.open();
if (dataTable != null && dataTable.Rows.Count != )
{
sqlBulkcopy.WritetoServer(dataTable);
}
sqlBulkcopy.Close();
sqlConnection.Close();
stopwatch.Stop();
stopwatch.ElapsedMilliseconds;
}
TVPInsert()
{
Stopwatch stopwatch passportKey;
dataTable.Rows.Add(daTarow);
}
sqlParameter[] sqlParameter @TVP sqlHelper.ExecuteNonQuery(connectionString,0)">CreatePassportWithTVP sqlParameter);
stopwatch.Stop();
DataTable GetTableSchema()
{
DataTable dataTable DataTable();
dataTable.Columns.AddRange( DataColumn[] { DataColumn( PassportKey ) });
dataTable; } } }
System.Diagnostics;
System.Data;
System.Data.sqlClient;
com.DataAccess;
namespace ConsoleAppInsertTest
{
class Program
{
static string connectionString = sqlHelper.ConnectionStringLocalTransaction; // 数据库连接字符串
int count 1000000 ; 插入的条数 void Main( [] args)
{
long commonInsertRunTime = CommonInsert();
Console.WriteLine(string.Format("普通方式插入{1}条数据所用的时间是{0}毫秒",commonInsertRunTime,count)); long sqlBulkcopyInsertRunTime sqlBulkcopyInsert();
Console.WriteLine( .Format( " 使用sqlBulkcopy插入{1}条数据所用的时间是{0}毫秒 " ,sqlBulkcopyInsertRunTime,count));
TVPInsertRunTime TVPInsert();
Console.WriteLine( 使用表值方式(TVP)插入{1}条数据所用的时间是{0}毫秒 /// <summary>
普通调用存储过程插入数据
</summary> <returns></returns>
private CommonInsert()
{
Stopwatch stopwatch new Stopwatch();
stopwatch.Start();
passportKey;
for ( i 0 ; i < count; i ++ )
{
passportKey Guid.NewGuid().ToString();
sqlParameter[] sqlParameter { sqlParameter( @passport ssportKey) };
sqlHelper.ExecuteNonQuery(connectionString,CommandType.StoredProcedure, CreatePassport sqlParameter);
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
使用sqlBulkcopy方式插入数据
<param name="dataTable"></param> sqlBulkcopyInsert()
{
Stopwatch stopwatch Stopwatch();
stopwatch.Start();
DataTable dataTable GetTableSchema();
Guid.NewGuid().ToString();
DaTarow daTarow dataTable.NewRow();
daTarow[ ] passportKey;
dataTable.Rows.Add(daTarow);
}
sqlBulkcopy sqlBulkcopy sqlBulkcopy(connectionString);
sqlBulkcopy.DestinationTableName Passport ;
sqlBulkcopy.BatchSize dataTable.Rows.Count;
sqlConnection sqlConnection sqlConnection(connectionString);
sqlConnection.open();
if (dataTable != null && dataTable.Rows.Count != )
{
sqlBulkcopy.WritetoServer(dataTable);
}
sqlBulkcopy.Close();
sqlConnection.Close();
stopwatch.Stop();
stopwatch.ElapsedMilliseconds;
}
TVPInsert()
{
Stopwatch stopwatch passportKey;
dataTable.Rows.Add(daTarow);
}
sqlParameter[] sqlParameter @TVP sqlHelper.ExecuteNonQuery(connectionString,0)">CreatePassportWithTVP sqlParameter);
stopwatch.Stop();
DataTable GetTableSchema()
{
DataTable dataTable DataTable();
dataTable.Columns.AddRange( DataColumn[] { DataColumn( PassportKey ) });
dataTable; } } }