我检查了上面的注释,但它们没有帮助。我使用 VS2008 for ASP.Net 和 MS Access 2010 作为数据库。我需要通过 ASP 网页将数据从 excel 上传到数据库。
但我收到如下错误:
“Microsoft Jet 数据库引擎无法打开文件 'C:\Users\poonamj\Documents\Visual Studio 2008\Projects\SmartTool\SmartTool\Uploads\'。它已被其他用户以独占方式打开,或者您需要权限才能查看其数据。”
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Data.OleDb;
namespace SmartTool
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void UploadFile(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void ViewFile(object sender, EventArgs e)
{
//string filePath = (sender as LinkButton).CommandArgument;
// File.ReadAllLines(filePath);
//GridView2.DataSource = File.ReadAllLines(filePath);
//GridView2.DataBind();
//string[] content = File.ReadAllLines(filePath);
//GridView2.DataSource = content.
// OleDbConnection conn = new OleDbConnection();
// conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/poonamj/Documents/Visual Studio 2008/Projects/SmartTool/SmartTool/fallout.accdb;User id=admin;Password=";
// conn.Open();
string Access = Server.MapPath("App_Data/fallout.accdb");
string Excel = Server.MapPath("~/Uploads/");
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;Mode=12;";
using (OleDbConnection conn = new OleDbConnection(connect))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT * INTO [MS Access;Database=" + Access + "].[New Table] FROM [Sheet1$]";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
}
}