更新时间:2023-12-01 10:05:28
您好Azuraxe,
Hi Azuraxe,
根据您的描述,您想在现有数据库中创建DataTable,请参阅下面的代码。
Based on your description, you want to create DataTable in the existing database, please refer to the code below.
Dim tablename As String = "Test5"
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "D:\Database\test2.accdb"
connString = provider & dataFile
Dim con As New OleDbConnection(connString)
con.Open()
Dim dbSchema As DataTable = con.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, tableName, "TABLE"})
If dbSchema.Rows.Count > 0 Then
MessageBox.Show("The Table has been existed!")
' do whatever you want to do if the table exists
Else
Dim cmd As New OleDbCommand()
cmd.Connection = con
cmd.CommandText = "CREATE TABLE " + tablename + " (ID COUNTER, [Year] INTEGER, FName TEXT)"
Try
cmd.ExecuteNonQuery()
Console.WriteLine("Table created.")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End If
con.Close()
***的问候,
Cherry