导入CSV

本文介绍了一种将CSV文件转换为DataSet和DataTable的方法。通过使用OleDbDataAdapter组件及相应的连接字符串,可以轻松实现CSV文件的数据读取,并将其转换为DataSet或DataTable格式,方便进一步的数据处理与分析。

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.OleDb;
using System.Web;
using System.IO;

namespace Common
{
    public class CSVHelper
    {
        /// <summary>
        /// Translate csv file to DataSet
        /// </summary>
        /// <param name="fileName">csv file name</param>
        /// <param name="folderName">The folder of the csv file</param>
        /// <returns>The relative DataSet of the csv file</returns>
        public static DataSet CSVToDataSet(string fileName, string folderName)
        {
            string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Text;HDR=yes;\";", folderName);

            using (DataSet dsReslut = new DataSet())
            {
                OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", fileName), strConn);
                oleDbAdapter.Fill(dsReslut);
                oleDbAdapter.Dispose();
                return dsReslut;
            }
        }

        /// <summary>
        /// Translate csv file to DataTable
        /// </summary>
        /// <param name="fileName">csv file name</param>
        /// <param name="folderName">The folder of the csv file</param>
        /// <returns>The relative DataTable of the csv file</returns>
        public static DataTable CSVToDataTable(string fileName, string folderName)
        {
            string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Text;HDR=yes;\";", folderName);

            using (DataTable dt = new DataTable())
            {
                OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", fileName), strConn);
                oleDbAdapter.Fill(dt);
                oleDbAdapter.Dispose();
                return dt;
            }
        }

        /// <summary>
        /// Translate csv file to DataSet
        /// </summary>
        /// <param name="fileName">csv file name</param>
        /// <param name="folderName">The folder of the csv file</param>
        /// <param name="count">Select how many datasets</param>
        /// <returns>The relative DataSet of the csv file</returns>
        public static DataSet CSVToDataSet(string fileName, string folderName, int count)
        {
            string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Text;HDR=yes;\";", folderName);
            using (DataSet dsReslut = new DataSet())
            {
                OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(string.Format("SELECT top {0} * FROM [{1}]", count, fileName), strConn);
                oleDbAdapter.Fill(dsReslut);
                oleDbAdapter.Dispose();
                return dsReslut;
            }
        }

        /// <summary>
        /// Translate csv file to DataTable
        /// </summary>
        /// <param name="fileName">csv file name</param>
        /// <param name="folderName">The folder of the csv file</param>
        /// <param name="count">Select how many datasets</param>
        /// <returns>The relative DataTable of the csv file</returns>
        public static DataTable CSVToDataTable(string fileName, string folderName, int count)
        {
            string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Text;HDR=yes;\";", folderName);

            using (DataTable dt = new DataTable())
            {
                OleDbDataAdapter oleDbAdapter = new OleDbDataAdapter(string.Format("SELECT top {0} * FROM [{1}]", count, fileName), strConn);
                oleDbAdapter.Fill(dt);
                oleDbAdapter.Dispose();
                return dt;
            }
        }

   }
}

转载于:https://www.cnblogs.com/Erik_Xu/articles/1867598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值