Using OleDbConnection to Read Tab-Separated File

时光毁灭记忆、已成空白 提交于 2019-12-05 08:33:43

Well, one obvious candidate is that this white space isn't actually a tab but spaces. Try FMT=Delimited( ). Use a hex viewer to see what's really there. Backgrounder is here.

And this thread shows why using a buggy chunk of code like Jet that hasn't been supported for the past 9 years is such as mistake. With the answer, leave the first line in schema.ini blank.

Uzair Tahir

Create and save an schema.ini file into the application folder containing the following text:

------------------Schema.ini file starts here-----------------
[Data.txt]
ColNameHeader=True
Format=TabDelimited
Col1=First_Name Text
Col2=Middle_Initial Text
Col3=Last_Name Text
------------------Schema.ini file ends here-----------------

Then use the following code to load the Data.txt file:

string fileName = string.Format("{0}", AppDomain.CurrentDomain.BaseDirectory);   
string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; " + "Extended Properties=\"text;HDR=YES;FMT=TabDelimited;\"", fileName);
string sql = "select * from " + "Data.txt";

OleDbConnection con = new OleDbConnection(connectionString);
con.Open();

OleDbDataAdapter dap = new OleDbDataAdapter(sql, con);
DataTable dt = new DataTable();
dt.TableName = "Data";
dap.Fill(dt);

con.Close();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!