首页 > 代码库 > 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>
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();
}
}
}
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;
}
}
}
{
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批量插入)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。