C#實現(xiàn)SQL批量插入數(shù)據(jù)到表的方法
更新時間:2016年04月18日 09:24:06 作者:且行且思
這篇文章主要介紹了C#實現(xiàn)SQL批量插入數(shù)據(jù)到表的方法,涉及C#批量操作SQL的相關技巧,需要的朋友可以參考下
本文實例講述了C#實現(xiàn)SQL批量插入數(shù)據(jù)到表的方法。分享給大家供大家參考,具體如下:
#region 幫助實例:SQL 批量插入數(shù)據(jù) 多種方法
/// <summary>
/// SqlBulkCopy往數(shù)據(jù)庫中批量插入數(shù)據(jù)
/// </summary>
/// <param name="sourceDataTable">數(shù)據(jù)源表</param>
/// <param name="targetTableName">服務器上目標表</param>
/// <param name="mapping">創(chuàng)建新的列映射,并使用列序號引用源列和目標列的列名稱。</param>
public static void BulkToDB(DataTable sourceDataTable, string targetTableName, SqlBulkCopyColumnMapping[] mapping)
{
/* 調(diào)用方法 -2012年11月16日編寫
//DataTable dt = Get_All_RoomState_ByHID();
//SqlBulkCopyColumnMapping[] mapping = new SqlBulkCopyColumnMapping[4];
//mapping[0] = new SqlBulkCopyColumnMapping("Xing_H_ID", "Xing_H_ID");
//mapping[1] = new SqlBulkCopyColumnMapping("H_Name", "H_Name");
//mapping[2] = new SqlBulkCopyColumnMapping("H_sName", "H_sName");
//mapping[3] = new SqlBulkCopyColumnMapping("H_eName", "H_eName");
//BulkToDB(dt, "Bak_Tts_Hotel_Name", mapping);
*/
SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString);
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn); //用其它源的數(shù)據(jù)有效批量加載sql server表中
bulkCopy.DestinationTableName = targetTableName; //服務器上目標表的名稱
bulkCopy.BatchSize = sourceDataTable.Rows.Count; //每一批次中的行數(shù)
try
{
conn.Open();
if (sourceDataTable != null && sourceDataTable.Rows.Count != 0)
{
for (int i = 0; i < mapping.Length; i++)
bulkCopy.ColumnMappings.Add(mapping[i]);
//將提供的數(shù)據(jù)源中的所有行復制到目標表中
bulkCopy.WriteToServer(sourceDataTable );
}
}
catch (Exception ex)
{
//throw ex;
Common.WriteTextLog("BulkToDB", ex.Message);
}
finally
{
conn.Close();
if (bulkCopy != null)
bulkCopy.Close();
}
}
/// <summary>
/// SQL2008以上方可支持自定義表類型 :調(diào)用存儲過程游標-往數(shù)據(jù)庫中批量插入數(shù)據(jù) ,注意
/// </summary>
/// <param name="sourceDataTable"></param>
public void DataTableToHotelDB(DataTable sourceDataTable)
{
/* -2012年11月15日編寫
ALTER PROCEDURE [dbo].[P_InsertSubject]
@tempStudentID int
AS
DECLARE rs CURSOR LOCAL SCROLL FOR
select H_ID from Tts_Hotel_Name
OPEN rs
FETCH NEXT FROM rs INTO @tempStudentID
WHILE @@FETCH_STATUS = 0
BEGIN
Insert student (tempStudentID) values (@tempStudentID)
FETCH NEXT FROM rs INTO @tempStudentID
END
CLOSE rs
* ***************************************************************
* create table Orders
(
Orders_ID int identity(1,1) primary key,
ItemCode nvarchar(50) not null,
UM nvarchar(20) not null,
Quantity decimal(18,6) not null,
UnitPrice decimal(18,6) not null
)
--創(chuàng)建用戶自定義表類型,在可編程性->類型性->用戶自定義表類型
create type OrdersTableType as table
(
ItemCode nvarchar(50) not null,
UM nvarchar(20) not null,
Quantity decimal(18,6) not null,
UnitPrice decimal(18,6) not null
)
go
create procedure Pro_Orders
(
@OrdersCollection OrdersTableType readonly
)
as
insert into Orders([ItemCode],[UM],[Quantity],[UnitPrice])
SELECT oc.[ItemCode],oc.[UM],[Quantity],oc.[UnitPrice] FROM @OrdersCollection AS oc;
go
*
*/
SqlParameter[] parameters = {new SqlParameter("@OrdersCollection", SqlDbType.Structured)};
parameters[0].Value = sourceDataTable;
new SQLHelper().ExecuteScalar("P_DataTable_ToHotelDB", parameters, true);
}
#endregion
更多關于C#相關內(nèi)容感興趣的讀者可查看本站專題:《C#數(shù)據(jù)結構與算法教程》、《C#常見控件用法教程》、《C#面向?qū)ο蟪绦蛟O計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
相關文章
C# 使用匿名函數(shù)解決EventHandler參數(shù)傳遞的難題
C#動態(tài)生成PictureBox并綁定右鍵菜單,實現(xiàn)刪除圖片2009-05-05
WPF開發(fā)之UniformGrid和ItemsControl的應用詳解
為了簡化開發(fā),WPF提供了UniformGrid布局和ItemsControl容器,本文以一個簡單的小例子,簡述如何在WPF開發(fā)中應用UniformGrid和ItemsControl實現(xiàn)均勻的布局,希望對大家有所幫助2024-01-01

