批量插入數據, 將DataTable里的數據批量寫入資料庫的方法

来源:https://www.cnblogs.com/fuchangmeng/archive/2019/04/27/10778666.html
-Advertisement-
Play Games

大量數據導入操作, 也就是直接將DataTable里的內容寫入到資料庫 通用方法: 拼接Insert語句, 好土鱉 1. MS Sql Server: 使用SqlBulkCopy 2. MySql: adapter.update()批量更新 MySqlBulkLoader, 這個是從文件裡邊到的, ...


大量數據導入操作, 也就是直接將DataTable里的內容寫入到資料庫

通用方法: 拼接Insert語句, 好土鱉

 

1. MS Sql Server:   使用SqlBulkCopy

2. MySql:

adapter.update()批量更新

MySqlBulkLoader, 這個是從文件裡邊到的, 有個實現是先將DATATable編程CSV文件, 在用MySqlBulkLoader導入MySql

參考文章: http://theonetechnologies.com/outsourcing/post/mysql-bulk-data-import-using-net-connector-mysqlbulkloader-class.aspx

原文引用於Rocho.J批量插入數據, 將DataTable里的數據批量寫入資料庫的方法

//參考代碼

 1 Function to create .csv file from DataTable (you can skip this, if you already have csv file)
 2     public static void CreateCSVfile(DataTable dtable, string strFilePath)
 3     {
 4         StreamWriter sw = new StreamWriter(strFilePath, false);
 5         int icolcount = dtable.Columns.Count;
 6         foreach (DataRow drow in dtable.Rows)
 7         {
 8             for (int i = 0; i < icolcount; i++)
 9             {
10                 if (!Convert.IsDBNull(drow[i]))
11                 {
12                     sw.Write(drow[i].ToString());
13                 }
14                 if (i < icolcount - 1)
15                 {
16                     sw.Write(",");
17                 }
18             }
19             sw.Write(sw.NewLine);
20         }
21         sw.Close();
22         sw.Dispose();
23     }
24 
25     //2. Import data into MySQL database
26     private void ImportMySQL()
27     {
28         DataTable orderDetail = new DataTable("ItemDetail");
29         DataColumn c = new DataColumn();        // always
30         orderDetail.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
31         orderDetail.Columns.Add(new DataColumn("value", Type.GetType("System.Int32")));
32         orderDetail.Columns.Add(new DataColumn("length", Type.GetType("System.Int32")));
33         orderDetail.Columns.Add(new DataColumn("breadth", Type.GetType("System.Int32")));
34         orderDetail.Columns.Add(new DataColumn("total", Type.GetType("System.Decimal")));
35         orderDetail.Columns["total"].Expression = "value/(length*breadth)";       //Adding dummy entries
36         DataRow dr = orderDetail.NewRow();
37         dr["ID"] = 1;
38         dr["value"] = 50;
39         dr["length"] = 5;
40         dr["breadth"] = 8;
41         orderDetail.Rows.Add(dr);
42         dr = orderDetail.NewRow();
43         dr["ID"] = 2;
44         dr["value"] = 60;
45         dr["length"] = 15;
46         dr["breadth"] = 18;
47         orderDetail.Rows.Add(dr);     //Adding dummy entries
48         string connectMySQL = "Server=localhost;Database=test;Uid=username;Pwd=password;";
49         string strFile = "/TempFolder/MySQL" + DateTime.Now.Ticks.ToString() + ".csv";       //Create directory if not exist... Make sure directory has required rights..    
50         if (!Directory.Exists(Server.MapPath("~/TempFolder/")))
51             Directory.CreateDirectory(Server.MapPath("~/TempFolder/"));       //If file does not exist then create it and right data into it..     
52         if (!File.Exists(Server.MapPath(strFile)))
53         {
54             FileStream fs = new FileStream(Server.MapPath(strFile), FileMode.Create, FileAccess.Write);
55             fs.Close();
56             fs.Dispose();
57         }
58         //Generate csv file from where data read
59         CreateCSVfile(orderDetail, Server.MapPath(strFile));
60         using (MySqlConnection cn1 = new MySqlConnection(connectMySQL))
61         {
62             cn1.Open();
63             MySqlBulkLoader bcp1 = new MySqlBulkLoader(cn1);
64             bcp1.TableName = "productorder"; //Create ProductOrder table into MYSQL database...     
65             bcp1.FieldTerminator = ",";
66             bcp1.LineTerminator = "\r\n";
67             bcp1.FileName = Server.MapPath(strFile);
68             bcp1.NumberOfLinesToSkip = 0;
69             bcp1.Load();       //Once data write into db then delete file..    
70             try
71             {
72                 File.Delete(Server.MapPath(strFile));
73             }
74             catch (Exception ex)
75             {
76                 string str = ex.Message;
77             }
78         }
79     }
View Code

3. MS Access: 只能用批量更新了, adapter.update()

備註:   此處先標記個思路, 等我這實現完了, 貼個示例

==============

其實早就做完了, 都忘記這回事了... 今天看到這篇, 補一下代碼

//枚舉

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ETLUtilityDAL.Enums
 7 {
 8     public enum DatabaseType
 9     {
10         MSSql,
11         MySql,
12         MSAccess,
13         Oracle
14     }
15 }
View Code

//公共方法

//DALFactory.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.Configuration;
  7 using ETLUtilityDAL.Enums;
  8 using ETLUtilityDAL.Interfaces;
  9 using ETLUtilityDAL.Implement;
 10 using System.Data.SqlClient;
 11 
 12 namespace ETLUtilityDAL.Common
 13 {
 14     /// <summary>
 15     /// 資料庫訪問工廠, 用於產生相應類型的資料庫實例
 16     /// </summary>
 17     public class DALFactory
 18     {
 19         private static readonly Dictionary<string, string> dictConnectionStrs = new Dictionary<string, string>();
 20         private static readonly DatabaseType currentDB = (DatabaseType)Enum.Parse(typeof(DatabaseType), ConfigurationManager.AppSettings["CurrentDatabase"]);
 21         
 22         /// <summary>
 23         /// 靜態構造函數, 用於初始化資料庫連接串字典
 24         /// </summary>
 25         static DALFactory()
 26         {
 27             getConnectionDictionary();
 28         }
 29 
 30         private static void getConnectionDictionary()
 31         {
 32             ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
 33             string tempConStr = "";
 34             foreach (string str in Enum.GetNames(typeof(DatabaseType)))
 35                 try
 36                 {
 37                     tempConStr = cssc[str.Trim().ToLower()].ConnectionString;
 38                     if (!string.IsNullOrEmpty(tempConStr))
 39                         dictConnectionStrs.Add(str, tempConStr);
 40                 }
 41                 catch (Exception ex)
 42                 {
 43                     //throw ex;
 44                 }
 45         }
 46 
 47         /// <summary>
 48         /// 返回連接串字典以供查看
 49         /// </summary>
 50         public static Dictionary<string,string> ConnectionStringsDictionary
 51         {
 52             get { return dictConnectionStrs; }
 53         }
 54 
 55         /// <summary>
 56         /// 根據資料庫的類型獲得有固定資料庫名稱的泛型類型的資料庫連接對象
 57         /// </summary>
 58         /// <typeparam name="T">T類型, 表示泛型類型的資料庫連接對象</typeparam>
 59         /// <param name="dbType">System.Enum類型, 表示資料庫的類型</param>
 60         /// <returns>T類型, 返回泛型類型的資料庫連接對象</returns>
 61         public static T GetDatabaseConnection<T>(DatabaseType dbType)
 62         {
 63             string dbTypeStr = Enum.GetName(typeof(DatabaseType), dbType);
 64             if(dictConnectionStrs.Keys.Contains(dbTypeStr))
 65                 return (T)Convert.ChangeType(Activator.CreateInstance(typeof(T), dictConnectionStrs[dbTypeStr]),typeof(T));
 66             else
 67                 return default(T);
 68         }
 69 
 70         /// <summary>
 71         /// 根據資料庫的類型獲得指定資料庫名稱的泛型類型的資料庫連接對象
 72         /// </summary>
 73         /// <typeparam name="T">T類型, 表示泛型類型的資料庫連接對象</typeparam>
 74         /// <param name="dbType">System.Enum類型, 表示資料庫的類型</param>
 75         /// <param name="dbName">System.String, 表示指定的資料庫名稱</param>
 76         /// <returns>T類型, 返回泛型類型的資料庫連接對象</returns>
 77         public static T GetDatabaseConnection<T>(DatabaseType dbType, string dbName)
 78         {
 79             string dbTypeStr = Enum.GetName(typeof(DatabaseType), dbType);
 80             if (dictConnectionStrs.Keys.Contains(dbTypeStr) && !string.IsNullOrEmpty(dbName))
 81                 return (T)Convert.ChangeType(Activator.CreateInstance(typeof(T), dictConnectionStrs[dbTypeStr].Replace("*",dbName)), typeof(T));
 82             else
 83                 return default(T);
 84         }
 85 
 86         /// <summary>
 87         /// 根據資料庫的類型獲得固定資料庫名稱的資料庫訪問工具類DBHelper
 88         /// </summary>
 89         /// <param name="dbType">System.Enum類型, 表示資料庫的類型</param>
 90         /// <returns>Interface, 根據不同的資料庫類型返回不同的工具類的實現</returns>
 91         public static IDBHelper GetDBHelper(DatabaseType dbType)
 92         {
 93             #region
 94             switch (dbType)
 95             {
 96                 case DatabaseType.MSSql:
 97                     return new MSSqlDBHelper();
 98                 case DatabaseType.MSAccess:
 99                     return new MSAccessDBHelper();
100                 case DatabaseType.MySql:
101                     return new MySqlDBHelper();
102                 case DatabaseType.Oracle:
103                     goto default;
104                 default:
105                     return null;
106             }
107             #endregion
108         }
109 
110         /// <summary>
111         /// 根據資料庫的類型獲得指定資料庫名稱的資料庫訪問工具類DBHelper
112         /// </summary>
113         /// <param name="dbType">System.Enum類型, 表示資料庫的類型</param>
114         /// <param name="dbName">System.String, 表示指定的資料庫名稱</param>
115         /// <returns>Interface, 根據不同的資料庫名稱和類型返回不同的工具類的實現</returns>
116         public static IDBHelper GetDBHelper(DatabaseType dbType, string dbName)
117         {
118             #region
119             switch (dbType)
120             {
121                 case DatabaseType.MSSql:
122                     return new MSSqlDBHelper(dbName);
123                 case DatabaseType.MSAccess:
124                     return new MSAccessDBHelper(dbName);
125                 case DatabaseType.MySql:
126                     return new MySqlDBHelper(dbName);
127                 case DatabaseType.Oracle:
128                     goto default;
129                 default:
130                     return null;
131             }
132             #endregion
133         }
134 
135         /// <summary>
136         /// 獲得當前正在使用的固定資料庫名稱的資料庫類型的訪問工具類Helper
137         /// </summary>
138         /// <returns>Interface, 根據不同的資料庫類型返回不同的工具類的實現</returns>
139         public static IDBHelper GetDBHelper()
140         {
141             return GetDBHelper(currentDB);
142         }
143 
144         /// <summary>
145         /// 獲得當前正在使用的指定據庫名稱的資料庫類型的訪問工具類Helper
146         /// </summary>
147         /// <returns>Interface, 根據不同的資料庫名稱和類型返回不同的工具類的實現</returns>
148         public static IDBHelper GetDBHelper(string dbName)
149         {
150             return GetDBHelper(currentDB,dbName);
151         }
152     }
153 }
View Code

 

//FileHelper.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.IO;
  7 using System.Data;
  8 
  9 namespace ETLUtilityDAL.Common
 10 {
 11     public class FileHelper
 12     {
 13         public static string ReadFileToString(string fileFullPath, Encoding codeType)
 14         {
 15             string result = "";
 16             if (string.IsNullOrEmpty(fileFullPath))
 17                 throw new ArgumentNullException( "fileFullPath","File path can not be null or empty! ");
 18             using (FileStream fileStream = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Read))
 19             {
 20                 if (!File.Exists(fileFullPath))
 21                     throw new FileNotFoundException("File not found! ");
 22             }
 23 
 24             using (StreamReader sReader = new StreamReader(fileFullPath, codeType))
 25             {
 26                 try
 27                 {
 28                     result = sReader.ReadToEnd();
 29                 }
 30                 catch (Exception ex)
 31                 {
 32                     throw new IOException(ex.Message);
 33                 }
 34             }
 35             return result;
 36         }
 37 
 38         public static string ReadFileToString(string fileFullPath)
 39         {
 40             return ReadFileToString(fileFullPath, Encoding.Default);
 41         }
 42 
 43         public static void WriteDataTableToCSVFile(DataTable dataTable, string fileFullPath, Encoding codeType)
 44         {
 45             using (Stream stream = new FileStream(fileFullPath,FileMode.Create,FileAccess.Write))
 46             using (StreamWriter swriter = new StreamWriter(stream, codeType))
 47             {
 48                 try
 49                 {
 50                     int icolcount = dataTable.Columns.Count;
 51                     foreach (DataRow drow in dataTable.Rows)
 52                     {
 53                         for (int i = 0; i < icolcount; i++)
 54                         {
 55                             if (!Convert.IsDBNull(drow[i]))
 56                             {
 57                                 swriter.Write(drow[i].ToString());
 58                             }
 59                             if (i < icolcount - 1)
 60                             {
 61                                 swriter.Write("|");
 62                             }
 63                         }
 64                         swriter.Write(swriter.NewLine);
 65                     }
 66                 }
 67                 catch (Exception ex)
 68                 {
 69                     throw new IOException(ex.Message);
 70                 }
 71             }
 72         }
 73 
 74         public static void WriteDataTableToCSVFile(DataTable dataTable, string fileFullPath)
 75         {
 76             WriteDataTableToCSVFile(dataTable, fileFullPath, Encoding.Default);
 77         }
 78 
 79         public static string[] GetFileFullPathList(string directoryPath, string fileType, bool IsRecursive)
 80         {
 81             return IsRecursive ? Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories) : Directory.GetFiles(directoryPath, fileType, SearchOption.TopDirectoryOnly);
 82         }
 83 
 84         public static string[] GetSubDirectorys(string directoryPath, string containsName, bool IsRecursive)
 85         {
 86             return IsRecursive ? Directory.GetDirectories(directoryPath, containsName, SearchOption.AllDirectories) : Directory.GetDirectories(directoryPath, containsName, SearchOption.TopDirectoryOnly);
 87         }
 88 
 89         public static void WriteStringToFile(string fileFullPath, bool isAppend ,string fileContent)
 90         {
 91             WriteStringToFile(fileFullPath, isAppend, fileContent, Encoding.Default);
 92         }
 93 
 94         public static void WriteStringToFile(string fileFullPath, bool isAppend, string fileContent, Encoding codeType)
 95         {
 96             //using (FileStream fileStream = new FileStream(fileFullPath, FileMode.OpenOrCreate, FileAccess.Write))
 97             using (StreamWriter sWriter = new StreamWriter(fileFullPath,isAppend,codeType))
 98             {
 99                 try
100                 {
101                     if (!File.Exists(fileFullPath))
102                         File.Create(fileFullPath);
103                     sWriter.Write(fileContent);
104                 }
105                 catch (Exception ex)
106                 {
107                     throw new IOException(ex.Message);
108                 }
109             }
110         }
111     }
112 }
View Code

 

//XMLHelper.cs, 用List模擬堆棧實現XML結點的操作

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.IO;
  7 using System.Xml;
  8 using System.Xml.XPath;
  9 using System.Xml.Schema;
 10 using ETLUtilityDAL.Enums;
 11 using System.Collections.Specialized;
 12 using ETLUtilityModel;
 13 using ETLUtilityModel.Enums;
 14 
 15 namespace ETLUtilityDAL.Common
 16 {
 17     public class XMLHelper
 18     {
 19         #region XMLStream
 20         	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在授權請求期間,如果IdentityServer需要用戶同意,則瀏覽器將被重定向到同意頁面。 同意用於允許最終用戶授予客戶端對資源(身份或API)的訪問許可權。這通常僅對第三方客戶端是必需的,並且可以在客戶端設置上按客戶端啟用/禁用。 28.1 確認頁 為了讓用戶同意,托管應用程式必須提供同意頁面。該 ...
  • 前言 在.Net程式開發過程中,我們經常會遇到如下場景: 編寫WinForm程式客戶端,需要查詢資料庫獲取數據,於是我們根據需求寫好了代碼後,點擊查詢,發現界面卡死,無法響應。經過調試,發現查詢資料庫這一步執行了很久,在此過程中,UI被阻塞,無法響應任何操作。 如何解決此問題?我們需要分析問題成因: ...
  • 1、HttpRuntime.Cache 相當於就是一個緩存具體實現類,這個類雖然被放在了 System.Web 命名空間下了。但是非 Web 應用也是可以拿來用的。 2、HttpContext.Cache 是對上述緩存類的封裝,由於封裝到了 HttpContext ,局限於只能在知道 HttpCon ...
  • Action與Func是APS.NET內置委托 // 無返回值的委托Action Action是無返回值的泛型委托 Action 表示無參,無返回值的委托 Action<int,string> 表示有傳入參數int,string無返回值的委托 Action<int,string,bool> 表示有傳 ...
  • 通用架構是所謂的聯合網關。在此方法中,IdentityServer充當一個或多個外部身份提供商的網關。 該架構具有以下優點: 您的應用程式只需要瞭解一個令牌服務(網關),並且屏蔽了有關連接到外部提供程式的所有詳細信息。這也意味著您可以添加或更改這些外部提供程式,而無需更新您的應用程式。 您控制網關( ...
  • 小地圖的基本概念眾所周知,小地圖(或雷達)是用於顯示周圍環境信息的。首先,小地圖是以主角為中心的。其次,小地圖上應該用圖標來代替真實的人物模型,因為小地圖通常很小,玩家可能無法看清真實的模型。大多數小地圖都是圓形的,所以本文也將創建圓形小地圖。通常小地圖周圍還會有一些按鈕和標簽,本文也會介紹。 創建 ...
  • 聯合註銷是指用戶使用外部身份提供程式登錄IdentityServer,然後用戶通過IdentityServer未知的工作流程註銷該外部身份提供程式的情況。當用戶註銷時,對IdentityServer進行通知將非常有用,這樣它就可以將用戶從IdentityServer和使用IdentityServer ...
  • 當用戶 "註銷" IdentityServer並且他們使用 "外部身份提供程式" 登錄時,可能會將其重定向到註銷外部提供程式。並非所有外部提供商都支持註銷,因為它取決於它們支持的協議和功能。 要檢測是否必須將用戶重定向到外部身份提供程式以進行註銷通常是通過使用 在IdentityServer中發佈到 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...