asp.net怎样将文件导入到数据库的表中,默认保存为路径
protected void btnOk_Click(object sender, EventArgs e){
//添加学生信息
#region
if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
{
Label2.Visible = true;
Label2.Text = "请选择导入文件";
}
//string IsXIs = System.IO.Path.GetExtension(FileUpload1.FileName).ToString();//System.IO.Path.GetExtension获得文件的扩展名
//if (IsXIs != ".doc")
//{
// lblShangChuan.Visible = true;
// lblShangChuan.Text = "只能选择word文件";
// return;//当选择的不是Excel文件时,返回
//}
string filename = FileUpload1.FileName;//获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("~/HouTai/wordfiles/") + filename);//Server.MapPath 获得虚拟服务器相对路径
FileUpload1.SaveAs(savePath);//SaveAs 将上传的文件内容保存在服务器上
ds = ExecleDs(savePath, filename);//调用自定义方法
DataRow[] dr = ds.Tables[0].Select();//定义一个DataRow数组
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Label2.Visible = true;
Label2.Text = "导入表为空表,无数据";
}
else
{
for (int i = 0; i < dr.Length; i++)
{
pathmodel.SName = txtname.Text;
pathmodel.PathAdd = filename;
pathmodel.DateTime = DateTime.Now.Date;
//存储到数据库中
ds = path.GetList("SName=" + txtname.Text + "");
if (ds.Tables[0].Rows.Count>0)
{
path.Update(pathmodel);
Label2.Visible = true;
Label2.Text = "添加成功!";
}
else
{
path.Add(pathmodel);
Label2.Visible = true;
Label2.Text = "添加成功!";
}
}
Repeater1.DataBind();
}
#endregion
}
public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [word.doc]", conn);
odda.Fill(ds, table);
return ds;
} --------------------编程问答--------------------
补充:.NET技术 , ASP.NET