VB.NET Create MS-Access database and table 01-02-2011, 07:08 AM
#1
Please note that the methods used to create the database will work in VS2005 but the layout of the attached project is VS2008 so if you are using a version less than VS2008 you will need to hack it up (excuse the pun).
The attached project done in VS2008, VB.NET demonstrates how to create a new Microsoft Access database using ADOX.
Figure 1 shows the creation of a table with an auto-incrementing primary key and two additional fields with a constraint on the primary key. Figure 2 shows a row added to the newly added table
One of the best things in working with data is the BindingSource class. With that said a extremely super example is shown in figure 3 which filters the underlying data source, a DataTable just created.
Caveat: I generally do not create Microsoft Access files this was as it is easier to simply include a blank Access database with my project and copy it to a working folder then add tables as needed but I know some programmers rather create them on the fly which is why I am presenting this here.
Figure 1
Figure 2
Figure 3
The attached project done in VS2008, VB.NET demonstrates how to create a new Microsoft Access database using ADOX.
Figure 1 shows the creation of a table with an auto-incrementing primary key and two additional fields with a constraint on the primary key. Figure 2 shows a row added to the newly added table
One of the best things in working with data is the BindingSource class. With that said a extremely super example is shown in figure 3 which filters the underlying data source, a DataTable just created.
Caveat: I generally do not create Microsoft Access files this was as it is easier to simply include a blank Access database with my project and copy it to a working folder then add tables as needed but I know some programmers rather create them on the fly which is why I am presenting this here.
Figure 1
Code:
Dim cmd As New OleDb.OleDbCommand( _
<text>
CREATE TABLE persons ([Identifier] int identity ,
UserName NVarchar(50),
[Password] NVarchar(50),
CONSTRAINT [pk_AutoId] PRIMARY KEY ([Identifier]))
</text>.Value, cn)Figure 2
Code:
cmd.CommandText = _
<Text>
INSERT INTO persons
(UserName,[Password])
VALUES
("Gallagher","pass1word")
</Text>.Value
cmd.ExecuteNonQuery()Figure 3
Code:
Private Sub cmdFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFilter.Click
If bsDataSource.DataSource IsNot Nothing Then
If bsDataSource.Filter = "" Then
bsDataSource.Filter = "UserName like 'G%'"
cmdFilter.Text = "Remove Filter"
Else
bsDataSource.Filter = ""
cmdFilter.Text = "Set Filter"
End If
End If
End Sub
Conflict is inevitable, but combat is optional
My humble self-defense site
Use Option Strict = On when coding with VB.NET
My humble self-defense site
Use Option Strict = On when coding with VB.NET
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)