asp读取txt文本内容并导入ACCESS的代码
我的毕业设计里面碰见这样的一个问题,需要用asp读取txt文件里面的内容,然后写入到access数据库里面的一个表里面!我的问题是1.txt的格式是怎么样的,(用什么隔开...
我的毕业设计里面碰见这样的一个问题,需要用asp读取txt文件里面的内容,然后写入到access数据库里面的一个表里面!
我的问题是
1.txt的格式是怎么样的,(用什么隔开txt文件里面的数据)?
2.具体的代码,(附上你的txt文件) 展开
我的问题是
1.txt的格式是怎么样的,(用什么隔开txt文件里面的数据)?
2.具体的代码,(附上你的txt文件) 展开
展开全部
读取文本内容:
Function ReadAllTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadAllTextFile = f.ReadAll
End Function
导入数据库:
FileStream fs = new FileStream ( "C:\\file.txt" , FileMode.Open , FileAccess.Read ) ;
StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//关闭此StreamReader对象
m_streamReader.Close ( ) ;
其实你可以把文件一行一行读出来,每行用一个分隔符,取出来的时候,再把他转化回来。
或者
//一、利用文本
<Script language="vbscript" runat="Server">
//确定与文件相关的用于储存数据的数据文件名 eg: 为myfile.asp网页中传回的数据建立的myfile.txt文件
function filename()
dim fullpath,pointposition
fullpath=request.ServerVariables("PATH_TRANSLATED")
pointposition=instrrev(fullpath,".")
filename=mid(fullpath,1,pointposition)+"txt"
end function
//将数据存储到myfile.txt中
sub webcounter(strfile)
cntname=filename()
set numobject=CreateObject("Scripting.FileSystemObject")
//set numfile=numobject.OpenTextFile(cntname,1,0,0) //只读出原文件
set numfile=numobject.OpenTextFile(cntname,8,0,0) //读出原文件,并在原文件基础上继续添加文本
//set numfile=numobject.CreateTextFile(cntname,-1,0) //用新的文本覆盖原文本
numfile.WriteLine strfile
end sub
</Script>
Function ReadAllTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadAllTextFile = f.ReadAll
End Function
导入数据库:
FileStream fs = new FileStream ( "C:\\file.txt" , FileMode.Open , FileAccess.Read ) ;
StreamReader m_streamReader = new StreamReader ( fs ) ;
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek ( 0 , SeekOrigin.Begin ) ;
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
this.richTextBox1.Text = "" ;
string strLine = m_streamReader.ReadLine ( ) ;
while ( strLine != null )
{
this.richTextBox1.Text += strLine + "\n" ;
strLine = m_streamReader.ReadLine ( ) ;
}
//关闭此StreamReader对象
m_streamReader.Close ( ) ;
其实你可以把文件一行一行读出来,每行用一个分隔符,取出来的时候,再把他转化回来。
或者
//一、利用文本
<Script language="vbscript" runat="Server">
//确定与文件相关的用于储存数据的数据文件名 eg: 为myfile.asp网页中传回的数据建立的myfile.txt文件
function filename()
dim fullpath,pointposition
fullpath=request.ServerVariables("PATH_TRANSLATED")
pointposition=instrrev(fullpath,".")
filename=mid(fullpath,1,pointposition)+"txt"
end function
//将数据存储到myfile.txt中
sub webcounter(strfile)
cntname=filename()
set numobject=CreateObject("Scripting.FileSystemObject")
//set numfile=numobject.OpenTextFile(cntname,1,0,0) //只读出原文件
set numfile=numobject.OpenTextFile(cntname,8,0,0) //读出原文件,并在原文件基础上继续添加文本
//set numfile=numobject.CreateTextFile(cntname,-1,0) //用新的文本覆盖原文本
numfile.WriteLine strfile
end sub
</Script>
展开全部
文本文件:p.txt格式
张三,男,50岁
李四,女,30岁
<%
filespec=server.mappath("\test\p.txt")
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
i=0
Do While theFile.AtEndOfStream <> True
session("行"&i) = theFile.ReadLine
i=i+1
Loop
theFile.Close
ReadEntireFile = retstring
for j=0 to i-1
arry=split(session("行"&j),",")
'arry(0),arry(1),arry(2)
'分别为:张三 男 30岁
'然后进行数据存储
'rs.add rs.update......这个就不写了
response.write session("行"&j)+"<br>"
next
%>
张三,男,50岁
李四,女,30岁
<%
filespec=server.mappath("\test\p.txt")
Const ForReading = 1
Dim fso, theFile, retstring
Set fso = CreateObject("Scripting.FileSystemObject")
Set theFile = fso.OpenTextFile(filespec, ForReading, False)
i=0
Do While theFile.AtEndOfStream <> True
session("行"&i) = theFile.ReadLine
i=i+1
Loop
theFile.Close
ReadEntireFile = retstring
for j=0 to i-1
arry=split(session("行"&j),",")
'arry(0),arry(1),arry(2)
'分别为:张三 男 30岁
'然后进行数据存储
'rs.add rs.update......这个就不写了
response.write session("行"&j)+"<br>"
next
%>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
做过一个php的,比asp的要简洁很多
<?
$conn=mssql_connect("localhost","me","123");
mssql_select_db("phonesystem") or die("数据库连接失败");
?>
<?
$lines = file('183000.clf');
foreach ($lines as $line_num => $line) {
list($kahao,$mima,$rubbish,$chanpin,$mianzhi,$riqi)=explode(",",$line);
$gsql="select * from [6data] where daCode1='".$kahao."'";
$nums=mssql_query($gsql);
if (mssql_num_rows($nums)=="") {
echo "当前正在操作第".$line_num."行.";
$isql="insert into [6data](daOperId,daPrid,daDeno,daCode1,daCode2,daIStorDate,daState,daExDate) values('".中国移动."','".$chanpin."','".$mianzhi."','".$mima."','".$kahao."','".$riqi."','1','')";
mssql_query($isql) or die("插入失败");
} else {
break;
}
}
?>
文本内容,是用","做分隔符
062551500884016933,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016934,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016935,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016936,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016937,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016938,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016939,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016940,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016941,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016942,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016943,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016944,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016945,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
<?
$conn=mssql_connect("localhost","me","123");
mssql_select_db("phonesystem") or die("数据库连接失败");
?>
<?
$lines = file('183000.clf');
foreach ($lines as $line_num => $line) {
list($kahao,$mima,$rubbish,$chanpin,$mianzhi,$riqi)=explode(",",$line);
$gsql="select * from [6data] where daCode1='".$kahao."'";
$nums=mssql_query($gsql);
if (mssql_num_rows($nums)=="") {
echo "当前正在操作第".$line_num."行.";
$isql="insert into [6data](daOperId,daPrid,daDeno,daCode1,daCode2,daIStorDate,daState,daExDate) values('".中国移动."','".$chanpin."','".$mianzhi."','".$mima."','".$kahao."','".$riqi."','1','')";
mssql_query($isql) or die("插入失败");
} else {
break;
}
}
?>
文本内容,是用","做分隔符
062551500884016933,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016934,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016935,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016936,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016937,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016938,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016939,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016940,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016941,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016942,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016943,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016944,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
062551500884016945,joe{}tjdcsxgjnywzn, ,0810030,30,20070930
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还是牛人多啊,呵呵。,我才写一半,他们都写好了……
看看我写的适合你不:(测试过的,好用的,记得测试的时候加 上包含文件conn.asp)
<%
Dim write
Dim fileSysObj, tf, read
read = "read.txt"
read = LEFT(Server.Mappath(Request.ServerVariables("PATH_INFO")), InStrRev(Server.Mappath(Request.ServerVariables("PATH_INFO")), "\")) & read
Set fileSysObj = createObject("Scripting.FileSystemObject")
if (fileSysObj.FileExists(read)) Then
Set tf = filesysobj.OpenTextFile(read, 1)
read = tf.ReadLine
tf.Close
'下面将数据写入数据库
set rs=server.createobject("adodb.recordset")
sql="select * from mdb" '数据库表名MDB
rs.open sql,conn,1,3
rs.addnew
rs("wenben")=read'字段名称为wenben
read="写入数据库成功。"
rs.close
else
read = "没有找到Rade.txt文件,写入数据库失败……"
end if
%>
'在下面表格中显示read.txt
<div align="center">
<center>
<table border="0" width="40%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#EEEECC"><%=read%>
</td>
</tr>
<tr>
<td width="100%">
看看我写的适合你不:(测试过的,好用的,记得测试的时候加 上包含文件conn.asp)
<%
Dim write
Dim fileSysObj, tf, read
read = "read.txt"
read = LEFT(Server.Mappath(Request.ServerVariables("PATH_INFO")), InStrRev(Server.Mappath(Request.ServerVariables("PATH_INFO")), "\")) & read
Set fileSysObj = createObject("Scripting.FileSystemObject")
if (fileSysObj.FileExists(read)) Then
Set tf = filesysobj.OpenTextFile(read, 1)
read = tf.ReadLine
tf.Close
'下面将数据写入数据库
set rs=server.createobject("adodb.recordset")
sql="select * from mdb" '数据库表名MDB
rs.open sql,conn,1,3
rs.addnew
rs("wenben")=read'字段名称为wenben
read="写入数据库成功。"
rs.close
else
read = "没有找到Rade.txt文件,写入数据库失败……"
end if
%>
'在下面表格中显示read.txt
<div align="center">
<center>
<table border="0" width="40%" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" bgcolor="#EEEECC"><%=read%>
</td>
</tr>
<tr>
<td width="100%">
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询