convert this VB code to C# help?

I am trying to create and populate a new access database. I found that I needed to include the reference to ADO extended in the com references. I includes the using ADOX; and the following if statement will successfully create the database.

if (!File.Exists("C:\\AccessDB\\NewMDB.mdb"))

{

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\AccessDB\\NewMDB.mdb;" + "Jet OLEDB:Engine Type=5");

MessageBox.Show("Database Created Successfully");

}

But I don't know how to add tables to this database. I found the following VB code and thought it would be easy enough to convert over but ran into some problems. Like the Columns append function requires a DataTypeEnum and I can't figure out how to create it. It also needed a columns object I specified the name but is there anything else I need to do with it If someone could just show me how you could do the same thing in c# that would be great thanks.

http://msdn.microsoft.com/library/default.asp url=/library/en-us/ado270/htm/admsctablecreationexample.asp

' BeginCreateTableVB
Sub Main()
On Error GoTo CreateTableError

Dim tbl As New Table
Dim cat As New ADOX.Catalog

' Open the Catalog.
cat.ActiveConnection = "Provider='Microsoft.Jet.OLEDB.4.0';" & _
"Data Source='Northwind.mdb';"

tbl.Name = "MyTable"
tbl.Columns.Append "Column1", adInteger
tbl.Columns.Append "Column2", adInteger
tbl.Columns.Append "Column3", adVarWChar, 50
cat.Tables.Append tbl
Debug.Print "Table 'MyTable' is added."

'Delete the table as this is a demonstration.
cat.Tables.Delete tbl.Name
Debug.Print "Table 'MyTable' is deleted."

'Clean up
Set cat.ActiveConnection = Nothing
Set cat = Nothing
Set tbl = Nothing
Exit Sub

CreateTableError:

Set cat = Nothing
Set tbl = Nothing

If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
' EndCreateTableVB



Answer this question

convert this VB code to C# help?

  • ytandeta

    You could try creating the tables using SQL statements

    Create table Users ...

    This would give you a number of benefits, such as ease of manitenance and less code. Is there any particular reason why you want to do it this way


  • Sugan_Dave

    No no particular reason. I have limited to no experiance in working with databases so I was just trying to get something to accomplish what I need to do and that is create an access database and populate it with data.
  • convert this VB code to C# help?