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>
可以看到如下的结果:
| 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 |
好了,知道了列名,那么按条件查询应该是可以的,下面就进行下测试:
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
注意:日期格式可能根据系统的设置有所不同,这点需要注意。 |