创建访问后如何将访问文件作为Web响应返回?

时间:2018-09-02 04:25:54

标签: c# asp.net webforms

我正在创建一个带有一些字段的访问文件,并在其中插入一些记录,我想将它作为Web响应作为Web响应返回给Asp.net Webforms。

我已经做到了:

boundary = dfff["geometry"][1:3].unary_union

但是当我想返回它作为响应时,会看到以下错误:

  

该进程无法访问该文件,因为该文件正在被另一个文件使用   过程。

我什至尝试过:

        CatalogClass cat = new CatalogClass();
        ADOX.Table table = new ADOX.Table();
        table.Name = "Table1";
        table.Columns.Append("Field1");
        table.Columns.Append("Field2");
        string str = "provider=Microsoft.Jet.OleDb.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "DataBase\\" + "t" + ".mdb;";
        cat.Create(str);
        cat.Tables.Append(table);
        using (OleDbConnection connection = new OleDbConnection(str))
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "INSERT INTO Table1 (Field1, Field2) VALUES (1, 2)";

            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Dispose();
            connection.Close();

        }
        cat.ActiveConnection = null;
        cat = null;
        table = null;
        string fileName = AppDomain.CurrentDomain.BaseDirectory + "DataBase\\" + "t" + ".mdb";
        Response.AddHeader("Content-Disposition",
        string.Format("attachment; filename={0}", fileName));
        Response.TransmitFile(fileName);
        Response.End();

但这没用。

如何解决?

1 个答案:

答案 0 :(得分:0)

最后我找到了问题。

我找到了另一种创建文件并将其作为响应返回的方法,如下所示:

        ADOX.Table table = new ADOX.Table();
        table.Name = "Users";   // Table name

        // Column 1 (id)
        ADOX.ColumnClass idCol = new ADOX.ColumnClass();
        idCol.Name = "Id";  // The name of the column
        idCol.ParentCatalog = catalog;
        idCol.Type = ADOX.DataTypeEnum.adInteger;   // Indicates a four byte signed integer.
        idCol.Properties["AutoIncrement"].Value = true;     // Enable the auto increment property for this column.

        // Column 2 (Name)
        ADOX.ColumnClass nameCol = new ADOX.ColumnClass();
        nameCol.Name = "Name";    // The name of the column
        nameCol.ParentCatalog = catalog;
        nameCol.Type = ADOX.DataTypeEnum.adVarWChar;   // Indicates a string value type.
        nameCol.DefinedSize = 60;   // 60 characters max.


        // Column 3 (Surname)
        ADOX.ColumnClass surnameCol = new ADOX.ColumnClass();
        surnameCol.Name = "Surname";    // The name of the column
        surnameCol.ParentCatalog = catalog;
        surnameCol.Type = ADOX.DataTypeEnum.adVarWChar;   // Indicates a string value type.
        surnameCol.DefinedSize = 60;   // 60 characters max.

        table.Columns.Append(idCol);        // Add the Id column to the table.
        table.Columns.Append(nameCol);      // Add the Name column to the table.
        table.Columns.Append(surnameCol);   // Add the Surname column to the table.

        catalog.Tables.Append(table);   // Add the table to our database.             

        // Close the connection to the database after we are done creating it and adding the table to it.
        ADODB.Connection con = (ADODB.Connection)catalog.ActiveConnection;
        if (con != null && con.State != 0)
            con.Close();




        string fileName = AppDomain.CurrentDomain.BaseDirectory + "DataBase\\t.mdb";

        Response.ContentType = "application/x-msaccess";
        Response.AddHeader("Content-Disposition",string.Format("attachment; filename={0}", fileName));
        Response.TransmitFile(fileName);
        Response.End();