Re: Creating a new Access database file (.mdb) in .NET/C#



You can create the file with the Office bits. If you want to avoid this (as
I have in the past), create an empty shell MDB file and you can then run
your DDL commands against it. If the database "created" always has certain
tables and seed values, I would make the "shell" with those tables and that
info already in it.

To do this, copy the MDB file (shell) to a folder and run the DDL against it
to finish set up.

This only answers part of your issue, of course, but you can add this method
to the case where the file is missing or wrong, etc.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"Frnak McKenney" <frnak@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:13ibjsnicudog1a@xxxxxxxxxxxxxxxxxxxxx

I'm making changes to a small, portable, single-user database
application I wrote a couple of years back built around a muli-table
Access database and written in C# and .NET 1.1. Most of the changes
are are minor, but one involves "subsetting" the database, that is,
creating a new database file (.mdb) with a subset of the current
database's rows. Read in a schema and some tables from one database
/ file, write them out to a brand new database / file. Right?

Well, either I'm being incredibly dense, or this is an extremely
non-trivial task. Since it's difficult for me to imagine that
'CREATE' support wouldn't be hidden _somewhere_ in .NET's OleDb
support, I'm hoping that if I describe what I've tried someone will
put me out of my misery with a few, well-chosen -- but helpful --
comments on my obtuseness. <grin?>

Reading tables from the original database ("Filling") into
DataTables in a DataSet is fairly straightforward, and the 'web
abounds with examples of how to do this. Creating a new DataSet
with a duplicate schema isn't hard, either:

DataSet dsClone = dsOriginal.Clone();

followed by selected row-copies from the old to the new via
DataViews:

dsClone.Tables[tname].ImportRow( dv[i].Row );

Then you open a connection to the new database-to-be:

OleDbConnection connDb = new OleDbConnection();
connDb.ConnectionString = DbConnString1
+ filename
+ DbConnString2;
connDb.Open();

Whoops!

- If the file name doesn't already exist, you get an exception.
- If the file exists but hasn't been Access-initialized, you get
an exception ("wrong file structure" or something similar).

So either there is a secret substring I need to include in my
ConnectionString ("...Mode=Create;..." ?) to prepare the file for
a CREATE-like operation, or the file structure has to be "built" /
initialized prior to opening the Connection.

There's got to be some step there that I'm overlooking.

Hints would be appreciated. If I can't figure out how to do this, my
fallbacks include:

1) Byte-copying the current .mdb file -- while it's open -- under a
new name, opening ("connecting to") the new file, and then
emptying some tables and chopping out rows from others (and
maybe doing a compress-in-place on the result),

2) Adding an Empty-Db.mdb file ("...just the Schema, Ma'am") to the
package that would only be used for subsetting the database,
following the steps in option (1), or

3) Reverse-engineering the .mdb file structure and coding the
creation of a new one "by hand". (No, I'm not serious -- or not
entirely serious-- about this one, just frustrated enough to
start thinking anlong those lines.)

4) Embedding a binary string containing a complete (but table-less)
.mdb file into my applic... no, let's not go there. (But it
_would_ be portable. <grin!>)

Please, someone, point out to me that I've been repeatedly reading
past an OleDbConnection.OpenNew() method that does exactly what I'm
trying to do... or something along those lines! <grin>


Frank McKenney
--
"The most amazing achievement of the computer software industry is
its continuing cancellation of the steady and staggering gains
made by the computer hardware industry..." -- Henry Petroski
--
Frank McKenney, McKenney Associates
Richmond, Virginia / (804) 320-4887
Munged E-mail: frank uscore mckenney ayut minds pring dawt cahm (y'all)


.



Relevant Pages

  • Re: Copy over current Database
    ... However, the shell ... function is not letting me call out a .mdb file. ... The database will not need to be updated ... >> folder, and I can open it from UserDB, but how do I ...
    (microsoft.public.access.modulesdaovba)
  • RE: Copy over database
    ... However, the shell ... function is not letting me call out a .mdb file. ... The database will not need to be updated ... >> folder, and I can open it from UserDB, but how do I ...
    (microsoft.public.access.modulesdaovba)
  • Re: QRe: Legacy Access 2 VB 4 app, Word 11 merge on XP Pro SP2 client
    ... selecting "blank database" or some such. ... possible to set a simple password on a whole mdb file, ... case, make sure you have a backup of the .mdw file, then launch Access ... >I could not get the app to accept the changes to Notepad within the ...
    (microsoft.public.access.conversion)
  • Re: Basic security questions
    ... Your ASP page would "connect" to the file on the Unix box via ADO or ADO.NET ... Password protected databases are different from database protected by ULS ... ULS protected databases require the use of the .mdw workgroup file ... >> If you double-click an mdb file, the sequence of actions is as follows. ...
    (microsoft.public.access.security)
  • Re: Help connecting to Access DB from web service
    ... I would love to move the database to SQL Express but that is not an option ... Given that I can not move the data to a better database, ... application that will also open and access the mdb file, if so, I'm ... appliation runs under the asp.net worker process (under the worker proces ...
    (microsoft.public.dotnet.framework.webservices)