locked
create a runtime table in Ms Access database RRS feed

  • Question

  • User-1709593219 posted

    I want to create table at runtime for an existing Ms Access database using ASP.net 2.0 and C#. Can anyone please guide me through it ?

    Monday, December 11, 2006 9:21 AM

Answers

  • User-186692512 posted

    Maybe something like this:

    void createTable(string connString, string tableName) 
    {
    	 try
    	 {
    			OleDbConnection con = new OleDbConnection(connString);
    			OleDbCommand cmd = con.CreateCommand();
    	
    			string cmdText = String.Format("CREATE TABLE {0} (dno int primary key, dname char(15), grade int, avg int)",tableName);
    		
    			cmd.CommandType = CommandType.Text;
    			cmd.CommandText = cmdText;
    			con.Open();
    	
    			cmd.ExecuteNonQuery();
    	 }
    	 catch (Exception e)
    	 {
    			// Process error
    			log.Error("Exception occured", e);
    	 }
    	 finally
    	 {
    			// Close connection if open
    		 
    			try { if (con != null) con.Close(); }
    			catch (Exception ex)
    			{
    				 log.Warn("Unable to close connection - exception occured.", ex);
    			}
    	 }
    } 
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2006 11:00 AM

All replies

  • User-186692512 posted

    Maybe something like this:

    void createTable(string connString, string tableName) 
    {
    	 try
    	 {
    			OleDbConnection con = new OleDbConnection(connString);
    			OleDbCommand cmd = con.CreateCommand();
    	
    			string cmdText = String.Format("CREATE TABLE {0} (dno int primary key, dname char(15), grade int, avg int)",tableName);
    		
    			cmd.CommandType = CommandType.Text;
    			cmd.CommandText = cmdText;
    			con.Open();
    	
    			cmd.ExecuteNonQuery();
    	 }
    	 catch (Exception e)
    	 {
    			// Process error
    			log.Error("Exception occured", e);
    	 }
    	 finally
    	 {
    			// Close connection if open
    		 
    			try { if (con != null) con.Close(); }
    			catch (Exception ex)
    			{
    				 log.Warn("Unable to close connection - exception occured.", ex);
    			}
    	 }
    } 
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 11, 2006 11:00 AM
  • User-902308856 posted

    Here is code modified from Dejan:

       }
        void createTable(string connString, string tableName)
        {
            OleDbConnection con = new OleDbConnection(connString);
            OleDbCommand cmd = con.CreateCommand();
    
            string cmdText = String.Format("CREATE TABLE {0} (dno int primary key, dname char(15), grade int, avg int)", tableName);
    
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = cmdText;
            try
            {
                con.Open();
    
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // Process error
            }
            finally
            {
                // Close connection if open
    
                try { if (con != null) con.Close(); }
                catch (Exception ex)
                {
                    // Process error
                }
            }
        }
     
    Monday, December 11, 2006 10:17 PM
  • User-1709593219 posted
    Thanks
    Monday, December 18, 2006 9:38 AM