将DataTable中的数据导入到数据库中
上次在根据excel的文件的路径提取其中表的数据到DataSet中 一文中介绍了将Excel文件中的数据读取到DataSet中的方法,今天我来介绍下我曾经在项目中用到的一个将DataTable中的数据导入到数据库中的方法和将DataTable中的数据写入数据库并过滤掉重复数据的方法。代码可以给大家作为Demo看下,如果需要应用的话,可能需要进行
小小的改动即可。
/// <summary>
/// 将DataTable中数据写入数据库中
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static bool WriteDataToDB(DataTable dt)
{
if (dt==null || dt.Rows.Count == 0)
{
return true;
}
string tname = dt.TableName;
string colNames = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
colNames += dt.Columns[i].ColumnName + ",";
}
colNames = colNames.TrimEnd(',');
string cmd = "";
string colValues;
string cmdmode = string.Format("insert into {0}({1}) values({{0}});", tname, colNames);
for (int i = 0; i < dt.Rows.Count; i++)
{
colValues = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].GetType() == typeof(DBNull))
{
colValues += "NULL,";
continue;
}
if (dt.Columns[j].DataType == typeof(string))
colValues += string.Format("'{0}',", dt.Rows[i][j]);
else if (dt.Columns[j].DataType == typeof(int) || dt.Columns[j].DataType == typeof(float) || dt.Columns[j].DataType == typeof(double))
{
colValues += string.Format("{0},", dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(DateTime))
{
colValues += string.Format("cast('{0}' as datetime),", dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(bool))
{
colValues += string.Format("{0},", dt.Rows[i][j].ToString());
}
else
colValues += string.Format("'{0}',", dt.Rows[i][j]);
}
cmd = string.Format(cmdmode, colValues.TrimEnd(','));
}
int ret = 0;
try
{
ret = DbHelperSQL.ExecuteSql(cmd);
}
catch (Exception e)
{
//写错误日志...
string strOuput = string.Format("向数据库中写数据失败,错误信息:{0},异常{1}\n", e.Message, e.InnerException);
//将信息写入到日志输出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
}
if (ret == -1)
{
return false;
}
return true;
}
/// <summary>
/// 写入基础数据,并删除其中的重复的项目
/// </summary>
/// <param name="dt"></param>
/// <param name="KeyName">主键</param>
/// <param name="icol">主键所在的列</param>
/// <returns></returns>
public static bool WriteDataToDB(DataTable dt, string KeyName, int icol)
{
//删除数据库中的重复项目
string mKeyStr = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
mKeyStr += "'" + dt.Rows[i][icol] + "',";
}
mKeyStr = mKeyStr.Trim(',');
string sqlStr = "Delete from " + dt.TableName + " where " + KeyName + " in (" + mKeyStr + ")";
DbHelperSQL.ExecuteSql(sqlStr);
//向数据库中写入新的数据
string tname = dt.TableName;
string colNames = "";
for (int i = 0; i < dt.Columns.Count; i++)
{
colNames += dt.Columns[i].ColumnName + ",";
}
colNames = colNames + "CreateDate ";
string cmd = "";
string colValues;
string cmdmode = string.Format("insert into {0}({1}) values({{0}});", tname, colNames);
for (int i = 0; i < dt.Rows.Count; i++)
{
colValues = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Rows[i][j].GetType() == typeof(DBNull))
{
colValues += "NULL,";
continue;
}
if (dt.Columns[j].DataType == typeof(string))
{
colValues += string.Format("'{0}',", dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(int) || dt.Columns[j].DataType == typeof(float) || dt.Columns[j].DataType == typeof(double))
{
colValues += string.Format("{0},", dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(DateTime))
{
colValues += string.Format("cast('{0}' as datetime),", dt.Rows[i][j]);
}
else if (dt.Columns[j].DataType == typeof(bool))
{
colValues += string.Format("{0},", dt.Rows[i][j].ToString());
}
else
colValues += string.Format("'{0}',", dt.Rows[i][j]);
}
colValues += "getdate()";
cmd = string.Format(cmdmode, colValues);
int ret = 0;
try
{
ret = DbHelperSQL.ExecuteSql(cmd);
}
catch (Exception e)
{
//写错误日志...
string strOuput = string.Format("向数据库中写数据失败,错误信息:{0},异常{1}\n", e.Message, e.InnerException);
//将信息写入到日志输出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
}
if (ret == -1)
{
return false;
}
}
return true;
}
转载于:https://www.cnblogs.com/kevinGao/archive/2011/09/23/2186441.html
将DataTable中的数据导入到数据库中相关推荐
- 查询oracle数据库的表格数据类型,excel表格中如何查询数据库数据类型-我想把excel表格中的数据导入oracle数据库中,想在......
在excel表里,什么是:字段.记录.数据类型.多工... declare @t table(id numeric(18,2)) insert into @t SELECT col1 FROM ...
- datatable 导入mysql 解决_将DataTable中的数据导入到数据库中
上次在 小小的改动即可. /// /// 将DataTable中数据写入数据库中 /// /// /// public static bool WriteDataToDB(DataTable dt) ...
- 如何将word中的数据导入到数据库中
前提:word中数据要是有规律的 如:xtext.doc Nombre:Peng Wenfei Cel:186****4515 Nombre:Wang Xulong Cel:180****1426 N ...
- sqlbulkcopy mysql_c# SqlBulkCopy实现批量从数据集中把数据导入到数据库中
今天遇到了一个导入类第一次见 SqlBulkCopy 可以实现从一个数据集导入到数据库中的表中 本来想从数据集中一条条遍历insert到库中 有了这个后发现: 只在把表与数据集的列做一下对应关系,再走 ...
- mysql数据库导入到excel表格数据_[转载]将EXCEL表格中的数据导入mysql数据库表中(两种方法)...
今天项目上遇到需要将excel中的数据导入到数据库的classify表中,于是乎拼命上网查阅方法,发现以下两种方法比较可行: 在此之前先说说按照网上的说法我没有试验成功的方法,将excel文件xls保 ...
- php中怎么连接数据库中的表,php 连接 excel表格数据库数据-php中如何将execl的数据导入到数据库中...
php中如何将execl的数据导入到数据库中 php导出大量数据的Excel: PHP从数据库分多次读取100万行记录,和分多次将100万行写入文本文件都没问题 Excel可以支持100万行记录,Ex ...
- php如何导入数据,““php中如何将execl的数据导入到数据库中
php中如何将execl的数据导入到数据库中 1.使用PHP Excel Parser Pro,但是这个软件为收费软件: 2.可EXCEL表保存为CSV,然后通过phpmyadmin或者SQLyog导 ...
- 文本的数据导入到数据库中 mysql
文本的数据导入到数据库中 mysql 示例 演练 注意 mysql 从文件中读取数据 格式: load data infile 文件路径 into table 表名 文件路径: C:/Users/Ad ...
- 把EXCEL表格导入到MYSQL中_将EXCEL表格中的数据导入mysql数据库表中(两种方法)...
今天项目上遇到需要将excel中的数据导入到数据库的classify表中,于是乎拼命上网查阅方法,发现以下两种方法比较可行: 在此之前先说说按照网上的说法我没有试验成功的方法,将excel文件xls保 ...
最新文章
- windows中的常用Dos命令
- Kotlin on Android 开发环境介绍
- Fall with Trees 二维平面直角坐标系-凸包-推公式
- 换了一个皮肤,心情也好多了
- IE6下溢出多余文字
- StyleCop学习笔记——默认的规则
- Eclipse之智能提示 actionscript javascript java开发环境智能提示
- 电路分析超详细思维导图
- SeaweedFS安全配置(Security Configuration)
- Vscode 配置默认浏览器打开
- 大健康生态应用PC管理后台、运动健康、医疗服务、商城系统、内容管理、健康数据统计、系统管理、医疗问诊、慢病管理、科室管理、问诊订单、医疗后台管理、挂号预约、体检订单、运动健康、疾病管理、血压、血糖
- 安卓系统API的使用
- 解决:java.lang.IllegalArgumentException: Can not set java.lang.Boolean field
- 短视频推荐算法过程分享,论如何针对推荐算法来优化短视频内容
- java 基本语法
- vue中弹幕实现及优化
- PHP动态网站开发技术
- 偏微分符号 ∂ 的说明
- 3318. 【BOI2013】Brunhilda的生日 (Standard IO)
- RabbitMQ超详学习
热门文章
- 设计模式(结构型模式)——享元模式(Flyweight)
- 模块化以及requirejs和seajs
- WPF 触发器Triggers
- 同一html页面中不同链接的不同样式
- [Silverlight] UI 测试/UI 自动化相关知识
- sqlite库——C实现,给sqlite数据库添加信息并把信息写入文件,删除日志和库中的日志信息
- SqlServer中获取所有数据库,所有表,所有字段
- 最小割分治(最小割树):BZOJ2229 BZOJ4519
- Spring AOP的一个简单实现
- spring(16)------spring的数据源配置