S
Scott Baxter
Hello,
In the new vb.net is a thing called dataset, which I guess is a standalone
database you can create and play with.
The following code works fine for that.
But I can't seem to define things so that it would work against a real
database.
Here's code that creates an access database, with no structure or fields on
the disk:
If CreateAccessDatabase("e:\test2.mdb") = True Then
MsgBox("Database Created")
Else
MsgBox("Database Creation Failed")
End If
Public Function CreateAccessDatabase( _
ByVal DatabaseFullPath As String) As Boolean
Dim bAns As Boolean
Dim cat As New ADOX.Catalog()
Try
'Make sure the folder
'provided in the path exists. If file name w/o path
'is specified, the database will be created in your
'application folder.
Dim sCreateString As String
sCreateString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DatabaseFullPath
cat.Create(sCreateString)
bAns = True
Catch Excep As System.Runtime.InteropServices.COMException
bAns = False
'do whatever else you need to do here, log,
'msgbox etc.
Finally
cat = Nothing
End Try
Return bAns
End Function
I want to use code like the code below to add the fields to the database.
Is there a way to do this?
Thanks.
Scott
Public Sub createdataset()
'Dim ds As New DataSet()
' Create a table; set its initial capacity.
Dim dtEmp As New DataTable("Employees")
dtEmp.MinimumCapacity = 200
' Create all columns.
' You can create a DataColumn and then add it to the Columns
collection.
Dim dcFName As New DataColumn("FirstName", GetType(String))
dtEmp.Columns.Add(dcFName)
' Or you can create an implicit DataColumn with the Columns.Add
method.
dtEmp.Columns.Add("LastName", GetType(String))
dtEmp.Columns.Add("BirthDate", GetType(Date))
' When you have to set additional properties, you can use an
explicit
' DataColumn object, or you can use a With block.
With dtEmp.Columns.Add("HomeAddress", GetType(String))
.MaxLength = 100
End With
' (When you must set only one property, you can be more concise,
' even though the result isn't very readable.)
dtEmp.Columns.Add("City", GetType(String)).MaxLength = 20
' Create a calculated column by setting the Expression
' property or passing it as the third argument to the Add method.
dtEmp.Columns.Add("CompleteName", GetType(String), _
"FirstName + ' ' + LastName")
' Create an identity, auto-incremented column.
Dim dcEmpId As New DataColumn("EmpId", GetType(Integer))
dcEmpId.AutoIncrement = True ' Make it auto-increment.
dcEmpId.AutoIncrementSeed = 1
dcEmpId.AllowDBNull = False ' Default is True.
dcEmpId.Unique = True ' All key columns should be unique.
dtEmp.Columns.Add(dcEmpId) ' Add to Columns collection.
' Make it the primary key. (Create the array on-the-fly.)
dtEmp.PrimaryKey = New DataColumn() {dcEmpId}
' This is a foreign key, but we haven't created the other table yet.
dtEmp.Columns.Add("DeptId", GetType(Integer))
' Add the DataTable to the DataSet.
ds.Tables.Add(dtEmp)
End Subl
In the new vb.net is a thing called dataset, which I guess is a standalone
database you can create and play with.
The following code works fine for that.
But I can't seem to define things so that it would work against a real
database.
Here's code that creates an access database, with no structure or fields on
the disk:
If CreateAccessDatabase("e:\test2.mdb") = True Then
MsgBox("Database Created")
Else
MsgBox("Database Creation Failed")
End If
Public Function CreateAccessDatabase( _
ByVal DatabaseFullPath As String) As Boolean
Dim bAns As Boolean
Dim cat As New ADOX.Catalog()
Try
'Make sure the folder
'provided in the path exists. If file name w/o path
'is specified, the database will be created in your
'application folder.
Dim sCreateString As String
sCreateString = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
DatabaseFullPath
cat.Create(sCreateString)
bAns = True
Catch Excep As System.Runtime.InteropServices.COMException
bAns = False
'do whatever else you need to do here, log,
'msgbox etc.
Finally
cat = Nothing
End Try
Return bAns
End Function
I want to use code like the code below to add the fields to the database.
Is there a way to do this?
Thanks.
Scott
Public Sub createdataset()
'Dim ds As New DataSet()
' Create a table; set its initial capacity.
Dim dtEmp As New DataTable("Employees")
dtEmp.MinimumCapacity = 200
' Create all columns.
' You can create a DataColumn and then add it to the Columns
collection.
Dim dcFName As New DataColumn("FirstName", GetType(String))
dtEmp.Columns.Add(dcFName)
' Or you can create an implicit DataColumn with the Columns.Add
method.
dtEmp.Columns.Add("LastName", GetType(String))
dtEmp.Columns.Add("BirthDate", GetType(Date))
' When you have to set additional properties, you can use an
explicit
' DataColumn object, or you can use a With block.
With dtEmp.Columns.Add("HomeAddress", GetType(String))
.MaxLength = 100
End With
' (When you must set only one property, you can be more concise,
' even though the result isn't very readable.)
dtEmp.Columns.Add("City", GetType(String)).MaxLength = 20
' Create a calculated column by setting the Expression
' property or passing it as the third argument to the Add method.
dtEmp.Columns.Add("CompleteName", GetType(String), _
"FirstName + ' ' + LastName")
' Create an identity, auto-incremented column.
Dim dcEmpId As New DataColumn("EmpId", GetType(Integer))
dcEmpId.AutoIncrement = True ' Make it auto-increment.
dcEmpId.AutoIncrementSeed = 1
dcEmpId.AllowDBNull = False ' Default is True.
dcEmpId.Unique = True ' All key columns should be unique.
dtEmp.Columns.Add(dcEmpId) ' Add to Columns collection.
' Make it the primary key. (Create the array on-the-fly.)
dtEmp.PrimaryKey = New DataColumn() {dcEmpId}
' This is a foreign key, but we haven't created the other table yet.
dtEmp.Columns.Add("DeptId", GetType(Integer))
' Add the DataTable to the DataSet.
ds.Tables.Add(dtEmp)
End Subl