예제 #1
0
        /// <summary>
        /// 参数非DT插入 不支持Oracle
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="BatchSize"></param>
        //private void BulkInsert_2(IDataReader reader, int BatchSize)
        //{
        //    if (reader == null) return;
        //    //每次插入的条数
        //    if (BatchSize <= 0)
        //    {
        //        BatchSize = 100;
        //    }
        //    Dictionary<string, string> kvSD = this.initReader(reader);
        //    string[] D = new string[kvSD.Count];
        //    string[] S = new string[kvSD.Count];
        //    kvSD.Values.CopyTo(D, 0);
        //    kvSD.Keys.CopyTo(S, 0);

        //    //处理字段名
        //    string open = "", close = "";
        //    string strNames = "", strParameterNames = "";
        //    for (int i = 0; i < D.Length; i++)
        //    {
        //        strNames += open + D[i] + close + ",";
        //        strParameterNames += Regex.Replace(D[i].Replace(" ", ""), "^\"|\"$", "") + ",";
        //    }
        //    string[] parameterNames = strParameterNames.Split(',');

        //    this._client.Open();
        //    bool ifTran = true;
        //    try
        //    {
        //        this._client.BeginTransaction();
        //    }
        //    catch
        //    {
        //        ifTran = false;
        //    }
        //    int count = 0;
        //    string strValue = "";
        //    string strParaName = "";
        //    int intReaded = 0;
        //    while (reader.Read())
        //    {
        //        intReaded++;
        //        if (this.RowsCopied != null && this.NotifyAfter != 0)
        //        {
        //            if (this.NotifyAfter > 0 && intReaded % this.NotifyAfter == 0)
        //            {
        //                this.RowsCopied?.BeginInvoke(this, new RowsCopiedEventArgs(intReaded), null, null);
        //            }
        //        }
        //        for (int j = 0; j < D.Length; j++)
        //        {
        //            if (j == 0)
        //            {
        //                if (count == 0)
        //                {
        //                    strValue = "(";
        //                }
        //                else
        //                {
        //                    strValue += ",(";
        //                }
        //            }
        //            strParaName = string.Format("\"@{0}a_b{1}\"", parameterNames[j], count);
        //            int index = reader.GetOrdinal(S[j]);
        //            this._client.Parameters.AddWithValue(strParaName, reader.GetValue(index));
        //            if (j == D.Length - 1)
        //            {
        //                strValue += strParaName;
        //                strValue += ")";
        //            }
        //            else
        //            {
        //                strValue += strParaName + ",";
        //            }
        //        }
        //        try
        //        {
        //            if (++count >= BatchSize)
        //            {
        //                count = 0;
        //                this._client.CommandText = string.Format(" INSERT INTO {0} ({1}) VALUES {2} ", this.DestinationTableName, strNames.Substring(0, strNames.Length - 1), strValue);
        //                this._client.ExecuteNonQuery();
        //                this._client.Parameters.Clear();
        //            }
        //        }
        //        catch { }
        //        if (ifTran && (intReaded + 1) % BatchSize == 0 && this._client.Command.Transaction != null)
        //        {
        //            this._client.Commit();
        //        }
        //    }

        //    if (ifTran && this._client.Command.Transaction != null)
        //    {
        //        this._client.Commit();
        //    }
        //    this._client.Dispose();

        //}



        /// <summary>
        /// 使用DataTable批量插入
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="BatchSize"></param>
        private void _Bulk_Insert(IDataReader reader, int BatchSize)
        {
            if (reader == null)
            {
                return;
            }
            if (BatchSize <= 0)
            {
                BatchSize = 2000;
            }
            int       intReaded = 0;
            DataTable dt0       = new DataTable();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                dt0.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
            }
            DataTable           dt             = dt0.Copy();
            write               _writeToServer = new write(this.InsertToServer);
            List <IAsyncResult> listIR         = new List <IAsyncResult>();
            IAsyncResult        ar             = null;
            DateTime            time           = DateTime.Now;

            while (reader.Read())
            {
                intReaded++;
                DataRow dr = dt.NewRow();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    dr[i] = reader.GetValue(i);
                }
                dt.Rows.Add(dr);
                if (intReaded % BatchSize == 0)
                {
                    DateTime time2 = DateTime.Now;
                    TimeSpan ts    = time2 - time;
                    if (ar != null && !ar.IsCompleted)
                    {
                        //ar.AsyncWaitHandle.WaitOne();
                        _writeToServer.EndInvoke(ar);
                    }
                    DateTime time3 = DateTime.Now;
                    TimeSpan ts2   = time2 - time3;
                    ar   = _writeToServer.BeginInvoke(dt, null, null);
                    dt   = dt0.Copy();
                    time = DateTime.Now;
                }

                if (intReaded % this.NotifyAfter == 0)
                {
                    this.RowsCopied?.BeginInvoke(this, new RowsCopiedEventArgs(intReaded), null, null);
                }
            }
            if (dt.Rows.Count > 0)
            {
                if (ar != null && !ar.IsCompleted)
                {
                    //ar.AsyncWaitHandle.WaitOne();
                    _writeToServer.EndInvoke(ar);
                }
                ar = _writeToServer.BeginInvoke(dt, null, null);
            }
            if (ar != null && !ar.IsCompleted)
            {
                //ar.AsyncWaitHandle.WaitOne();
                _writeToServer.EndInvoke(ar);
            }

            this.RowsCopied?.BeginInvoke(this, new RowsCopiedEventArgs(intReaded), null, null);
            dt = null;
        }