惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Securelist
博客园 - Franky
P
Proofpoint News Feed
量子位
雷峰网
雷峰网
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Latest news
Latest news
L
Lohrmann on Cybersecurity
W
WeLiveSecurity
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
宝玉的分享
宝玉的分享
GbyAI
GbyAI
小众软件
小众软件
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
The Cloudflare Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
Scott Helme
Scott Helme
O
OpenAI News
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
K
Kaspersky official blog
Microsoft Azure Blog
Microsoft Azure Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
腾讯CDC
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tailwind CSS Blog

博客园 - 小光_520

这个博客18年了.. C#基础概念之延迟加载(转) 把EXCEL文件导入到GridView,GridView根据要求动态的增加列(转) GridView 使用方法总结二(转) GridView 使用方法总结一(转) 单条SQL语句实现复杂逻辑几例(转) Statspack之十四-"log file sync" 等待事件(转贴) Statspack之十三-Enqueue(转贴) Statspack之十二-db file scattered read-DB文件分散读取(转帖) Statspack之十一-Statspack报告各部分简要说明(转帖) Statspack之十-调整STATSPACK的收集门限(转帖) Statspack之九-其它重要脚本(转帖) Statspack之八-删除历史数据(转帖) Statspack之七-移除定时任务(转贴) Statspack之六-生成分析报告(转贴) Statspack之五-规划自动任务(转贴) Statspack之四-测试安装好的Statspack(转贴) Statspack之三-安装statspack(转贴) Statspack之二-需要更改的系统参数(转贴)
C# ASP.NET CSV文件导入数据库(转)
小光_520 · 2012-11-20 · via 博客园 - 小光_520

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.OleDb;

using System.Data.Sql;

using System.Data.SqlClient;

using System.IO;

namespace HPRSP.CommonDataObject

{

public class CSVHelper

{

private string path;

private string fileName;

/// <summary>

/// z构造函数

/// </summary>

public CSVHelper(string filePath, string fileName)

{

this.path = filePath;

this.fileName = fileName;

}

/// <summary>

/// 从Csv中读取数据

/// </summary>

/// <returns></returns>

public DataTable Read()

{

return Read(null);

}

/// <summary>

/// 通过文件流的方式来读取CSV文件,默认第一列为标题列,列之间用逗号分隔

/// </summary>

/// <param name="files"></param>

/// <returns></returns>

public DataTable ReadCsvFileToTable()

{

return ReadCsvFileToTable(true, ',');

}

/// <summary>

/// 通过文件流的方式来读取CSV文件,默认列之间用逗号分隔

/// </summary>

/// <param name="files">文件名称</param>

/// <param name="HeadYes">第一行是否为列标题</param>

/// <returns></returns>

public DataTable ReadCsvFileToTable(bool HeadYes)

{

return ReadCsvFileToTable(HeadYes, ',');

}

/// <summary>

/// 通过文件流的方式来读取CSV文件

/// </summary>

/// <param name="files">文件名称</param>

/// <param name="HeadYes">第一行是否为列标题</param>

/// <param name="span">分隔符</param>

/// <returns></returns>

public DataTable ReadCsvFileToTable(bool HeadYes, char span)

{

//文件路径和文件名

string files = path + fileName;

DataTable dt = new DataTable();

StreamReader fileReader = new StreamReader(files, Encoding.Default);

try

{

//是否为第一行(如果HeadYes为TRUE,则第一行为标题行)

int lsi = 0;

//列之间的分隔符

char cv = span;

while (fileReader.EndOfStream == false)

{

string line = fileReader.ReadLine();

string[] y = line.Split(cv);

//第一行为标题行

if (HeadYes == true)

{

//第一行

if (lsi == 0)

{

for (int i = 0; i < y.Length; i++)

{

dt.Columns.Add(y[i].Trim().ToString());

}

lsi++;

}

//从第二列开始为数据列

else

{

DataRow dr = dt.NewRow();

for (int i = 0; i < y.Length; i++)

{

dr[i] = y[i].Trim();

}

dt.Rows.Add(dr);

}

}

//第一行不为标题行

else

{

if (lsi == 0)

{

for (int i = 0; i < y.Length; i++)

{

dt.Columns.Add("Col" + i.ToString());

}

lsi++;

}

DataRow dr = dt.NewRow();

for (int i = 0; i < y.Length; i++)

{

dr[i] = y[i].Trim();

}

dt.Rows.Add(dr);

}

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

fileReader.Close();

fileReader.Dispose();

}

return dt;

}

/// <summary>

/// 从csv中读取数据

/// </summary>

/// <param name="colNames">列名列表,可为空</param>

/// <returns></returns>

private DataTable Read(string[] colNames)

{

string sql = CreateSql(colNames);

return ExecuteTable(sql);

}

/// <summary>

/// 通过执行sql语句,从Csv中读取数据

/// </summary>

/// <param name="sql">sql语句</param>

/// <returns></returns>

private DataTable ExecuteTable(string sql)

{

DataTable table = new DataTable();

using (OleDbConnection connection = new OleDbConnection(GetConnString(true)))

{

OleDbCommand command = connection.CreateCommand();

command.CommandText = sql;

OleDbDataAdapter adapter = new OleDbDataAdapter(command);

adapter.Fill(table);

}

return table;

}

#region 私有方法

private string CreateSql(string[] colNames)

{

string cols = "*";

if (null != colNames && colNames.Length > 0)

{

StringBuilder colList = new StringBuilder();

for (int i = 0; i < colNames.Length - 1; i++)

{

colList.AppendFormat("[{0}],", colNames[i]);

}

colList.AppendFormat("[{0}]", colList[colList.Length]);

cols = colList.ToString();

}

return string.Format("select {0} from {1}", cols, fileName);

}

private string GetConnString(bool IsHeaderRow)

{

return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Text;FMT=Delimited; HDR={0}';data source={1}", IsHeaderRow ? "Yes" : "No", path);

}

#endregion

}

}

转自http://www.cnblogs.com/starxp/articles/2387804.html