首页 > 代码库 > sqlhelper(带sqlbulkcopy批量插入)

sqlhelper(带sqlbulkcopy批量插入)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataController
{
    static class SqlHelper
    {
        const int SQLTimeout = 120;
        const string Connection = "SERVER=127.0.0.1;USER ID=sa;PWD=password;DATABASE=database;CONNECT TIMEOUT=120;";
        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="dt">要写入的datatable</param>
        /// <param name="tablename">要插入数据库的表名</param>
   /// <param name="columnnames">要插入数据库的列名</param>
        public static void SqlBulkCopyInsert(DataTable dt, string tablename,List<string> columnnames)
        {
            using (SqlConnection conn = new SqlConnection(Connection))
            {
                conn.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
                {
                    bulkCopy.DestinationTableName = tablename;
                    foreach (string str in columnnames)
                    {
                        bulkCopy.ColumnMappings.Add(str, str);
                    }
                    bulkCopy.WriteToServer(dt);
                }
            }
        }

        public static DataTable SelectInfo(string sql)
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(Connection))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandTimeout = SQLTimeout;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                       
                        sda.Fill(ds);
                    }
                }
            }
            return ds.Tables[0];
        }
        public static int AddDelUpdate(string sql)
        {
            using (SqlConnection conn = new SqlConnection(Connection))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandTimeout = SQLTimeout;
                    return cmd.ExecuteNonQuery();
                }
            }
            return 0;
        }
        public static object ExecuteScalar(string sql)
        {
            using (SqlConnection conn = new SqlConnection(Connection))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandTimeout = SQLTimeout; 
                    return cmd.ExecuteScalar();
                }
            }
        }
   /// <summary>
        /// 通过表名获得所有列名 
        /// </summary> 
       /// <param name="tablename">要插入数据库的表名</param>
        public static List<string> GetTableColumnNames(string tablename)
        {

            string sql = "select name from syscolumns where id=(select max(id) from sysobjects where xtype=‘u‘ and name=‘" + tablename + "‘)";
            DataTable dt = SelectInfo(sql);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                columnNames.Add(dt.Rows[i][0].ToString());
            }
            return columnNames;
        }
    }
}

sqlhelper(带sqlbulkcopy批量插入)