SQL Server數據庫 857萬條數據導入用時4分鐘。【數據庫訪問類】

using System;
using System.Data;
using System.Data.SqlClient;

public class SqlDbHelper
    {
        /// <summary>
        /// 連接字符串
        /// </summary>
        public static readonly string connectionString = "data source=152.136.162.148;initial catalog=LingLongDB_02_Pre;user id=LLDB_Pre;password=Nqm0q8letzk8dBcV8cB4nbPsDxeP3I;";

        #region ExecuteNonQuery命令
        /// <summary>
        /// 對數據庫執行增、刪、改命令
        /// </summary>
        /// <param name="safeSql">T-Sql語句</param>
        /// <returns>受影響的記錄數</returns>
        public static int ExecuteNonQuery(string safeSql)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                Connection.Open();
                SqlTransaction trans = Connection.BeginTransaction();
                try
                {
                    SqlCommand cmd = new SqlCommand(safeSql, Connection);
                    cmd.Transaction = trans;

                    if (Connection.State != ConnectionState.Open)
                    {
                        Connection.Open();
                    }
                    int result = cmd.ExecuteNonQuery();
                    trans.Commit();
                    return result;
                }
                catch
                {
                    trans.Rollback();
                    return 0;
                }
            }
        }

        /// <summary>
        /// 對數據庫執行增、刪、改命令
        /// </summary>
        /// <param name="sql">T-Sql語句</param>
        /// <param name="values">參數數組</param>
        /// <returns>受影響的記錄數</returns>
        public static int ExecuteNonQuery(string sql, SqlParameter[] values)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                Connection.Open();
                SqlTransaction trans = Connection.BeginTransaction();
                try
                {
                    SqlCommand cmd = new SqlCommand(sql, Connection);
                    cmd.Transaction = trans;
                    cmd.Parameters.AddRange(values);
                    if (Connection.State != ConnectionState.Open)
                    {
                        Connection.Open();
                    }
                    int result = cmd.ExecuteNonQuery();
                    trans.Commit();
                    return result;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    return 0;
                }
            }
        }
        #endregion

        #region ExecuteScalar命令
        /// <summary>
        /// 查詢結果集中第一行第一列的值
        /// </summary>
        /// <param name="safeSql">T-Sql語句</param>
        /// <returns>第一行第一列的值</returns>
        public static int ExecuteScalar(string safeSql)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
        }

        /// <summary>
        /// 查詢結果集中第一行第一列的值
        /// </summary>
        /// <param name="sql">T-Sql語句</param>
        /// <param name="values">參數數組</param>
        /// <returns>第一行第一列的值</returns>
        public static int ExecuteScalar(string sql, SqlParameter[] values)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.Parameters.AddRange(values);
                int result = Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
        }
        #endregion

        #region ExecuteReader命令
        /// <summary>
        /// 創建數據讀取器
        /// </summary>
        /// <param name="safeSql">T-Sql語句</param>
        /// <param name="Connection">數據庫連接</param>
        /// <returns>數據讀取器對象</returns>
        public static SqlDataReader ExecuteReader(string safeSql, SqlConnection Connection)
        {
            if (Connection.State != ConnectionState.Open)
                Connection.Open();
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        /// <summary>
        /// 創建數據讀取器
        /// </summary>
        /// <param name="sql">T-Sql語句</param>
        /// <param name="values">參數數組</param>
        /// <param name="Connection">數據庫連接</param>
        /// <returns>數據讀取器</returns>
        public static SqlDataReader ExecuteReader(string sql, SqlParameter[] values, SqlConnection Connection)
        {
            if (Connection.State != ConnectionState.Open)
                Connection.Open();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }
        #endregion

        #region ExecuteDataTable命令
        /// <summary>
        /// 執行指定數據庫連接對象的命令,指定存儲過程參數,返回DataTable
        /// </summary>
        /// <param name="type">命令類型(T-Sql語句或者存儲過程)</param>
        /// <param name="safeSql">T-Sql語句或者存儲過程的名稱</param>
        /// <param name="values">參數數組</param>
        /// <returns>結果集DataTable</returns>
        public static DataTable ExecuteDataTable(CommandType type, string safeSql, params SqlParameter[] values)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                cmd.CommandType = type;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }

        /// <summary>
        /// 執行指定數據庫連接對象的命令,指定存儲過程參數,返回DataTable
        /// </summary>
        /// <param name="safeSql">T-Sql語句</param>
        /// <returns>結果集DataTable</returns>
        public static DataTable ExecuteDataTable(string safeSql)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                try
                {
                    da.Fill(ds);
                }
                catch (Exception ex)
                {

                }
                return ds.Tables[0];
            }
        }

        /// <summary>
        /// 執行指定數據庫連接對象的命令,指定存儲過程參數,返回DataTable
        /// </summary>
        /// <param name="sql">T-Sql語句</param>
        /// <param name="values">參數數組</param>
        /// <returns>結果集DataTable</returns>
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] values)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, Connection);
                cmd.CommandTimeout = 0;
                cmd.Parameters.AddRange(values);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
        #endregion

        #region GetDataSet命令
        /// <summary>
        /// 取出數據
        /// </summary>
        /// <param name="safeSql">sql語句</param>
        /// <param name="tabName">DataTable別名</param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string safeSql, string tabName, params SqlParameter[] values)
        {
            using (SqlConnection Connection = new SqlConnection(connectionString))
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(safeSql, Connection);

                if (values != null)
                    cmd.Parameters.AddRange(values);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                try
                {
                    da.Fill(ds, tabName);
                }
                catch (Exception ex)
                {

                }
                return ds;
            }
        }
        #endregion

        #region ExecureData 命令
        /// <summary>
        /// 批量修改數據
        /// </summary>
        /// <param name="ds">修改過的DataSet</param>
        /// <param name="strTblName">表名</param>
        /// <returns></returns>
        public static int ExecureData(DataSet ds, string strTblName)
        {
            try
            {
                //創建一個數據庫連接
                using (SqlConnection Connection = new SqlConnection(connectionString))
                {
                    if (Connection.State != ConnectionState.Open)
                        Connection.Open();

                    //創建一個用於填充DataSet的對象
                    SqlCommand myCommand = new SqlCommand("SELECT * FROM " + strTblName, Connection);
                    SqlDataAdapter myAdapter = new SqlDataAdapter();
                    //獲取SQL語句,用於在數據庫中選擇記錄
                    myAdapter.SelectCommand = myCommand;

                    //自動生成單表命令,用於將對DataSet所做的更改與數據庫更改相對應
                    SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);

                    return myAdapter.Update(ds, strTblName);  //更新ds數據
                }

            }
            catch (Exception err)
            {
                throw err;
            }
        }

        #endregion





        public static void TableValuedToDB(DataTable dt)
        {
            SqlConnection sqlConn = new SqlConnection(connectionString);
            const string TSqlStatement =
             "insert into [BarCodeRecordes9] (is_add,qr_code,integral,created_time,id,terminal_code)" +
             " SELECT is_add,qr_code,integral,created_time,id,terminal_code" +
             " FROM @NewBulkTestTvp AS nc";
            SqlCommand cmd = new SqlCommand(TSqlStatement, sqlConn);
            SqlParameter catParam = cmd.Parameters.AddWithValue("@NewBulkTestTvp", dt);
            catParam.SqlDbType = SqlDbType.Structured;
            //表值參數的名字叫BulkUdt,在上面的建立測試環境的SQL中有。  
            catParam.TypeName = "dbo.BulkUdt";
            try
            {
                sqlConn.Open();
                if (dt != null && dt.Rows.Count != 0)
                {
                    cmd.ExecuteNonQuery();
                }
            }
           catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sqlConn.Close();
            }
        }
          
    }

  使用

TableValuedToDB方法導入 每次導入一百萬條,依次分頁導入了9次。

大數據(857萬條數據)導入使用時間爲:  362708 毫秒,  大概4分鐘多一點。

TableValuedToDB(DataTable dt) 方法中 dt注意一定要和 數據庫 類型 BulkUdt 中定義的列保持一致。


保證你的數據庫存在該類型。自定義表類型。

CREATE TYPE dbo.BulkUdt AS TABLE 
(
	    [is_add] [bit] NULL,
        [qr_code] [nvarchar](255) NULL,
        [integral] [decimal](18, 2) NULL,
        [created_time] [datetime] NULL,
        [id] [bigint] NOT NULL PRIMARY KEY ,
		[terminal_code] [nvarchar](255) NULL
)

  



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章