有三种不同的方法其中update insert 相比于SqlBulkCopy 都比较慢
UserBll类 需要引用UserDal类
UserDal类 需要引用 sqlHelper33类
窗体程序 需要引用UserBll类
1.首先新建一个UserBll 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sanCeng.Model33;
using sannCeng.DAL33;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
using System.IO;
namespace sanCeng.BLL33
{
public class UserBll
{
private UserDal uDAL = new UserDal();
public void EXcelAdd2()
{
string TargetFileNamePath = @"C:\Users\xg\Desktop\test.xls";
DataTable dataTable = Import(TargetFileNamePath);
uDAL.DataTableToSQLServer(dataTable);
}
public static DataTable Import(string strFileName)
{
DataTable dt = new DataTable();
HSSFWorkbook hssfworkbook;
using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
{
hssfworkbook = new HSSFWorkbook(file);
}
ISheet sheet = hssfworkbook.GetSheetAt(0);
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
IRow headerRow = sheet.GetRow(0);
int cellCount = headerRow.LastCellNum;
for (int j = 0; j < cellCount; j++)
{
ICell cell = headerRow.GetCell(j);
dt.Columns.Add(cell.ToString());
}
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
DataRow dataRow = dt.NewRow();
for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
{
dataRow[j] = row.GetCell(j).ToString();
}
}
dt.Rows.Add(dataRow);
}
return dt;
}
}
}
2.新建一个UserDal类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using sanCeng.Model33;
using System.Data;
namespace sannCeng.DAL33
{
public class UserDal
{
public void DataTableToSQLServer(DataTable dt)
{
sqlHelper33.DataTableToSQLServer(dt);
}
}
}
3新建一个sqlHelper33类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace sannCeng.DAL33
{
class sqlHelper33
{
//获取配置文件
private static string connStr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
//批量插入
public static void DataTableToSQLServer(DataTable dt)
{
using (SqlConnection destinationConnection = new SqlConnection(connStr))
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
{
bulkCopy.BatchSize = dt.Rows.Count;
bulkCopy.DestinationTableName = "T_Users";//要插入的表的表名
//bulkCopy.ColumnMappings.Add("sid", "sID");//映射字段名:DataTable列名 数据库中的字段名
bulkCopy.ColumnMappings.Add("Age", "Age");
bulkCopy.ColumnMappings.Add("UserName", "UserName");
bulkCopy.ColumnMappings.Add("Password", "Password");
bulkCopy.ColumnMappings.Add("PhoneNum", "PhoneNum");
bulkCopy.ColumnMappings.Add("IsDeleted", "IsDeleted");
bulkCopy.WriteToServer(dt);
}
}
}
}
}
4.我用是窗体程序调用 ,控制台其实更方便。
using sanCeng.BLL33;
using sanCeng.Model33;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sanCeng.UI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
UserBll u2 = new UserBll();
u2.EXcelAdd2();
MessageBox.Show("成功");
}
}
网友评论