none
How to creat a new access tabel in C# RRS feed

  • Question

  • Hi everybody

    I want to add a new table to my access database programticly using C#

    I used the  data source configration wirzd to connect my access database

    but I dont't know how to add a new table to my database using a C# code

     

    can yopu help me plz !!

    thnx

    MSHR2 

     

     

    Sunday, July 15, 2007 2:43 PM

Answers

  • Hi, MSHR2

    You can utilize the ADOX to create an Access database.

    ADOX is an extension to ActiveX Data Objects that allows the manipulation of the database schema.

    Select References to add the following type libraries:

    • Microsoft ActiveX Data Objects 2.1 Library
    • Microsoft ADO Ext. 2.1 for DDL and Security

    Add the following code:

    Code Snippet

                ADODB.Connection Cn =new ADODB.Connection();

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

                ADOX.Table objTable = new ADOX.Table();

                Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb", string.Empty, string.Empty, 0);

                cat.ActiveConnection = Cn;

                objTable.Name = "Test_Table";

     

                //Create and Append a new field to the "Test_Table"

                //Columns Collection

                objTable.Columns.Append("PrimaryKey_Field", DataTypeEnum.adInteger, 0);

     

                //Create and Append a new key. Note that we are merely passing

                //the "PimaryKey_Field" column as the source of the primary key.

                //Thi snew Key will be Appended to the Keys Collection of

                //"Test_Table"

                objTable.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "PrimaryKey_Field", string.Empty, string.Empty);

     

                //Append the newly created table to the Tables Collection

                cat.Tables.Append(objTable);

     

                // clean up objects

                //objKey = null;

                objTable = null;

                cat = null;

                Cn.Close();

                Cn = null;

     

    You will see the result.

    Otherwise, you could check this article with VB.NET code: http://www.codeguru.com/vb/gen/vb_database/microsoftaccess/article.php/c5149/

     

    Thanks


    This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Tuesday, July 17, 2007 6:09 AM

All replies

  • Since you asking for help on how to do this in C#, I've moved the post to a C# forum - hopefully you'll have more luck there than in the VB forums

     

    Best regards,

    Johan Stenberg

    Sunday, July 15, 2007 9:42 PM
  • thannks Johan
    Sunday, July 15, 2007 11:08 PM
  • Hi, MSHR2

    You can utilize the ADOX to create an Access database.

    ADOX is an extension to ActiveX Data Objects that allows the manipulation of the database schema.

    Select References to add the following type libraries:

    • Microsoft ActiveX Data Objects 2.1 Library
    • Microsoft ADO Ext. 2.1 for DDL and Security

    Add the following code:

    Code Snippet

                ADODB.Connection Cn =new ADODB.Connection();

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

                ADOX.Table objTable = new ADOX.Table();

                Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb", string.Empty, string.Empty, 0);

                cat.ActiveConnection = Cn;

                objTable.Name = "Test_Table";

     

                //Create and Append a new field to the "Test_Table"

                //Columns Collection

                objTable.Columns.Append("PrimaryKey_Field", DataTypeEnum.adInteger, 0);

     

                //Create and Append a new key. Note that we are merely passing

                //the "PimaryKey_Field" column as the source of the primary key.

                //Thi snew Key will be Appended to the Keys Collection of

                //"Test_Table"

                objTable.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "PrimaryKey_Field", string.Empty, string.Empty);

     

                //Append the newly created table to the Tables Collection

                cat.Tables.Append(objTable);

     

                // clean up objects

                //objKey = null;

                objTable = null;

                cat = null;

                Cn.Close();

                Cn = null;

     

    You will see the result.

    Otherwise, you could check this article with VB.NET code: http://www.codeguru.com/vb/gen/vb_database/microsoftaccess/article.php/c5149/

     

    Thanks


    This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Tuesday, July 17, 2007 6:09 AM
  • thanks  Figo
    Tuesday, July 17, 2007 9:26 AM