Creating a Access database using ADOX and C# 
Author Message
 Creating a Access database using ADOX and C#

Hi,

I'm using C# to create an application which will use an access database.
I'm using ADOX to actually create the Access database, this works fine.
However when I try to create and append a table to the database I get an
error (The data bank module cannot find ' [AccessLevel] '. Guarantee that it
is about a valid parameter or alias name which contains no invalid marks or
wrong punctuation and whose name is not too long.)

As far as I can tell the database and table are valid.  Can anyone help?

Thanks in advance

Rob.

<code snippet>


// For access databases, I need to create the file first.


{

// If it exists overwrite it.


Quote:
}

// Use ADOX to create the database.

ADOX.CatalogClass userPrefsDB = new ADOX.CatalogClass();

userPrefsDB.Create( connectionString );

ADODB.ConnectionClass dbConnection = new ADODB.ConnectionClass();


userPrefsDB.ActiveConnection = dbConnection;

ADOX.TableClass accessLevel = new ADOX.TableClass();

accessLevel.ParentCatalog = database;


// Append the table to the database

database.Tables.Append(accessLevel);



Mon, 30 May 2005 00:34:04 GMT  
 Creating a Access database using ADOX and C#
Hi Rob,

I am not sure why you need square brackets in the TableName. Can you test it
out by removing brackets from the table name?
There is a C# article on creating a database via ADOX however, it does not have
any sample code for adding a Table. So I will give you a link to old VB6 code.

252908 HOWTO: Create a Table with Primary Key Through ADOX
http://support.microsoft.com/?id=252908

You should be able to grab the logic from this sample code.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Mon, 30 May 2005 05:09:19 GMT  
 Creating a Access database using ADOX and C#
HI Bharat,

Thanks for that, it worked (removing the square brackets).  The reason for
the square brackets is a hang up from SQL where I use the square brackets to
avoid any potential confussion with SQL keywords, I assumed I would need the
same with this code, but obviously not.

Thanks again.

Rob.



Quote:
> Hi Rob,

> I am not sure why you need square brackets in the TableName. Can you test
it
> out by removing brackets from the table name?
> There is a C# article on creating a database via ADOX however, it does not
have
> any sample code for adding a Table. So I will give you a link to old VB6
code.

> 252908 HOWTO: Create a Table with Primary Key Through ADOX
> http://support.microsoft.com/?id=252908

> You should be able to grab the logic from this sample code.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Mon, 30 May 2005 18:15:12 GMT  
 Creating a Access database using ADOX and C#
Hi,

Further to my last problem, is there a way of setting a column to be of type
AutoNumber using code.

Thanks

Rob.


Quote:
> Hi,

> I'm using c# to create an application which will use an access database.
> I'm using ADOX to actually create the Access database, this works fine.
> However when I try to create and append a table to the database I get an
> error (The data bank module cannot find ' [AccessLevel] '. Guarantee that
it
> is about a valid parameter or alias name which contains no invalid marks
or
> wrong punctuation and whose name is not too long.)

> As far as I can tell the database and table are valid.  Can anyone help?

> Thanks in advance

> Rob.

> <code snippet>

=

> // For access databases, I need to create the file first.


> {

> // If it exists overwrite it.


> }

> // Use ADOX to create the database.

> ADOX.CatalogClass userPrefsDB = new ADOX.CatalogClass();

> userPrefsDB.Create( connectionString );

> ADODB.ConnectionClass dbConnection = new ADODB.ConnectionClass();


> userPrefsDB.ActiveConnection = dbConnection;

> ADOX.TableClass accessLevel = new ADOX.TableClass();

> accessLevel.ParentCatalog = database;


> // Append the table to the database

> database.Tables.Append(accessLevel);



Mon, 30 May 2005 21:17:49 GMT  
 Creating a Access database using ADOX and C#
Rob,

You should be able to do that using the Properties collection of the Column
object.
See the following link for an example:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnim...
o0143.asp

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Mon, 30 May 2005 23:55:04 GMT  
 Creating a Access database using ADOX and C#
Bharat,

I've seen that article before.  The problem is that it is in VB.  When I try
to do the same in C#

    ADOX.CatalogClass db = new ADOX.CatologClass();
    // Set the connection string.  (Not included)
    ADOX.TableClass tbl = new ADOX.ColumnClass();
    ADOX.ColumnClass col = new ADOX.ColumnClass();
    tbl.ParentCatolog = db;
    // Set other column properties.
    col.Properties["AutoIncrement"] = true;

I get the following compile errors

    Cannot implicitly convert type 'bool' to 'ADOX.Property'
    Property or indexer 'ADOX.Properties.this[object]' cannot be assigned
to -- it is read only

Is there a method I can achieve the same thing but using C#?  Am I doing
something stupid in the C# code?

Thanks

Rob.



Quote:
> Rob,

> You should be able to do that using the Properties collection of the
Column
> object.
> See the following link for an example:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnim...
l/im
Quote:
> o0143.asp

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Tue, 31 May 2005 00:24:39 GMT  
 Creating a Access database using ADOX and C#
Hi Rob,

I will check this out by creating some sample code. I am sure since these are
dynamic properties dependent on the Provider, compiler is not going to find it
and will complain about it. I will see if I can find some work around by
contacting DataBase team.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Tue, 31 May 2005 07:15:46 GMT  
 Creating a Access database using ADOX and C#
HI Bharat,

This was the same thought I had.  I think it works in VB because VB uses
late binding, C# will use early binding.  Is there away that I can set it to
late binding?

Thanks for your help.

Rob.



Quote:
> Hi Rob,

> I will check this out by creating some sample code. I am sure since these
are
> dynamic properties dependent on the Provider, compiler is not going to
find it
> and will complain about it. I will see if I can find some work around by
> contacting DataBase team.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Tue, 31 May 2005 17:36:13 GMT  
 Creating a Access database using ADOX and C#
Hi Rob,

Bharat got me involved in this and I couldn't resist the challenge<G>  Considering I am NOT  really good at C# yet, Here is what I got..

                private void btnadox_Click(object sender, System.EventArgs e)
                {
                        ADOX.CatalogClass cat = new ADOX.CatalogClass();

                        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
                                "Data Source=C:\\NewMDB.mdb;" +
                                "Jet OLEDB:Engine Type=5");
                        Console.WriteLine("Database Created Successfully");

                        ADOX.TableClass tbl = new ADOX.TableClass();
                        tbl.ParentCatalog = cat;
                        tbl.Name="Table1";

                        ADOX.ColumnClass col = new ADOX.ColumnClass();
                        col.ParentCatalog = cat;
                        col.Type=ADOX.DataTypeEnum.adInteger;
                        col.Name = "IDField";
                        col.Properties["Jet OLEDB:Allow Zero Length"].Value= false;
                        col.Properties["AutoIncrement"].Value= true;
                                                tbl.Columns.Append (col,ADOX.DataTypeEnum.adInteger,0);

                        ADOX.ColumnClass col2 = new ADOX.ColumnClass();
                        col2.ParentCatalog = cat;
                        col2.Name = "Description";
                        col2.Properties["Jet OLEDB:Allow Zero Length"].Value= false;
                        tbl.Columns.Append (col2,ADOX.DataTypeEnum.adVarChar,25);
                        tbl.Keys.Append("PrimaryKey",ADOX.KeyTypeEnum.adKeyPrimary,"IDField","","");

                        cat.Tables.Append (tbl);

                        cat = null;
                }

The code generates at Database, Adds a table with 2 fields the first being an Autonumber Primary Key field... Hopefully this does the trick...

Want to know more? Check out the MSDN Library at http://msdn.microsoft.com  or the Microsoft Knowledge Base at http://support.microsoft.com

Scot Rose, MCSD
Microsoft Visual Basic Developer Support

This posting is provided AS IS, with no warranties, and confers no rights.



Tue, 31 May 2005 21:25:33 GMT  
 Creating a Access database using ADOX and C#
Hi Scot, Bharat,

Thanks for your help.  That worked a treat.  So simply when you know how!?!

Thanks again.

Rob.


Quote:
> Hi Rob,

> Bharat got me involved in this and I couldn't resist the challenge<G>

Considering I am NOT  really good at C# yet, Here is what I got..
Quote:

> private void btnadox_Click(object sender, System.EventArgs e)
> {
> ADOX.CatalogClass cat = new ADOX.CatalogClass();

> cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
> "Data Source=C:\\NewMDB.mdb;" +
> "Jet OLEDB:Engine Type=5");
> Console.WriteLine("Database Created Successfully");

> ADOX.TableClass tbl = new ADOX.TableClass();
> tbl.ParentCatalog = cat;
> tbl.Name="Table1";

> ADOX.ColumnClass col = new ADOX.ColumnClass();
> col.ParentCatalog = cat;
> col.Type=ADOX.DataTypeEnum.adInteger;
> col.Name = "IDField";
> col.Properties["Jet OLEDB:Allow Zero Length"].Value= false;
> col.Properties["AutoIncrement"].Value= true;
>                                                 tbl.Columns.Append

(col,ADOX.DataTypeEnum.adInteger,0);
Quote:

> ADOX.ColumnClass col2 = new ADOX.ColumnClass();
> col2.ParentCatalog = cat;
> col2.Name = "Description";
> col2.Properties["Jet OLEDB:Allow Zero Length"].Value= false;
> tbl.Columns.Append (col2,ADOX.DataTypeEnum.adVarChar,25);

tbl.Keys.Append("PrimaryKey",ADOX.KeyTypeEnum.adKeyPrimary,"IDField","","");
Quote:

> cat.Tables.Append (tbl);

> cat = null;
> }

> The code generates at Database, Adds a table with 2 fields the first being

an Autonumber Primary Key field... Hopefully this does the trick...
Quote:

> Want to know more? Check out the MSDN Library at http://msdn.microsoft.com

or the Microsoft Knowledge Base at http://support.microsoft.com

- Show quoted text -

Quote:

> Scot Rose, MCSD
> Microsoft Visual Basic Developer Support

> This posting is provided "AS IS", with no warranties, and confers no
rights.



Fri, 03 Jun 2005 17:53:23 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Creating MSDE database using ADO/ADOX with VC++

2. Create access database on the fly using C#

3. Clone an empty database using ADOX.....

4. Using ADOX with C#

5. Using ADOX in C#

6. I am creating a new query using ADOX and no matter

7. Creating tables in an access database using ODBC???

8. create autonum field using adox

9. How to create a new Access database, table, and field using CDaoDatabase, CDasoTableDef

10. Using C# to Access Binary Files Created in C programs

11. Create database without using VC++ database tools

12. Problem accessing Access 2000 database using DAO. - URGENT

 

 
Powered by phpBB® Forum Software