我想使用asp.net将数据从Excel导入数据库。我正在使用VS-2012来做到这一点。
这是我点击事件的代码。但是当我上传文件并单击按钮时,页面显示“找不到页面”错误。
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("select * from [Sheet$]", OleDbcon);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
OleDbcon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str = "Gems1ConnectionString1";
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "tbl_energy_report";
bulkInsert.WriteToServer(dr);
Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
Label1.ForeColor = Color.Green;
Label1.Text = "Succefully Imported the File";
}
else
{
Label1.ForeColor = Color.Red;
Label1.Text = "Please Select A File";
}
}
请帮忙。
答案 0 :(得分:0)
尝试手动分配完整路径,而不使用fileupload和本地路径,如:
string path = @"C:\fileName.xls";
看看它是否有效!