我想上传一文件,在上传过程中读取其中的数据,并将其写入数据库中?
(就是在客户端(IE),让用户上传一txt文件,将其的数据倒入数据库中)
形如以下的.txt文档:
10001,王 强,3500,,
10002,李小华,4000,,
10003,王崇文,3500,,
10004,姚 敏,5000,,
1楼
这个问题用二进制流肯定不行。
上传后,实例化一个streamreader,然后读取,最后保存倒一个数组中。
dim reader as new streamreader(file.opentext(filepath,accessmod))
dim strContent as string=reader.readtoend()
dim arryContent()as string=strContent.split(",")
往下楼主应该明白了,数组里保存得是用逗号格开得
,如果你一组数据是一行,那就用reader.readline(),应该是这个东西!然后循环保存。
数组数据做参数。
2楼
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Request.RequestType=="POST")
{
if(this.UpFile.PostedFile.ContentLength>0)
{
int Pos = UpFile.PostedFile.FileName.LastIndexOf("//")+1;
FileName = UpFile.PostedFile.FileName.Substring(Pos);
FilePath="../" + FileName;
UpFile.PostedFile.SaveAs(Server.MapPath(FilePath));
UpFile.PostedFile.InputStream.Close();
ReadFile(Server.MapPath(FilePath));
}
else
{
Response.Write("<script language='Javascript'>alert('请选择要上传的文件!');history.go(-1);</script>");
Response.End();
}
}
}
3楼
楼上的代码是先上传文件,然后读取文件
private bool existedName(string usercode)
{
dc.refConn(ref dbconn);
dbconn.Open();
string sel="select count(*) from tab_xx where usercode='"+usercode+"'";
SqlCommand sc=new SqlCommand(sel,dbconn);
int ck=(int)sc.ExecuteScalar();
dbconn.Close();
if(ck>0)
{
return true;
}
return false;
}
这是判断数据库是否已存在记录
4楼
private void ReadFile(string path)
{
if(System.IO.File.Exists(path))
{
StreamReader reader=new StreamReader(path,System.Text.Encoding.GetEncoding("GB2312"));
reader.ReadLine();
string oneline;
while((oneline =reader.ReadLine()) != null)
{
oneline=oneline.Trim();
if(oneline != "")
{
string [] ss=oneline.Split(",".ToCharArray());
if(!existedName(ss[0].ToString()))
{
StringBuilder sb=new StringBuilder();
sb.Append("insert into tab_adminuser values(");
for(int i=0;i<ss.Length;i++)
{
sb.Append("'"+ss[i]+"'"+",");
}
sb.Append("'3,'");
// sb.Remove(sb.ToString().LastIndexOf(","),1);
sb.Append(")");
// Response.Write(sb.ToString());
// Response.End();
dc.exeSQL(sb.ToString());
}
}
}
reader.Close();//记得要关闭输入输出流
}
else
{
Response.Write("<script language='javascript'>alert('指定的文件不存在!');history.go(-1);</script>");
Response.End();
}
}
这个函数是读取文件
5楼
zhanqiangz(闲云野鹤) 正解,我做个和这个差不多的一个上传txt文件写如数据库的,也是这样处理。
streamreader sr;
sr = File.OpenText(filename);
while ((input=sr.ReadLine())!=null)
{
string[] col=input.Split(',');
//这个时候col里面的东西应该有你需要的值了
}
这样循环一行行读,应该ok
6楼
上传后,实例化一个streamreader,然后读取,最后保存倒一个数组中。
dim reader as new streamreader(file.opentext(filepath,accessmod))
dim strContent as string=reader.readtoend()
dim arryContent()as string=strContent.split(",")
往下楼主应该明白了,数组里保存得是用逗号格开得
,如果你一组数据是一行,那就用reader.readline(),应该是这个东西!然后循环保存。
数组数据做参数。
7楼
谢谢各位!
to:zhanqiangz(闲云野鹤)
ss[i]是否就是我需写入数据库的值?
可否简单注释一下最后一段代码,谢谢!
private void ReadFile(string path)
{
if(System.IO.File.Exists(path))
{
StreamReader reader=new StreamReader(path,System.Text.Encoding.GetEncoding("GB2312"));
reader.ReadLine();
string oneline;
while((oneline =reader.ReadLine()) != null)
{
oneline=oneline.Trim();
if(oneline != "")
{
string [] ss=oneline.Split(",".ToCharArray());
if(!existedName(ss[0].ToString()))
{
StringBuilder sb=new StringBuilder();
sb.Append("insert into tab_adminuser values(");
for(int i=0;i<ss.Length;i++)
{
sb.Append("'"+ss[i]+"'"+",");
}
sb.Append("'3,'");
//sb.Remove(sb.ToString().LastIndexOf(","),1);
sb.Append(")");
//Response.Write(sb.ToString());
//Response.End();
dc.exeSQL(sb.ToString());
}
}
}
reader.Close();//记得要关闭输入输出流
}
else
{
Response.Write("<script language='javascript'>alert('指定的文件不存在!');history.go(-1);</script>");
Response.End();
}
}
8楼
比较麻烦
用zhanqiangz(闲云野鹤)方法先把数据流读出来
然后,再根据您的 , /n 这两个符号把你的数据分开
然后保存入数据库
9楼
俺只会使用企业管理器导入
10楼
???那段代码是我贴出的,你问错人了吧?
11楼
呵呵!
Sorry, realconnection(龙苗)
能注释一下吗?
12楼
private void ReadFile(string path)
{
if(System.IO.File.Exists(path))//判断指定路径的文件是否存在
{
StreamReader reader=new StreamReader(path,System.Text.Encoding.GetEncoding("GB2312"));
//实例化一个StreamReader ,并且指定编码方式,不然读取中文的时候会出现乱码
reader.ReadLine();//先读取一行(如果文件上方没有标题之类的东西,可不要这行代码)
string oneline;
while((oneline =reader.ReadLine()) != null)
{
oneline=oneline.Trim();
if(oneline != "")
{
string [] ss=oneline.Split(",".ToCharArray());//每行的数据以,隔开,保存在数组里
if(!existedName(ss[0].ToString())) ss[0]是数组第一个元素,以此判断数据库中数据是否存在
{
StringBuilder sb=new StringBuilder();实例化StringBuilder
sb.Append("insert into tab_adminuser values(");
for(int i=0;i<ss.Length;i++)
{
sb.Append("'"+ss[i]+"'"+",");
}
sb.Append("'3,'");//以上几句是构造SQL的插入语句
//sb.Remove(sb.ToString().LastIndexOf(","),1);
sb.Append(")");
//Response.Write(sb.ToString());
//Response.End();
dc.exeSQL(sb.ToString());//执行SQL,DC是我的一个类,作用是完成数据库插入
}
}
}
reader.Close();//记得要关闭输入输出流
}
else
{
Response.Write("<script language='javascript'>alert('指定的文件不存在!');history.go(-1);</script>");
Response.End();
}
}
呵呵,其实挺简单的,现在应该没什么不明白的吧
13楼
先试试!