none
Add Column to Access Database Using Visual Basic 2008 RRS feed

  • Question

  • How can I add a column to a Access Database using code.  I have no problems adding rows and changing the data contained in the database but I can not add a column.  No error is given, the column simply is not added.

    Private m_cnADONetConnection As New OleDb.OleDbConnection
        Private m_daDataAdapter As OleDb.OleDbDataAdapter
        Private m_cbCommandBuilder As OleDb.OleDbCommandBuilder
        Private m_dtcontacts As New DataTable
    
    Private Sub Database_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            m_cnADONetConnection.ConnectionString = _
                "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\hinshahun\My Documents\Visual Studio 2008\Projects\Database\Contacts.mdb"
            m_cnADONetConnection.Open()
            m_daDataAdapter = New OleDb.OleDbDataAdapter("Select * From COntacts", m_cnADONetConnection)
            m_cbCommandBuilder = New OleDb.OleDbCommandBuilder(m_daDataAdapter)
            m_daDataAdapter.Fill(m_dtcontacts)
        End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim clmn As DataColumn
            clmn = New DataColumn("SaleAmount", System.Type.GetType("System.Decimal"))
            clmn.AllowDBNull = True
            m_dtcontacts.Columns.Add(clmn)
            m_dtcontacts.AcceptChanges()
            m_daDataAdapter.Update(m_dtcontacts)
            
        End Sub
     
    Tuesday, October 13, 2009 5:11 PM

Answers