Search code examples
c#ms-accessoledb

Wrong data type OleDB Create Table


I m using the following function to create an access table:

        public static void createtable(string path, string tablename, string[] columnnames)
    {
        try
        {
            string connectionstring = creadteconnectionstring(path);
            OleDbConnection myConnection = new OleDbConnection(connectionstring);
            myConnection.Open();
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myConnection;
            string columnam = "[" + columnnames[0] + "] Text";

            for (int i = 1; i < columnnames.Length; i++)
            {
                    columnam = columnam + ", [" + columnnames[i] + "] Text";
            }

            myCommand.CommandText = "CREATE TABLE [" + tablename + "](" + columnam + ")";
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
            Console.WriteLine("Access table " + tablename + " created.");
        }
        catch 
        {
                Console.WriteLine("Access table " + tablename + " already exists.");
                return;


        }


    }

However if I open the access table in MS access the data type is Memo, not Text. Other data types link specified by MS dont seem to work at all.

Any ideas are welcome? Thanks!


Solution

  • The Access DDL TEXT data type behaves differently depending on the context where you execute your statement.

    From DAO in Access, this creates bar as a text field. But from ADO in Access, bar will be memo type.

    CREATE TABLE tblFoo (bar TEXT)
    

    Executing that statement from an OleDb connection produces the same result as with ADO in Access.

    If you want bar to be an actual text field, include a field length <= 255.

    CREATE TABLE tblFoo (bar TEXT(255))