public DataSet ConnectCSV(string filetable,string CSVFilePath)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or connection string
// Create a connection string as below, if you want to use DSN less connection. The DBQ attribute sets the path of directory which contains CSV files
string strConnString="Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+CSVFilePath+";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;
//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim());
//Open the connection
conn.Open();
//Fetch records from CSV
try
{
sql_select = "SELECT * from ["+ filetable +"]";
obj_oledb_da = new System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
obj_oledb_da.Fill(ds,"Tag");
dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Tag";
}
catch(Exception ex)
{
MessageBox.Show("待导入的CSV文件格式不正确","警告");
}
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message,"警告");
}
return ds;
}
参照:
http://blog.sina.com.cn/s/blog_540a6a3e01000756.html
努力+方法=成功