求将csv文件导入数据库(sql server)的代码,最好是C#
感谢majunbopm!我想要的是C#代码,包括数据库的连接,将CSV文件导入到sqlserver数据库我已自己解决,感谢majunbopm,虽然你的答案不是我想要的,不...
感谢majunbopm!我想要的是C#代码,包括数据库的连接,将CSV文件导入到sql server数据库
我已自己解决,感谢majunbopm,虽然你的答案不是我想要的,不过我还是把你的答案设为最佳答案! 展开
我已自己解决,感谢majunbopm,虽然你的答案不是我想要的,不过我还是把你的答案设为最佳答案! 展开
1个回答
展开全部
public class CsvFileImportDAO
implements FileImportDAO {
/**
* 要导入的文件对象
*/
CsvFile csvFile = new CsvFile();
/**
* 导入成功的记录数
*/
private int countImported;
/**
* 构造器
*/
public CsvFileImportDAO() {
}
public ArrayList parseFile(MyFile myFile) throws CustomException {
csvFile = (CsvFile) myFile;
/**
* 保存由行数据的到的SysUserInfo对象的实例
*/
ArrayList listResult = new ArrayList();
File file = new File(csvFile.getFileName());
String line = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
/**
* 如果文件包含表头, 则跳过
*/
if (csvFile.isIncludeHead()) {
line = in.readLine();
}
while (null != (line = in.readLine())) {
SysUserInfo sysUserInfo = this.parseLine(line, csvFile.getColumnCount(),
csvFile.getDelimeter());
if (sysUserInfo != null) {
listResult.add(sysUserInfo);
}
}
return listResult;
}
catch (IOException e) {
throw new CustomException(CustomException.IO_READ_FAILURE, e.getMessage());
}
catch (CustomException e) {
throw e;
}
}
private SysUserInfo parseLine(String line, int columnCount, String delim) throws
CustomException {
StringTokenizer st = new StringTokenizer(line, delim);
/**
* 判断该行数据是否合法, 如果不合法则返回一个null对象
*/
if (!this.isLineLegal(line, columnCount, delim)) {
return null;
}
/**
* 开始处理各个字段
*/
String word = null;
SysUserInfo sysUserInfo = new SysUserInfo();
/* * 身份证号
* 姓名
* 性别
* 出生日期
* 地址
* 邮编
* 办公电话
* 家庭电话
* 移动电话
* Email
* 个人简介
* 职务编码
* 学历编码
* 地区编码
* 部门编码
*/
//处理身份证号字段
sysUserInfo.setIdCardNumber(st.nextToken().trim());
//处理姓名字段
sysUserInfo.setName(st.nextToken().trim());
//处理性别字段 - 文件中性别以 0 = 女 1 = 男
sysUserInfo.setSex( (st.nextToken().trim().compareToIgnoreCase("女") == 0) ? false : true);
//取得出身年月
try {
sysUserInfo.setBornDate(Tools.stringToDate(st.nextToken().trim(),
Constant.PUB_LONG_DATE_PATTERN));
}
catch (CustomException e) {
throw e;
}
//地址
sysUserInfo.setAddress(st.nextToken());
//邮编
sysUserInfo.setPostalCode(st.nextToken());
sysUserInfo.setOfficeTel(st.nextToken());
sysUserInfo.setHomeTel(st.nextToken());
sysUserInfo.setMobileTel(st.nextToken());
sysUserInfo.setEmail(st.nextToken());
sysUserInfo.setIntroduction(st.nextToken());
Duty duty = new Duty();
duty.setDutyId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDuty(duty);
Degree degree = new Degree();
degree.setDegreeId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDegree(degree);
ChinaArea chinaArea = new ChinaArea();
chinaArea.setAreaId(Integer.parseInt(st.nextToken()));
sysUserInfo.setArea(chinaArea);
Department department = new Department();
department.setId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDepartment(department);
return sysUserInfo;
}
/**
* 判断某一行的数据是否合法.
* @return boolean 合法,则返回true
*/
private boolean isLineLegal(String line, int columnCount, String delim) {
boolean isLegal = true;
StringTokenizer st = new StringTokenizer(line, delim);
//字段个数不为定义的字段个数, 则不合法
if (st.countTokens() != columnCount) {
this.saveError(line);
return (isLegal = false);
}
//如果有一个字段值不合法, 则认为该行数据不合法
while (st.hasMoreTokens()) {
String word = st.nextToken();
if (word == null || word.trim().compareTo("") == 0) {
isLegal = false;
break;
}
}
if (!isLegal) {
this.saveError(line);
}
return isLegal;
}
/**
* 保存数据不正确的行
* @param errorLine String 数据不正确的行
*/
private void saveError(String errorLine) {
// this.listRecFailed.add(errorLine);
this.csvFile.setErrorLine(errorLine);
}
}
参考一下吧
implements FileImportDAO {
/**
* 要导入的文件对象
*/
CsvFile csvFile = new CsvFile();
/**
* 导入成功的记录数
*/
private int countImported;
/**
* 构造器
*/
public CsvFileImportDAO() {
}
public ArrayList parseFile(MyFile myFile) throws CustomException {
csvFile = (CsvFile) myFile;
/**
* 保存由行数据的到的SysUserInfo对象的实例
*/
ArrayList listResult = new ArrayList();
File file = new File(csvFile.getFileName());
String line = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new
FileInputStream(file)));
/**
* 如果文件包含表头, 则跳过
*/
if (csvFile.isIncludeHead()) {
line = in.readLine();
}
while (null != (line = in.readLine())) {
SysUserInfo sysUserInfo = this.parseLine(line, csvFile.getColumnCount(),
csvFile.getDelimeter());
if (sysUserInfo != null) {
listResult.add(sysUserInfo);
}
}
return listResult;
}
catch (IOException e) {
throw new CustomException(CustomException.IO_READ_FAILURE, e.getMessage());
}
catch (CustomException e) {
throw e;
}
}
private SysUserInfo parseLine(String line, int columnCount, String delim) throws
CustomException {
StringTokenizer st = new StringTokenizer(line, delim);
/**
* 判断该行数据是否合法, 如果不合法则返回一个null对象
*/
if (!this.isLineLegal(line, columnCount, delim)) {
return null;
}
/**
* 开始处理各个字段
*/
String word = null;
SysUserInfo sysUserInfo = new SysUserInfo();
/* * 身份证号
* 姓名
* 性别
* 出生日期
* 地址
* 邮编
* 办公电话
* 家庭电话
* 移动电话
* 个人简介
* 职务编码
* 学历编码
* 地区编码
* 部门编码
*/
//处理身份证号字段
sysUserInfo.setIdCardNumber(st.nextToken().trim());
//处理姓名字段
sysUserInfo.setName(st.nextToken().trim());
//处理性别字段 - 文件中性别以 0 = 女 1 = 男
sysUserInfo.setSex( (st.nextToken().trim().compareToIgnoreCase("女") == 0) ? false : true);
//取得出身年月
try {
sysUserInfo.setBornDate(Tools.stringToDate(st.nextToken().trim(),
Constant.PUB_LONG_DATE_PATTERN));
}
catch (CustomException e) {
throw e;
}
//地址
sysUserInfo.setAddress(st.nextToken());
//邮编
sysUserInfo.setPostalCode(st.nextToken());
sysUserInfo.setOfficeTel(st.nextToken());
sysUserInfo.setHomeTel(st.nextToken());
sysUserInfo.setMobileTel(st.nextToken());
sysUserInfo.setEmail(st.nextToken());
sysUserInfo.setIntroduction(st.nextToken());
Duty duty = new Duty();
duty.setDutyId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDuty(duty);
Degree degree = new Degree();
degree.setDegreeId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDegree(degree);
ChinaArea chinaArea = new ChinaArea();
chinaArea.setAreaId(Integer.parseInt(st.nextToken()));
sysUserInfo.setArea(chinaArea);
Department department = new Department();
department.setId(Integer.parseInt(st.nextToken()));
sysUserInfo.setDepartment(department);
return sysUserInfo;
}
/**
* 判断某一行的数据是否合法.
* @return boolean 合法,则返回true
*/
private boolean isLineLegal(String line, int columnCount, String delim) {
boolean isLegal = true;
StringTokenizer st = new StringTokenizer(line, delim);
//字段个数不为定义的字段个数, 则不合法
if (st.countTokens() != columnCount) {
this.saveError(line);
return (isLegal = false);
}
//如果有一个字段值不合法, 则认为该行数据不合法
while (st.hasMoreTokens()) {
String word = st.nextToken();
if (word == null || word.trim().compareTo("") == 0) {
isLegal = false;
break;
}
}
if (!isLegal) {
this.saveError(line);
}
return isLegal;
}
/**
* 保存数据不正确的行
* @param errorLine String 数据不正确的行
*/
private void saveError(String errorLine) {
// this.listRecFailed.add(errorLine);
this.csvFile.setErrorLine(errorLine);
}
}
参考一下吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询