SqlBlukCopy

  • 做大量数据插入的时候非常快。可以用飞的感觉。
  • 支持DataTable作为数据源,十分适合数据迁移。

    /// <summary>
    /// Bulk Insert function
    /// </summary>
    /// <param name="dt"></param>
    public void bulkInertInfo(DataTable dt)
    {
        string server = "";
        string db = "";
        string userid = "";
        string psw = "";
        string desConnString = string.Format("Server={0};DataBase={1};user id={2};password={3};pooling=false", server, db, userid, psw);
        SqlConnection desConnection = new SqlConnection();
        SqlCommand sqlcmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        SqlBulkCopy sbc = new SqlBulkCopy(desConnString, SqlBulkCopyOptions.UseInternalTransaction);
        sbc.BulkCopyTimeout = 5000; // time out
        sbc.BatchSize = dt.Rows.Count; // the size of bulk.
    
        try
        {
            sbc.DestinationTableName = "iRealWeather";//the target tablename
            sbc.WriteToServer(dt);
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }
        finally
        {       
            desConnection.Close();
    
        }
    }