OleDb和ADO.NET读取文本文件时进行条件查询

OleDb 和ADO.NET读取文本文件如何像一般的SQL查询那样进行条件查询呢?文本文件没有所谓的字段名称,该怎么处理呢?其实,在使用ADO.NET进行读 取数据记录的时候,在程序内部应该是有字段名称来标识这些列的(具体的定义没有找到官方的文档,本文是采用实验的方法得到的结论),那么,怎么知道列名是 什么呢?下面的方法就可以很清楚地看到:

ASPX 代码
<% @ Page Language = " C# " EnableViewState = " false " %>

<! 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 >

可以看到如下的结果:

F1F2F3F4F5
2010/7/27 8:58:350.0058mxhhttp://dotnet.aspx.cc
2010/7/27 8:58:360.0052mxhhttp://dotnet.aspx.cc
2010/7/27 8:58:370.0056net_loverhttp://dotnet.aspx.cc
2010/7/27 8:58:380.0050net_loverhttp://dotnet.aspx.cc
2010/7/27 8:58:390.0040net_loverhttp://dotnet.51aspx.com
2010/7/27 8:58:400.0053net_loverhttp://dotnet.aspx.cc
2010/7/27 8:58:410.0050net_loverhttp://dotnet.aspx.cc
2010/7/27 8:58:420.0059net_loverhttp://dotnet.aspx.cc
2010/7/27 8:58:430.0050孟宪会http://dotnet.51aspx.com
2010/7/27 8:58:440.0050孟宪会http://dotnet.aspx.cc

好了,知道了列名,那么按条件查询应该是可以的,下面就进行下测试:

ASPX 代码
<% @ Page Language = " C# " EnableViewState = " false " %>

<! 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

注意:日期格式可能根据系统的设置有所不同,这点需要注意。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值