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;
        } 
    }
}
Ozma64
  • 23
  • 1
  • 7
  • What happens if you execute `CREATE TABLE` commands instead of using `ADOX`? – Dai Mar 25 '14 at 19:03
  • i havent gotten that far with directly querying a database file in C#....im used to doing it with PHP so im not 100% on how to create a connection and sending commands to the database – Ozma64 Mar 25 '14 at 19:36
  • Like @Dai I would start off by querying an existing database. Once that is working then begin using ADOX, creating one column at a time. – Jeremy Cook Mar 25 '14 at 19:59
  • Well Id rather use ADOX because it "seems" more efficient on making a database to me (save for the errors that I have been getting), I want to use ADOX to be able to automate creating a new database if the database file ever gets deleted...essentially ADOX is more compact and direct on creating it where querying it is longer and drawn out. at least in my opinion....From what I have gathered about my error message it is because I'm having a conflict between two size values I believe. Default Size and a length property that I can't seem to locate. But I'm not 100% sure. – Ozma64 Mar 25 '14 at 21:02

0 Answers0