码迷,mamicode.com
首页 > 数据库 > 详细

C# ASP.NET CSV文件导入数据库

时间:2014-08-25 19:01:04      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   文件   for   ar   数据   

原文:C# ASP.NET CSV文件导入数据库

 

 

 

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

}

}

 

 

 

在D:\VSS2005\政务\HPRSP_黄埔海关风险管理平台\源代码\CommonDataObject\下增加了CSV文件导入数据库的操作类:CSVHelper.cs .用法很简单,只要传入CSV文件的文件路径和文件名就行。下面是一个例子:

 

 

string conn = ConfigurationManager.ConnectionStrings["HPRSP"].ToString();

//获取并保存上传的文件

if (fuUpload.PostedFile != null)

fuUpload.PostedFile.SaveAs(Server.MapPath("") + "\\" + fuUpload.FileName);

else

throw new Exception("请选择需上传的文件!");

//在构造函数中传入文件路径和文件名

HPRSP.CommonDataObject.CSVHelper CsvFile = new HPRSP.CommonDataObject.CSVHelper(Server.MapPath("") + "\\", fuUpload.FileName);

//调用Read()方法读取文件,具体的实现过程请查看类:CSVHelper.cs

DataTable dt= CsvFile.Read();

foreach(DataRow dr in dt.Rows)

{

string StoreName = "uspZFPG_INDEX_UPDATE";

 

ParamCollection parmsInsert = new ParamCollection();

//下面[]内为列名,即第一行对应的列名称,如下图的测试数据

//bubuko.com,布布扣

parmsInsert.Add(new Param("CustomsCode", dr["关区"].ToString(), ParamType.Varchar, 4, ParameterDirection.Input));

parmsInsert.Add(new Param("IndexID", dr["指标ID"].ToString(), ParamType.Varchar, 10, ParameterDirection.Input));

parmsInsert.Add(new Param("StandardValue", dr["目标值"].ToString(), ParamType.Varchar, 50, ParameterDirection.Input));

parmsInsert.Add(new Param("Value", dr["完成值"].ToString(), ParamType.Varchar, 50, ParameterDirection.Input));

parmsInsert.Add(new Param("IsReachStandard", dr["是否达标"], ParamType.Nvarchar, 50, ParameterDirection.Input));

 

try

{

SqlDatabase.ExecuteNonQuery(conn, CommandType.StoredProcedure, StoreName, 300, parmsInsert);

}

catch (SqlException ex)

{

Errors++;

//string Desc = "导入“执法评估指标 " + dr["IndexID"].ToString() + ",关区 " + dr["CustomsCode"].ToString() + ",时间段标志 " + dr["TimeSpan"].ToString() + "”时出错:" + ex.ToString() + DateTime.Now;

throw ex;

}

}

C# ASP.NET CSV文件导入数据库

标签:des   blog   http   os   io   文件   for   ar   数据   

原文地址:http://www.cnblogs.com/lonelyxmas/p/3935380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!