Jump to content

OLEDB Problems

- - - - -

  • Please log in to reply
No replies to this topic

#1
reed

reed

    Newbie

  • Members
  • PipPip
  • 15 posts
I swear this code worked before, but now it doesn't seem to matter what I put in my query, it will fail every time when I try to create the table. Visual Studio tells me that there is a syntax error in my query. This doesn't make any sense.


class DataHandler

    {

        private const string MASTER_DB_LOCATION = @".\ATAMaster.accdb";

        private OleDbConnection myconnection;

        private OleDbCommand command;

        private const string ATA_MASTER_SHOOTER_TABLE = "Master";


        //Constructor

        public DataHandler()

        {

        }


        //Begin Methods


        //This method creates an Access 2002-2003 database at DB_LOCATION

        //params: in

        public void CreateDB(string dbloc)

        {

            try

            {

                if (!File.Exists(dbloc))

                {

                    Catalog cat = new Catalog();

                    cat.Create("Provider=Microsoft.JET.OLEDB.4.0;" +

                               "Data Source= " + dbloc + ";" +

                               "Jet OLEDB:Engine Type=5");


                    MessageBox.Show("New Database has been created.");

                    cat = null;

                }

            }

            catch (Exception e)

            {

                MessageBox.Show("Failed to Create Database",e.Message);

            }

        }


        private void ConnectToDB(string dbloc)

        {

            try

            {

                myconnection = new OleDbConnection();

                myconnection.ConnectionString = @"Provider=Microsoft." + 

                                        @"Jet.OLEDB.4.0;" +

                                        @"Data source= " + dbloc;

                myconnection.Open();

            }

            catch (Exception e)

            {

                MessageBox.Show("Failed to Connect to Database",e.Message);

            }

        }


        private void CreateTable(string tblname, string[] tblfields)

        {

            //Construct Command String from Params

            string mycommand = "CREATE TABLE " + tblname + "( ";

            for (int i = 0; i < tblfields.Length; i++)

            {

                mycommand += tblfields[i];

                if (i < tblfields.Length - 1)

                    mycommand += " , ";

                else

                    mycommand += " )";

            }


            //Construct object and execute query

            command = new OleDbCommand(mycommand,myconnection);

            command.ExecuteNonQuery();

        }


        public void DBMain()

        {

            CreateDB(MASTER_DB_LOCATION);

            ConnectToDB(MASTER_DB_LOCATION);

            string table = "shoots ";

            string[] field = new string[1];

            field[0] = "fieldnme";

            //Create Master Table

            CreateTable(table, field);

            //CreateTable(TODO);

            //Create Perpetual Options Table

            //CreateTable(TODO);

        }

    }


Can anyone figure out what's wrong?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users