OleDb 和ADO.NET讀取文本文件如何像一般的SQL查詢那樣進行條件查詢呢?文本文件沒有所謂的字段名稱,該怎麼處理呢?其實,在使用ADO.NET進行讀 取數據記錄的時候,在程序內部應該是有字段名稱來標識這些列的(具體的定義沒有找到官方的文檔,本文是採用實驗的方法得到的結論),那麼,怎麼知道列名是 什麼呢?下面的方法就可以很清楚地看到:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" >
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString;
string SQLString;
ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " +
Server.MapPath( " ~ " ) + " ;Extended Properties=/"Text;HDR=No;FMT=Delimited/" " ;
SQLString = " Select * from a.txt " ;
System.Data.OleDb.OleDbConnection ConnectionText = new System.Data.OleDb.OleDbConnection();
ConnectionText.ConnectionString = ConnectionString;
ConnectionText.Open();
System.Data.OleDb.OleDbDataAdapter AdapterText = new System.Data.OleDb.OleDbDataAdapter(SQLString, ConnectionText);
System.Data.DataSet DataSetText = new System.Data.DataSet();
AdapterText.Fill(DataSetText, " TextFile " );
GridView1.DataSource = DataSetText;
GridView1.DataBind();
ConnectionText.Dispose();
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟憲會讀取文本文件測試 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:GridView ID ="GridView1" runat ="server" ShowHeader ="True" >
</ asp:GridView >
</ form >
</ body >
</ html >
可以看到如下的結果:
F1 | F2 | F3 | F4 | F5 |
---|---|---|---|---|
2010/7/27 8:58:35 | 0.005 | 8 | mxh | http://dotnet.aspx.cc |
2010/7/27 8:58:36 | 0.005 | 2 | mxh | http://dotnet.aspx.cc |
2010/7/27 8:58:37 | 0.005 | 6 | net_lover | http://dotnet.aspx.cc |
2010/7/27 8:58:38 | 0.005 | 0 | net_lover | http://dotnet.aspx.cc |
2010/7/27 8:58:39 | 0.004 | 0 | net_lover | http://dotnet.51aspx.com |
2010/7/27 8:58:40 | 0.005 | 3 | net_lover | http://dotnet.aspx.cc |
2010/7/27 8:58:41 | 0.005 | 0 | net_lover | http://dotnet.aspx.cc |
2010/7/27 8:58:42 | 0.005 | 9 | net_lover | http://dotnet.aspx.cc |
2010/7/27 8:58:43 | 0.005 | 0 | 孟憲會 | http://dotnet.51aspx.com |
2010/7/27 8:58:44 | 0.005 | 0 | 孟憲會 | http://dotnet.aspx.cc |
好了,知道了列名,那麼按條件查詢應該是可以的,下面就進行下測試:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script runat ="server" >
protected void Page_Load(object sender, EventArgs e)
{
string ConnectionString;
string SQLString;
ConnectionString = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " +
Server.MapPath( " ~ " ) + " ;Extended Properties=/"Text;HDR=No;FMT=Delimited/" " ;
SQLString = " Select * from a.txt Where F1=#2010/7/27 8:58:36# " ;
SQLString = " Select * from a.txt Where F2=0.006 " ;
SQLString = " Select * from a.txt Where F4='孟憲會' And F5='http://dotnet.aspx.cc' " ;
SQLString = " Select * from a.txt Where F4='孟憲會' " ;
System.Data.OleDb.OleDbConnection ConnectionText = new System.Data.OleDb.OleDbConnection();
ConnectionText.ConnectionString = ConnectionString;
ConnectionText.Open();
System.Data.OleDb.OleDbDataAdapter AdapterText = new System.Data.OleDb.OleDbDataAdapter(SQLString, ConnectionText);
System.Data.DataSet DataSetText = new System.Data.DataSet();
AdapterText.Fill(DataSetText, " TextFile " );
GridView1.DataSource = DataSetText;
GridView1.DataBind();
ConnectionText.Dispose();
}
</ script >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 孟憲會讀取文本文件測試 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:GridView ID ="GridView1" runat ="server" ShowHeader ="False" >
</ asp:GridView >
</ form >
</ body >
</ html >
測試用的a.txt文件內容如下:
2010-7-27 8:58:35,0.005,8,mxh,http://dotnet.aspx.cc
2010-7-27 8:58:36,0.005,2,mxh,http://dotnet.aspx.cc
2010-7-27 8:58:37,0.005,6,net_lover,http://dotnet.aspx.cc
2010-7-27 8:58:38,0.005,0,net_lover,http://dotnet.aspx.cc
2010-7-27 8:58:39,0.004,0,net_lover,http://dotnet.51aspx.com
2010-7-27 8:58:40,0.005,3,net_lover,http://dotnet.aspx.cc
2010-7-27 8:58:41,0.005,0,net_lover,http://dotnet.aspx.cc
2010-7-27 8:58:42,0.006,9,net_lover,http://dotnet.aspx.cc
2010-7-27 8:58:43,0.005,0,孟憲會,http://dotnet.51aspx.com
2010-7-27 8:58:44,0.005,0,孟憲會,http://dotnet.aspx.cc
注意:日期格式可能根據系統的設置有所不同,這點需要注意。