我如何在.msdb格式的asp.net创建的MS访问中打开文件

时间:2013-07-22 10:01:16

标签: c#

如何在MS Access中导出gridview?

我正在使用此代码

 Response.ClearContent();
 Response.Buffer = true;
 Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.mdb"));
 Response.ContentType = "application/ms-access";
 StringWriter sw = new StringWriter();
 HtmlTextWriter htw = new HtmlTextWriter(sw);
 DataGrid dGrid = new DataGrid();
 dGrid.AllowPaging = false;
 dGrid.DataSource = dt;
 dGrid.DataBind();
 dGrid.RenderControl(htw);
 Response.Write(sw.ToString());
 Response.End();

但是此代码文件未在MS访问中打开 我该怎么办?

1 个答案:

答案 0 :(得分:-1)

我使用以下代码使用C#开放MS访问数据库:

在这里输入代码

    ADOX.Catalog cat = new ADOX.Catalog();
    ADOX.Table table = new ADOX.Table();


    table.Name = "Table1";
    table.Columns.Append("PartNumber", ADOX.DataTypeEnum.adVarWChar, 6); 
    table.Columns.Append("AnInteger", ADOX.DataTypeEnum.adInteger, 10); 
    try
    {
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=d:/m2.accdb;" + "Jet OLEDB:Engine Type=5");
        cat.Tables.Append(table);
        OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" + "Data Source=d:/m2.accdb");
       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 =“neha”;

             cmd.Parameters.Add("@LastName", 

OleDbType.VarChar).Value = 20;

             cmd.ExecuteNonQuery();

            conn.Close();                                 

    }
    catch (Exception ex)
    {
        result = false;
    }
    cat = null;