Check for MS Access database table if not exist create it

后端 未结 5 1746
失恋的感觉
失恋的感觉 2020-12-20 14:55

How do you programmatically check for MS Access database table, if not exist then create it?

5条回答
  •  一个人的身影
    2020-12-20 15:42

    an easy way to do this is

    public bool CheckTableExistance(string TableName)
        {
            // Variable to return that defines if the table exists or not.
            bool TableExists = false;
    
            // Try the database logic
            try
            {
                // Make the Database Connection
                ConnectAt();
    
                // Get the datatable information
                DataTable dt = _cnn.GetSchema("Tables");
    
                // Loop throw the rows in the datatable
                foreach (DataRow row in dt.Rows)
                {
                    // If we have a table name match, make our return true
                    // and break the looop
                    if (row.ItemArray[2].ToString() == TableName)
                    {
                        TableExists = true;
                        break;
                    }
                }
    
                //close database connections!
                Disconnect();
                return TableExists;
            }
            catch (Exception e)
            {
                // Handle your ERRORS!
                return false;
            }
        }
    

提交回复
热议问题