上篇文章将Excel中数据导入数据库时,将从Excel读入的数据均转换成了数据库相应字段的类型,其实这是没有必要的,因为对于数据库各种类型的插入,均可以字符串格式插入。比如表WQ_SWMSAR_A字段定义如下:

  

  其中RESULTID为自增长字段。

  可利用以下语句插入:

insert into [WaterQuality2].[dbo].[WQ_SWMSAR_A]
(STCD,ASOT,ASDR,WQG,MESPEST) values('','2013-07-25','','','chenyuming')

  插入结果如下:

  

  所以对于DataTable数据导入到数据库的通用函数可以写成如下形式:

        // 从System.Data.DataTable导入数据到数据库(by chenyuming)
public int InsetData(System.Data.DataTable dt, string tableName)
{
int i = ; for (int rowIndex = ; rowIndex < dt.Rows.Count; rowIndex++)
{
string strSql = "Insert into " +tableName +"(";
string strValue = "";
for (int colIndex = ; colIndex < dt.Columns.Count; colIndex++)
{
if(colIndex == )
{
strSql += dt.Columns[colIndex].ColumnName;
strValue += dt.Rows[rowIndex][colIndex];
}
else
{
strSql += "," + dt.Columns[colIndex].ColumnName;
strValue += "," + dt.Rows[rowIndex][colIndex];
}
}
strSql += ") values ('";
strSql += strValue;
strSql += ")"; String strConnection = ConfigurationSettings.AppSettings["ConnectionStr"].ToString();
SqlConnection sqlConnection = new SqlConnection(strConnection);
try
{
sqlConnection.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = strSql;
sqlCmd.Connection = sqlConnection;
SqlDataReader sqlDataReader = sqlCmd.ExecuteReader();
i++;
sqlDataReader.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConnection.Close();
}
}
return i;
}

  当然这里有个性能劣点:每插入一行数据均进行了打开与关闭数据库。

  相关文章:

  (1)将Excel中数据导入数据库(一)

  (2)将Excel中数据导入数据库(二)

  (3)将Excel中数据导入数据库(三)

将Excel中数据导入数据库(三)的更多相关文章

  1. 将Excel中数据导入数据库(二)

    在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ...

  2. 将Excel中数据导入数据库(一)

    在工作中经常要将Excel中数据导入数据库,这里介绍一种方法. 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: Excel中数据导入数据库帮助类如下: using System; ...

  3. SpringBoot通过Ajax批量将excel中数据导入数据库

    Spring Boot通过Ajax上传Excel并将数据批量读取到数据库中 适合场景:需要通过excel表格批量向数据库中导入信息 操作流程 [1]前端上传一个excel表格 [2] 后端接收这个ex ...

  4. Excel表数据导入数据库表中

    ***Excel表数据导入到数据库表中 通过数据库表的模板做成‘Excel’表的数据导入到数据库相应的表中(注意:主表 和 从表的关系,要先导‘主表’在导入从表) 过程:通过数据库的导入工具—先导入为 ...

  5. mysqlconnector将EXCEL表数据导入数据库

    测试excel和脚本放在同一个目录 测试excel和脚本放在同一个目录 #!/usr/bin/env python #coding=utf-8 import xlrd import mysql.con ...

  6. excel表格数据导入数据库Oracle

    方法一: 1.创建数据表 CREATE TABLE T_USER (   ID             VARCHAR2(32) primary key,   NAME           VARCH ...

  7. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  8. larave5.6 将Excel文件数据导入数据库代码实例

    <?php namespace App\Admin\Controllers; use App\AdminUser; use Illuminate\Http\Request; use Excel; ...

  9. java后端服务器读取excel将数据导入数据库

    使用的是easypoi,官网文档:http://easypoi.mydoc.io/ /** * 导入Excel文件 */ @PostMapping("/importTeacher" ...

随机推荐

  1. sublime_text3 用户配置

    { "auto_complete_triggers": [ { "characters": "", "selector" ...

  2. UIActivityIndicatorView添加到UIButton上并响应事件

    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewSty ...

  3. SQL语句技巧_索引的优化_慢查询日志开启_root密码的破解

    1.正则表达式的使用 regexp例:select name,email from t where email regexp '@163[.,]com$'使用like方式查询selct name,em ...

  4. 黄聪:走进wordpress 详细说说template-loader.php

    再看template-laoder.php,这个文件总共只有45行.它的作用是基于访问的URL装载正确的模板. 文件第六行,也是第一条语句,如下: if ( defined('WP_USE_THEME ...

  5. 黄聪:wordpress自定义post_type,并且自定义固定链接

    <? class zsjh { function init() { add_action( 'init', array($this,'create_zsjh') ); add_filter('p ...

  6. Chrome开发者工具详解(2)

    Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板.Timeline面板.Profiles面板.Application面板.Sec ...

  7. Redis简介及3.0.2编译安装

    由于项目需要Redis作为内存数据库,所以也开始搞Redis~ Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).l ...

  8. BestCoder Round #85 hdu5778 abs(素数筛+暴力)

    abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...

  9. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  10. CF 500 C. New Year Book Reading 贪心 简单题

    New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n b ...