以例子说明:

记事本内容显示如上图显示
数据库字段显示如下图显示
上传按钮代码:


string
name, fl;
name = this .Txt.Text;
string filename = "" ;
if (File1.PostedFile.ContentLength != 0 )
{
string fn = File1.PostedFile.FileName;
filename = getRandomFilename();
filename += fn.Substring(fn.LastIndexOf( " . " ));
if ((fn.Substring(fn.LastIndexOf( " . " ) + 1 ).ToLower() == " txt " ))
{
if (File1.PostedFile.ContentLength > 25000000 )
{
Response.Write( " <script>alert('不得上传大于25M的文件!')</script> " );
}
else
{
File1.PostedFile.SaveAs(Server.MapPath( " ~/txt " ) + " / " + filename);
}
}
else
{
Response.Write( " <script> alert(' " + fn.Substring(fn.LastIndexOf( " . " ) + 1 ).ToLower() + " 请选择格式为“txt”的文件')</script> " );
return ;
}
}
fl = " txt\\ " + filename;
this .Txt.Text = fl;
name = this .Txt.Text;
string filename = "" ;
if (File1.PostedFile.ContentLength != 0 )
{
string fn = File1.PostedFile.FileName;
filename = getRandomFilename();
filename += fn.Substring(fn.LastIndexOf( " . " ));
if ((fn.Substring(fn.LastIndexOf( " . " ) + 1 ).ToLower() == " txt " ))
{
if (File1.PostedFile.ContentLength > 25000000 )
{
Response.Write( " <script>alert('不得上传大于25M的文件!')</script> " );
}
else
{
File1.PostedFile.SaveAs(Server.MapPath( " ~/txt " ) + " / " + filename);
}
}
else
{
Response.Write( " <script> alert(' " + fn.Substring(fn.LastIndexOf( " . " ) + 1 ).ToLower() + " 请选择格式为“txt”的文件')</script> " );
return ;
}
}
fl = " txt\\ " + filename;
this .Txt.Text = fl;
命名记事本文件名代码:


static
public
string
getRandomFilename()
{
System.Random rand = new System.Random();
DateTime now = DateTime.Now;
string str = "" ;
str += now.Year.ToString();
str += now.Month.ToString();
str += now.Day.ToString();
str += now.Hour.ToString();
str += now.Minute.ToString();
str += now.Second.ToString();
str += rand.Next( 0 , 1000 );
return str;
}
{
System.Random rand = new System.Random();
DateTime now = DateTime.Now;
string str = "" ;
str += now.Year.ToString();
str += now.Month.ToString();
str += now.Day.ToString();
str += now.Hour.ToString();
str += now.Minute.ToString();
str += now.Second.ToString();
str += rand.Next( 0 , 1000 );
return str;
}
数据发布到数据库代码:
string f0=this.Txt.Text;
string f ="E:\\edt_manager\\"+f0;//获取文件存放路径
string sql = "BULK INSERT txt_table FROM '"+f+"' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n') ";//插入数据
edt.ExeSql(sql);
edt为类文件,连接数据库。以上就可以实现了。