Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to create a table in access by c# code where name can be select by user thanks for notice me
Posted

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

// Add references for the following COM Objects to the project:
//
//  - Microsoft ActiveX Data Objects 6.0 Library
//  - Microsoft ADO Ext. 6.0 for DDL and Security 
//
using ADOX;
using ADODB;

namespace AccessTest
{
    public partial class Form1 : Form
    {
        const string _ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=BookCSharp.mdb";

        private Catalog OpenDatabase()
        {
            Catalog catalog = new Catalog();
            Connection connection = new Connection();
        
            try
            {
                connection.Open( _ConnectionString);
                catalog.ActiveConnection = connection;
            }
            catch (Exception)
            {
                catalog.Create(_ConnectionString);
            }
            return catalog;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            // Only for demonstration purposes, no error checks:
            // This code will only work as long as the table "Publisher" does not exist

            // First create an new database if necessary
            Catalog cat = OpenDatabase();

            // Create a new table "Publisher" using ADOX ...
            Table table = new Table();
            table.Name = "Publisher";
            cat.Tables.Append(table);

            // Add Column "PublisherID" with Autoincrement
            ADOX.Column col = new Column();
            col.Name = "PublisherID";
            col.ParentCatalog = cat;
            col.Type = ADOX.DataTypeEnum.adInteger;
            col.Properties["Nullable"].Value = false;
            col.Properties["AutoIncrement"].Value = true;
            table.Columns.Append(col);

            // Add column "PublisherName"
            col = new Column();
            col.Name = "PublisherName";
            col.ParentCatalog = cat;
            col.Type = ADOX.DataTypeEnum.adWChar;
            col.DefinedSize = 50;
            col.Attributes = ColumnAttributesEnum.adColNullable;
            table.Columns.Append(col);

            // Make "PublisherID" the primary key
            ADOX.Index index = new ADOX.Index();
            index.PrimaryKey = true;
            index.Name = "PK_Publisher";
            index.Columns.Append("PublisherID", table.Columns["PublisherID"].Type, table.Columns["PublisherID"].DefinedSize);
            table.Indexes.Append(index);

            MessageBox.Show("A new Data Table is successfully Created");

        }

        public Form1()
        {
            InitializeComponent();
        }

    }


}



Hope that helps,
Sharath kumar
 
Share this answer
 
v2
Comments
Member 11221917 17-Feb-15 5:10am    
How can I populate a column with a list?
rijo 9-Nov-15 16:06pm    
How append a date column with format dd-MMM-yyyy
try this:-

// Creating OLEDB connection string for Ms-Access 2007 database file
            OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= D:\\MyDatabase.accdb;Persist Security Info=False;");
            myConnection.Open();
            // Create Oledb command to execute particular query
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;
            // Query to create table with specified data columne
            myCommand.CommandText = "CREATE TABLE tblIdentityTesting([MyIdentityColumn] long, [Name] text)";
            myCommand.ExecuteNonQuery();
            MessageBox.Show("Table Created Successfully");


if you using ms-access 2003 then change
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= D:\\MyDatabase1.mdb; OLE DB Services=-1");
 
Share this answer
 
Comments
TUMAAJKAL 2-Jan-14 8:24am    
my friend i also want to select name of table by user only please tell me how could i do that thanks for your answer and time.
Member 12978870 28-Oct-17 6:11am    
how to create column with attachment property in ms access
Member 13489153 30-Oct-17 4:36am    
How to check if this code works or not?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900