- public class CsvDAO
- {
- public static OleDbConnection OleCon = new OleDbConnection();
- public static OleDbCommand OleCmd = new OleDbCommand();
- public static OleDbDataAdapter OleDa = new OleDbDataAdapter();
- #region 读取Csv文件返回DataSet
- /// <summary>
- /// 读取Csv文件返回DataSet
- /// </summary>
- /// <param name="sql">查询语句 </param>
- /// <param name="FilePath">文件路径 </param>
- /// <returns>Csv内容</returns>
- public static DataSet CsvReader(string sql,string FilePath)
- {
- DataSet CsvData = new DataSet();
- OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Text;FMT=Delimited;HDR=YES;'";
- OleCon.Open();
- OleCmd.Connection = OleCon;
- OleCmd.CommandText = sql;
- OleDa.SelectCommand=OleCmd;
- try
- {
- OleDa.Fill(CsvData,"Csv");
- return CsvData;
- }
- catch
- {
- return CsvData;
- }
- finally
- {
- OleCon.Close();
- OleCmd.Dispose();
- OleDa.Dispose();
- OleCon.Dispose();
- }
- }
- #endregion
- }
c#读取csv文件
最新推荐文章于 2024-07-19 09:30:00 发布