C_读Csv文件、Execl并导入SqlServer数据库.txt

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace lyzApplication2009
{
    class ReadExcel
    {
        private string FileName;
        private string FilsPath;
        /// <summary>
        /// 构造函数文件所在路径
        /// </summary>
        /// <param name="Path">文件路径(Execl文件为完整的路径)</param>
        public ReadExcel(string Path)//构造
        {
            FileName = Path;
            //FilsPath = Files;
        }
        /// <summary>
        /// 构造函数:文件所在路径
        /// </summary>
        /// <param name="Path">文件路径(为Csv文件所在的文件夹路径)</param>
        /// <param name="Files">读Csv文件时的文件名</param>
        public ReadExcel(string Path, string Files)//构造
        {
            FileName = Path;
            FilsPath = Files;
        }

        OleDbConnection OleCon = new OleDbConnection();
        OleDbCommand OleCmd = new OleDbCommand();
        OleDbDataAdapter OleDa = new OleDbDataAdapter();

        #region 读取Execl文件返回DataSet
        /// <summary>
        /// 读取Execl文件返回DataSet
        /// </summary>
        /// <returns>返回结果集</returns>
        public DataSet Execl()
        {
            DataSet Myds = new DataSet();
            OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileName+";Extended Properties=Excel 8.0;";
            OleCon.Open();
            OleCmd.CommandText="select * from [sheet1$]";
            OleCmd.Connection = OleCon;
            OleDa.SelectCommand = OleCmd;
            try
            {
                OleDa.Fill(Myds, "Execl");
                return Myds;
            }
            catch
            {
                return Myds;
            }
            finally
            {
                OleCon.Close();
                OleCmd.Dispose();
                OleDa.Dispose();
                OleCon.Dispose();
            }

            
        }
        #endregion

        #region 读取Csv文件返回DataSet
        /// <summary>
        ///  读取Csv文件返回DataSet
        /// </summary>
        /// <returns>Csv内容</returns>
        public DataSet Csv()
        {
            DataSet CsvData = new DataSet();
            OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties='Text;FMT=Delimited;HDR=YES;'";
            OleCon.Open();
            OleCmd.Connection = OleCon;
            OleCmd.CommandText = "select * From " + FilsPath ;
            OleDa.SelectCommand=OleCmd;
            try
            {
                OleDa.Fill(CsvData, "Csv");
                return CsvData;
            }
            catch
            {
                return CsvData;
            }
            finally
            {
                OleCon.Close();
                OleCmd.Dispose();
                OleDa.Dispose();
                OleCon.Dispose();
            }
        }
        #endregion
    }
}


//写入数据库源代码;此方法是源文件内容中的例必须和要导入的表的例数一至,且数据格式一至
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace lyzApplication2009
{
    /// <summary>
    /// 将数据写入数据库表中去
    /// </summary>
    class SqlBulkData
    {
        SqlConnection SqlCon = new SqlConnection();

        #region 将数据写数据库表中去(大容量)例与数据库表例一至
        /// <summary>
        /// 将数据写数据库表中去(大容量)例与数据库表例一至
        /// </summary>
        /// <param name="_Ds">数据源内容</param>
        /// <param name="_TableName">表名</param>
        /// <returns>返回是否成功!</returns>
        public bool BulkData(DataSet _Ds,string _TableName)
        {
            SqlCon.ConnectionString = PublicClass.ConnectString;
            SqlCon.Open();
            SqlBulkCopy SqlBulk = new SqlBulkCopy(SqlCon);
            SqlBulk.DestinationTableName = _TableName;
            try
            {
                SqlBulk.WriteToServer(_Ds.Tables[0], DataRowState.Unchanged);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                SqlCon.Close();
                SqlCon.Dispose();
                SqlBulk.Close();
            }
        }
        #endregion
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值