Visual Studio 2012和Microsoft Access在现有数据库中创建新表 [英] Visual Studio 2012 and Microsoft Access create new table in existing database

查看:108
本文介绍了Visual Studio 2012和Microsoft Access在现有数据库中创建新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个数据库,其中一个表保存用户的登录数据。我如何使用注册用户名在其用户名下自动创建一个存储其他信息的新表? 

I currently have a database with one table that holds a user's login data. How would I go about using the username from registration to automatically create a new table under their username that stores other information? 

我似乎只能找到有关创建新记录的信息,而不是实际表格本身?

I can only seem to find information about creating new records, not actual tables itself?

干杯

推荐答案

您好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


这篇关于Visual Studio 2012和Microsoft Access在现有数据库中创建新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆