2
我試圖讀取文本文件中的數據並將其加載到數據集中,但下面圖像中的不同列僅作爲一個長列出現。我想將數據作爲7列返回(與其在下圖中顯示的方式相同)。如何從文本文件中正確讀取列
這是代碼正在使用,
public DataSet LoadTxtFile(int numberOfRows)
{
DataSet ds = new DataSet();
//try
//{
// Creates and opens an ODBC connection
string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";
string sql_select;
OdbcConnection conn;
conn = new OdbcConnection(strConnString.Trim());
conn.Open();
//Creates the select command text
if (numberOfRows == -1)
{
sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
}
else
{
sql_select = "select top " + numberOfRows + " * from [" + this.FileNevCSV.Trim() + "]";
}
//Creates the data adapter
OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);
//Fills dataset with the records from CSV file
obj_oledb_da.Fill(ds, "csv");
//closes the connection
conn.Close();
//}
//catch (Exception e) //Error
//{
//MessageBox.Show(e.Message, "Error - LoadCSV",MessageBoxButtons.OK,MessageBoxIcon.Error);
//}
return ds;
}
什麼語言,看起來像Java/C#,但您應該適當地標記它。 – 2012-07-16 07:36:34
你的名字指出它是C# – 2012-07-16 07:37:17