将gridview数据导出到ms访问数据库(mdb)

时间:2015-01-02 09:21:02

标签: c# asp.net

如何将网格视图数据导出到Ms访问数据库。

我会尝试下面的代码:

private void ExportGridToAccess()
{

    Response.ContentType = "application/ms-access";
    Response.AddHeader("content-disposition", "attachment; filename=Vithal_Wadje.mdb"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document AccessDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(AccessDoc);
    PdfWriter.GetInstance(AccessDoc, Response.OutputStream);
    AccessDoc.Open();
    htmlparser.Parse(sr);
    AccessDoc.Close();
    Response.Write(AccessDoc);
    Response.End();
    GridView1.AllowPaging = true;
    GridView1.DataBind();
}

但是当我打开时下载文件:无法识别的数据库格式。正在显示警告

2 个答案:

答案 0 :(得分:1)

using ADOX; 
protected void btnacess_Click(object sender, EventArgs e)
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("Student_ID", typeof(Int32));
        dt.Columns.Add("Student_Name", typeof(string));
        dt.Columns.Add("Education", typeof(string));
        dt.Columns.Add("City", typeof(string));
        DataRow dtrow = dt.NewRow();
        dtrow["Student_ID"] = 1;
        dtrow["Student_Name"] = "Musakkhir";
        dtrow["Education"] = "MCA";
        dtrow["City"] = "Pune";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();
        dtrow["Student_ID"] = 2;
        dtrow["Student_Name"] = "Azhar";
        dtrow["Education"] = "M.E";
        dtrow["City"] = "Mumbai";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();
        dtrow["Student_ID"] = 3;
        dtrow["Student_Name"] = "Pervaiz";
        dtrow["Education"] = "M.Tech";
        dtrow["City"] = "Pune";
        dt.Rows.Add(dtrow);
        dtrow = dt.NewRow();
        dtrow["Student_ID"] = 4;
        dtrow["Student_Name"] = "Mustaheer";
        dtrow["Education"] = "M.S.";
        dtrow["City"] = "Pune";
        dt.Rows.Add(dtrow);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        ADOX.Catalog cat = new ADOX.Catalog();
        ADOX.Table table = new ADOX.Table();
        //Create the table and it's fields. 
        table.Name = "Table1";
        table.Columns.Append("PartNumber", ADOX.DataTypeEnum.adVarWChar, 60); // text[6]
        table.Columns.Append("AnInteger", ADOX.DataTypeEnum.adInteger, 10); // Integer 
        try
        {
            cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=d:/m11.mdb;" + "Jet OLEDB:Engine Type=5");
            cat.Tables.Append(table);
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" + "Data Source=d:/m11.mdb");
            int i = GridView1.Rows.Count;
            for (int j = 0; j <= i; j++)
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO Table1([PartNumber],[AnInteger]) VALUES (@FirstName,@LastName)";
                cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = GridView1.Rows[j].Cells[1].Text;
                cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = GridView1.Rows[j].Cells[0].Text;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        catch (Exception ex)
        {
        }
        cat = null;
    }

这将通过这种方式解决

答案 1 :(得分:0)

以下是将asp.net网格视图数据导出到MS Access的链接http://www.c-sharpcorner.com/UploadFile/0c1bb2/export-gridview-to-access-using-Asp-Net-C-Sharp/我希望这可能会有所帮助