我正在使用 C# 语言。我必须将 MS Access 数据库上传到服务器上的文件夹并从上传的数据库中获取数据并显示在 GridView 中。我做不到。
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileInfo f = new FileInfo(FileUpload1.PostedFile.FileName);
if (f.Extension.ToLower() == ".mdb" || f.Extension.ToLower() == ".accdb")
{
FileUpload1.SaveAs(Server.MapPath("~/Pics/"+f.Name+ "" + f.Extension.ToLower()));
string DBpath = Server.MapPath("~/Pics/" + f.Name);
string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBpath + "";
OleDbConnection cn = new OleDbConnection(connection);
OleDbDataAdapter da = new OleDbDataAdapter("Select * from Table1", cn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}