方法一:
#region 读取Csv文件返回DataSet
/// <summary>
/// 读取Csv文件返回DataSet
/// </summary>
/// <returns>Csv内容</returns>
public DataSet Csv()
{
string FilePath = @"E:\数据抓取\入库\";
string FileName = "gouwuke_shopy.csv";
OleDbConnection OleCon = new OleDbConnection();
OleDbCommand OleCmd = new OleDbCommand();
OleDbDataAdapter OleDa = new OleDbDataAdapter();
DataSet CsvData = new DataSet();
OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Text;FMT=Delimited;HDR=YES;'";
//OleCon.ConnectionString = " 'Microsoft.Jet.OLEDB.4.0 ','Provider=SQLOLEDB', 'Extended Properties=text;HDR=Yes;FMT=Delimited ', 'Data Source=E:\\数据抓取\\入库\\";
OleCon.Open();
OleCmd.Connection = OleCon;
OleCmd.CommandText = "select * From " + FileName;
OleDa.SelectCommand = OleCmd;
try
{
OleDa.Fill(CsvData, "Csv");
return CsvData;
}
catch
{
return CsvData;
}
finally
{
OleCon.Close();
OleCmd.Dispose();
OleDa.Dispose();
OleCon.Dispose();
}
}
#endregion
方法二:
private DataSet GetDataSetFromCSV( )
{
//string fileName = “000001.csv”;string filePath = "C:\\temp\\" + "\\";
string test_filePath = @"E:\数据抓取\入库\";
string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + test_filePath + ";Extensions=asc,csv,tab,txt;";
OdbcConnection objConn = new OdbcConnection(strConn);
DataSet dsCSV = new DataSet();
try
{
string strSql = "select * from gouwuke_shopy.csv where ident <> '' and price_1 <> ''";
OdbcDataAdapter odbcCSVDataAdapter = new OdbcDataAdapter(strSql, objConn);
odbcCSVDataAdapter.Fill(dsCSV);
odbcCSVDataAdapter.Dispose();
objConn.Close();
return dsCSV;
}
catch (Exception exp)
{
throw exp;
}
}