How do I create an Access 2010 database and configure the tables and column?

0

I have been trying to create an Access 2010 database for a few days now and I get all sorts of errors each attempt I make. I have the drivers installed already and I have made references to ADOX and ADODB. The most recent error I have encountered is 0x80040E21 which is "An OLE DB error has occurred. Error code: 0x80040E21. An OLE DB record is available. Source: "Microsoft Native Client" Hresult: 0x80040E21 Description: "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.". Cannot create an OLE DB accessor. Verify that the column metadata is valid."

I'm not entirely sure as to the cause of this error. I have my entire program below. I just want to simply create a database with 1 table, and fill it with a few columns and set the data types of the columns and the size of the entries. Does anyone have any ideas of what I'm missing?

I could also like help to figure out how to set the auto increment of the ID column as well.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADOX;
using ADODB;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateNewAccessDatabase(@"C:\Users\owner\Desktop\Reports.accdb");
        }

        public bool CreateNewAccessDatabase(string fileName)
        {
            bool result = false;
            ADOX.Catalog cat = new ADOX.Catalog();
            ADOX.Table table = new ADOX.Table();
            table.Name = "ServerReports";

            ADOX.Column ID = new ADOX.Column();
            ID.Type = ADOX.DataTypeEnum.adBigInt;
            ID.DefinedSize = 19;
            ID.Name = "ID";

            ADOX.Column INDEX = new ADOX.Column();
            INDEX.Type = ADOX.DataTypeEnum.adInteger;
            INDEX.DefinedSize = 10;
            INDEX.Name = "Index";

            ADOX.Column NAME = new ADOX.Column();
            NAME.Type = ADOX.DataTypeEnum.adVarChar;
            NAME.DefinedSize = 256;
            NAME.Name = "Name";

            ADOX.Column ADDRESS = new ADOX.Column();
            ADDRESS.Type = ADOX.DataTypeEnum.adVarChar;
            ADDRESS.DefinedSize = 256;
            ADDRESS.Name = "Address";

            ADOX.Column STATUS = new ADOX.Column();
            STATUS.Type = ADOX.DataTypeEnum.adBoolean;
            STATUS.DefinedSize = 1;
            STATUS.Name = "Status";

            ADOX.Column PING = new ADOX.Column();
            PING.Type = ADOX.DataTypeEnum.adDouble;
            PING.DefinedSize = 9;
            PING.Name = "Ping";

            ADOX.Column DATE = new ADOX.Column();
            DATE.Type = ADOX.DataTypeEnum.adDate;
            DATE.Name = "Date";

            table.Columns.Append(ID);
            table.Columns.Append(INDEX);
            table.Columns.Append(NAME);
            table.Columns.Append(ADDRESS);
            table.Columns.Append(STATUS);
            table.Columns.Append(PING);
            table.Columns.Append(DATE);

            table.Keys.Append("ID", ADOX.KeyTypeEnum.adKeyPrimary, "ID", "ServerReports", "ID");

            try
            {
                cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Persist Security Info=False;");
                cat.Tables.Append(table);
                ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
                if (con != null)
                    con.Close();
                textBox1.Text = "Database Created Successfully";
                result = true;
            }
            catch (Exception ex)
            {
                textBox1.Text = Convert.ToString(ex);
                result = false;
            }
            cat = null;
            return result;
        } 
    }
}
c#
database
ms-access-2010
adodb
adox
asked on Stack Overflow Mar 25, 2014 by Ozma64

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0