读取txt文本到DataTable中

System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
String folder
= " K:\\share\\ " ;
String sql
= " select * from test.txt " ;
cn.ConnectionString
= " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + folder + " ;Extended Properties=\"Text;HDR=No;FMT=Delimited\" " ;
System.Data.OleDb.OleDbDataAdapter ad
= new System.Data.OleDb.OleDbDataAdapter(sql, cn);
System.Data.DataTable dt
= new DataTable();
ad.Fill(dt);
String result
= "" ;
for ( int i = 0 ; i < dt.Rows.Count; i ++ )
{
     
 
for ( int m = 0 ; m < dt.Columns.Count; m ++ )
  {
    result
+= dt.Rows[i][m].ToString() + " -- " ;
  }
  result
+= Environment.NewLine;
}
MessageBox.Show(result);




你可能感兴趣的:(Datatable)