文本文件中有内容:
1345asf aaaa 1111
as12fd2 bbbb 2222
asdf123 cccc 3333
oracle数据库中有相应的列:
id varchar(20) PK
name varchar(20)
pwd varchar(20)
现在要将文本文件的内容按要求在网页中导入数据库内,很急,希望高手帮忙~!要能用的例子!第一个能用的给全部分数!!
7 个解决方案
#1
抱歉,没有现成的例子,给个思路:
读取txt文件到Stream(.net有个方法),逐行添加到一个List
然后遍历List,写入到Oracle
读取txt文件到Stream(.net有个方法),逐行添加到一个List
然后遍历List,写入到Oracle
#2
//读取文件
public StringBuilder GetFileCode()
{
StringBuilder text = new StringBuilder();
try
{
string FilePageDir = ConfigurationManager.AppSettings["txt"].ToString();
using (StreamReader sr = new StreamReader(FilePageDir, Encoding.Default))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return text;
}
/
#3
System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt");
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//存数据库
}
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//存数据库
}
#4
又一个懒人!思路能给,代码还是要自己写!
1、读取文件;
2、分行;每行一条数据;根据回车或者其他判断;
3、分字段;
4、存入数据库;
上面的做法效率不高;
或者干脆使文本文件符合导出文件的格式,直接导入,这样快多了。
#5
增加一些东东
System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt");
//伪代码 get Connection
// DBTrans tran = Connection.BeginTran();
try{
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//getDBCommand
//存数据库 insert into table () values(strArray[0],strArray[1],strArray[2] )
//DBCommand.Excute(tran);
}
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
}
finally
{
Connection.Close();
sr.Close();
}
#6
同意楼上的说法
using System.Collections;
using System.IO;
...
{
FileInfo file = new FileInfo("1.text");
StreamReader strmR= file.OpenText();
//ArrayList txtArry = new ArrayList();
while (!strmR.EndOfStream)
{
//txtArry.Add(strmR.ReadLine());
string line = strmR.ReadLine();
if(line.Trim()!="")
{
string[] parms= line.Split(new char[] { ' ' });
InsertOracle(parms[0], parms[1], parms[3]);
}
}
strmR.Close();
}
...
private void InsertOracle(string p, string p_2, string p_3)
{
//数据存入数据库
...
}
using System.Collections;
using System.IO;
...
{
FileInfo file = new FileInfo("1.text");
StreamReader strmR= file.OpenText();
//ArrayList txtArry = new ArrayList();
while (!strmR.EndOfStream)
{
//txtArry.Add(strmR.ReadLine());
string line = strmR.ReadLine();
if(line.Trim()!="")
{
string[] parms= line.Split(new char[] { ' ' });
InsertOracle(parms[0], parms[1], parms[3]);
}
}
strmR.Close();
}
...
private void InsertOracle(string p, string p_2, string p_3)
{
//数据存入数据库
...
}
#7
顶
#1
抱歉,没有现成的例子,给个思路:
读取txt文件到Stream(.net有个方法),逐行添加到一个List
然后遍历List,写入到Oracle
读取txt文件到Stream(.net有个方法),逐行添加到一个List
然后遍历List,写入到Oracle
#2
//读取文件
public StringBuilder GetFileCode()
{
StringBuilder text = new StringBuilder();
try
{
string FilePageDir = ConfigurationManager.AppSettings["txt"].ToString();
using (StreamReader sr = new StreamReader(FilePageDir, Encoding.Default))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return text;
}
/
#3
System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt");
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//存数据库
}
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//存数据库
}
#4
又一个懒人!思路能给,代码还是要自己写!
1、读取文件;
2、分行;每行一条数据;根据回车或者其他判断;
3、分字段;
4、存入数据库;
上面的做法效率不高;
或者干脆使文本文件符合导出文件的格式,直接导入,这样快多了。
#5
增加一些东东
System.IO.StreamReader sr = new System.IO.StreamReader("c:/f.txt");
//伪代码 get Connection
// DBTrans tran = Connection.BeginTran();
try{
while (!sr.EndOfStream)
{
string strReadLine = sr.ReadLine();
string[] strArray = strReadLine.Split(' ');
//getDBCommand
//存数据库 insert into table () values(strArray[0],strArray[1],strArray[2] )
//DBCommand.Excute(tran);
}
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
}
finally
{
Connection.Close();
sr.Close();
}
#6
同意楼上的说法
using System.Collections;
using System.IO;
...
{
FileInfo file = new FileInfo("1.text");
StreamReader strmR= file.OpenText();
//ArrayList txtArry = new ArrayList();
while (!strmR.EndOfStream)
{
//txtArry.Add(strmR.ReadLine());
string line = strmR.ReadLine();
if(line.Trim()!="")
{
string[] parms= line.Split(new char[] { ' ' });
InsertOracle(parms[0], parms[1], parms[3]);
}
}
strmR.Close();
}
...
private void InsertOracle(string p, string p_2, string p_3)
{
//数据存入数据库
...
}
using System.Collections;
using System.IO;
...
{
FileInfo file = new FileInfo("1.text");
StreamReader strmR= file.OpenText();
//ArrayList txtArry = new ArrayList();
while (!strmR.EndOfStream)
{
//txtArry.Add(strmR.ReadLine());
string line = strmR.ReadLine();
if(line.Trim()!="")
{
string[] parms= line.Split(new char[] { ' ' });
InsertOracle(parms[0], parms[1], parms[3]);
}
}
strmR.Close();
}
...
private void InsertOracle(string p, string p_2, string p_3)
{
//数据存入数据库
...
}
#7
顶