1. 程式人生 > >C#Datatable匯入sqlserver資料庫中,三種常見,快捷的方法

C#Datatable匯入sqlserver資料庫中,三種常見,快捷的方法

第一種,最常見的Executenonquery(返回影響的行數)sql是我的查詢插入語句,你可以換成你的!這種方式入庫,速度一般,大量資料時不提倡使用

        /// <summary>
        /// 通過table一個一個的插入
        /// </summary>
        /// <param name="table"></param>
        public static void Executenonquery(DataTable table)
        {
            foreach (DataRow itemRow in table.Rows)
            {
                //if exists(select * from dbo.ID where ENG = '')
                //   begin
                //        return;
                //                end
                //else
                //   begin
                //        INSERT INTO ID([ENG],[GB],[B5],[FILE],[MSG]) values('', '', '', '', '')
                //   end
                string sql = "if exists(select * from " + table.TableName + " where ENG = '" + itemRow["ENG"].ToString() + "') "+
                             " begin return; end else begin INSERT INTO " + table.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
                             "VALUES('" + itemRow["ENG"].ToString() + "'" +
                             ",'" + itemRow["GB"].ToString() + "'" +
                             ",'" + itemRow["B5"].ToString() + "'" +
                             ",'" + itemRow["FILE"].ToString() + "'" +
                             ",'" + itemRow["MSG"].ToString() + "') end";
                using (SqlConnection sqlconn = new SqlConnection(connectString))
                {
                    sqlconn.Open();

                    SqlCommand sqlcommand = new SqlCommand(sql, sqlconn);
                    sqlcommand.ExecuteNonQuery();
                    sqlconn.Close();
                }
            }
        }

2,通過adapter入庫,這種入庫,起先,你需要先把datatable放入到dataset中然後進行入庫,這種方式主要是對庫中對應的表進行增刪改,方便使用(效率只比第一種方式好點)

        /// <summary>
        /// 通過adapter更新資料庫
        /// </summary>
        /// <param name="dataset"></param>
        public static void DataadapterInssert(DataSet dataset)
        {
            if (dataset.Tables.Count > 0)
            {
                foreach (DataTable itemTable in dataset.Tables)
                {
                    SqlCommand insertcommand = new SqlCommand("if exists(select * from " + itemTable.TableName + " where ENG = @ENG) begin return; end "+
                                      " else begin INSERT INTO " + itemTable.TableName + "([ENG],[GB],[B5],[FILE],[MSG])" +
                                      "VALUES(@ENG, @GB,@B5,@FILE,@MSG) end", new SqlConnection(connectString));
                    insertcommand.Parameters.Add("@ENG", SqlDbType.VarChar, 100, "ENG");
                    insertcommand.Parameters.Add("@GB", SqlDbType.VarChar, 100, "GB");
                    insertcommand.Parameters.Add("@B5", SqlDbType.VarChar, 200, "B5");
                    insertcommand.Parameters.Add("@FILE", SqlDbType.VarChar, 200, "FILE");
                    insertcommand.Parameters.Add("@MSG", SqlDbType.VarChar, 100, "MSG");

                    SqlDataAdapter sqldataadapter = new SqlDataAdapter();
                    sqldataadapter.InsertCommand = insertcommand;

                    sqldataadapter.Update(dataset, itemTable.TableName);
                }
            }
            
        }

3,重頭戲都在最後,這種方式速度比前兩種快很多,適合用於大量資料插入更新,也將datatable放入dataset中然後通過遍歷,將datatable複製到資料庫中對應的表中,快速便捷

        /// <summary>
        /// 通過SqlBulkCopy複製table資料到資料庫
        /// </summary>
        /// <param name="dataset"></param>
        public static void SqlbulkcopyInsert(DataSet dataset)
        {
            string ie;
            if (dataset.Tables.Count > 0)
            {
                foreach (DataTable itemTable in dataset.Tables)
                {
                    SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectString, SqlBulkCopyOptions.UseInternalTransaction);
                    sqlbulkcopy.DestinationTableName = itemTable.TableName;//資料庫中的表名
                    for (int i = 0; i < itemTable.Rows.Count; i++)
                    {
                        ie = itemTable.Rows[i][2].ToString();
                    }
                    sqlbulkcopy.WriteToServer(itemTable);
                }
            }
            
        }