大數據批量導入,解決辦法,實踐從定時從 sqlserver 批量同步數據到 mySql


c#代碼,批量導入數據代碼

public class MySql_Target : ZFCommon.DataAccesser.Base.DABase
    {

        public MySql_Target()
        {
            this.InitDataAccesser(ZFCommon.DataAccesser.DatabaseType.MySql, ReadConfig.TargetConnection);
        }
       ///大批量數據插入,返回成功插入行數  
        /// </summary>  
        /// <param name="connectionString">數據庫連接字符串</param>  
        /// <param name="table">數據表</param>  
        /// <returns>返回成功插入行數</returns>  
        public  int BulkInsert( DataTable table)
        {
            if (string.IsNullOrEmpty(table.TableName)) throw new Exception("請給DataTable的TableName屬性附上表名稱");
            if (table.Rows.Count == 0) return 0;
            int insertCount = 0;
            string tmpPath = Path.GetTempFileName();
            string csv = DataTableToCsv(table);
            File.WriteAllText(tmpPath, csv);
            // MySqlTransaction tran = null;  

            using (MySqlConnection conn = new MySqlConnection(ReadConfig.TargetConnection))
            {

                try
                {

                    conn.Open();
                    //tran = conn.BeginTransaction();  
                    MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
                    {
                        FieldTerminator = ",",
                        FieldQuotationCharacter = '"',
                        EscapeCharacter = '"',
                        LineTerminator = "\r\n",
                        FileName = tmpPath,
                        NumberOfLinesToSkip = 0,
                        TableName = table.TableName,
                    };
                    //bulk.Columns.AddRange(table.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToArray());  
                    insertCount = bulk.Load();
                    // tran.Commit();  
                }
                catch (MySqlException ex)
                {
                    // if (tran != null) tran.Rollback();  
                    throw ex;
                }
            }
            File.Delete(tmpPath);
            return insertCount;
        }

        ///將DataTable轉換為標准的CSV  
        /// </summary>  
        /// <param name="table">數據表</param>  
        /// <returns>返回標准的CSV</returns>  
        private static string DataTableToCsv(DataTable table)
        {
            //以半角逗號(即,)作分隔符,列為空也要表達其存在。  
            //列內容如存在半角逗號(即,)則用半角引號(即"")將該字段值包含起來。  
            //列內容如存在半角引號(即")則應替換成半角雙引號("")轉義,並用半角引號(即"")將該字段值包含起來。  
            StringBuilder sb = new StringBuilder();
            DataColumn colum;
            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    colum = table.Columns[i];
                    if (i != 0) sb.Append(",");
                    if (colum.DataType == typeof(string) && row[colum].ToString().Contains(","))
                    {
                        sb.Append("\"" + row[colum].ToString().Replace("\"", "\"\"") + "\"");
                    }
                    else sb.Append(row[colum].ToString());
                }
                sb.AppendLine();
            }


            return sb.ToString();
        }  


    }

從sqlserver讀取數據,並調用 上面導入代碼

  //學生課表
        public int SyncStu_Course()
        {
            DataTable dataTable = this.GetDataTable("select * from V_Schedule_Stud");
            dataTable.TableName = "V_Schedule_Stud";
            MySql_Target tarDb = new MySql_Target();
            return tarDb.BulkInsert(dataTable);
        }

 

運行速度高效: 63萬條數據 只需5分鍾 導出並導入完成!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM