C#使用Datatable導(dǎo)入sqlserver數(shù)據(jù)庫(kù)的三種方法
第一種,最常見(jiàn)的Executenonquery(返回影響的行數(shù))sql是我的查詢插入語(yǔ)句,你可以換成你的!這種方式入庫(kù),速度一般,大量數(shù)據(jù)時(shí)不提倡使用
/// <summary>
/// 通過(guò)table一個(gè)一個(gè)的插入
/// </summary>
/// <param name="table"></param>
public static void Executenonquery(DataTable table)
{
foreach (DataRow itemRow in table.Rows)
{
//if exists(select * from dbo.ID where ENG = '')
// begin
// return;
// end
//else
// begin
// INSERT INTO ID([ENG],[GB],[B5],[FILE],[MSG]) values('', '', '', '', '')
// end
string sql = "if exists(select * from " + table.TableName + " where ENG = '" + itemRow["ENG"].ToString() + "') "+
" begin return; end else begin INSERT INTO " + table.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
"VALUES('" + itemRow["ENG"].ToString() + "'" +
",'" + itemRow["GB"].ToString() + "'" +
",'" + itemRow["B5"].ToString() + "'" +
",'" + itemRow["FILE"].ToString() + "'" +
",'" + itemRow["MSG"].ToString() + "') end";
using (SqlConnection sqlconn = new SqlConnection(connectString))
{
sqlconn.Open();
SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
sqlcommand.ExecuteNonQuery();
sqlconn.Close();
}
}
}2,通過(guò)adapter入庫(kù),這種入庫(kù),起先,你需要先把datatable放入到dataset中然后進(jìn)行入庫(kù),這種方式主要是對(duì)庫(kù)中對(duì)應(yīng)的表進(jìn)行增刪改,方便使用(效率只比第一種方式好點(diǎn))
/// <summary>
/// 通過(guò)adapter更新數(shù)據(jù)庫(kù)
/// </summary>
/// <param name="dataset"></param>
public static void DataadapterInssert(DataSet dataset)
{
if (dataset.Tables.Count > 0)
{
foreach (DataTable itemTable in dataset.Tables)
{
SqlCommand insertcommand = new SqlCommand("if exists(select * from " + itemTable.TableName + " where ENG = @ENG) begin return; end "+
" else begin INSERT INTO " + itemTable.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
"VALUES(@ENG, @GB,@B5,@FILE,@MSG) end", new SqlConnection(connectString));
insertcommand.Parameters.Add("@ENG", SqlDbType.VarChar, 100, "ENG");
insertcommand.Parameters.Add("@GB", SqlDbType.VarChar, 100, "GB");
insertcommand.Parameters.Add("@B5", SqlDbType.VarChar, 200, "B5");
insertcommand.Parameters.Add("@FILE", SqlDbType.VarChar, 200, "FILE");
insertcommand.Parameters.Add("@MSG", SqlDbType.VarChar, 100, "MSG");
SqlDataAdapter sqldataadapter = new SqlDataAdapter();
sqldataadapter.InsertCommand = insertcommand;
sqldataadapter.Update(dataset, itemTable.TableName);
}
}
}3,重頭戲都在最后,這種方式速度比前兩種快很多,適合用于大量數(shù)據(jù)插入更新,也將datatable放入dataset中然后通過(guò)遍歷,將datatable復(fù)制到數(shù)據(jù)庫(kù)中對(duì)應(yīng)的表中,快速便捷
// <summary>
/// 通過(guò)SqlBulkCopy復(fù)制table數(shù)據(jù)到數(shù)據(jù)庫(kù)
/// </summary>
/// <param name="dataset"></param>
public static void SqlbulkcopyInsert(DataSet dataset)
{
string ie;
if (dataset.Tables.Count > 0)
{
foreach (DataTable itemTable in dataset.Tables)
{
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectString, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = itemTable.TableName;//數(shù)據(jù)庫(kù)中的表名
for (int i = 0; i < itemTable.Rows.Count; i++)
{
ie = itemTable.Rows[i][2].ToString();
}
sqlbulkcopy.WriteToServer(itemTable);
}
}
}到此這篇關(guān)于C#使用Datatable導(dǎo)入sqlserver數(shù)據(jù)庫(kù)的三種方法的文章就介紹到這了,更多相關(guān)C# Datatable導(dǎo)入sql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類,結(jié)合具體實(shí)例形式分析了C#針對(duì)UDP請(qǐng)求的監(jiān)聽(tīng)、接收、發(fā)送等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
在類庫(kù)或winform項(xiàng)目中打開(kāi)另一個(gè)winform項(xiàng)目窗體的方法
這篇文章主要介紹了在類庫(kù)或winform項(xiàng)目中打開(kāi)另一個(gè)winform項(xiàng)目窗體的方法,可以實(shí)現(xiàn)Winform項(xiàng)目間窗體的調(diào)用,在進(jìn)行Winform項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
C#實(shí)現(xiàn)圖片放大功能的按照像素放大圖像方法
這篇文章主要介紹了C#實(shí)現(xiàn)圖片放大功能的按照像素放大圖像方法,功能非常實(shí)用,需要的朋友可以參考下2014-07-07
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來(lái)比較麻煩。今天偶然得之“Aspose.Words.dll”可以實(shí)現(xiàn)2013-08-08
C#的Socket實(shí)現(xiàn)UDP協(xié)議通信示例代碼
本篇文章主要介紹了C#的Socket實(shí)現(xiàn)UDP協(xié)議通信示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在服務(wù)器端裁剪圖片的方法,涉及C#操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-04-04

