使用另一个表的架构在 SQL Server 上创建新表

HKI*_*act 0 vb.net dbase

我在 VB.net 中有一个程序,运行时确定表是否存在。如果它不存在,我想在 SQL Server 上创建一个与本地 FoxPro 表具有相同架构的表。这是可以做的事情吗?

这是我到目前为止所拥有的。现在它只是从 Visual Foxpro 表中获取架构并显示它。不知道从这里去哪里。有任何想法吗?

Private Sub dwprm01()
    Try
        Dim tableName As String = "dwprm01"
        Dim tableExists As Boolean = False
        Dim foxConn As New OleDbConnection("Provider=vfpoledb.1;Data Source=Z:\update_dwprm01\" & tableName & ".DBF;Collating Sequence=general;")

        sConn.Open()
        Dim restrictions(3) As String
        restrictions(2) = tableName
        Dim dbTbl As DataTable = sConn.GetSchema("Tables", restrictions)

        Console.WriteLine("Checking if " & tableName & " exists")

        If dbTbl.Rows.Count = 0 Then
            'Table does not exist
            tableExists = False

            Console.WriteLine(tableName & " does not exist")
            Console.WriteLine()
            Console.WriteLine("Creating " & tableName)

            Dim fSQL = "SELECT * FROM " & tableName
            Dim cmd As New OleDbCommand(fSQL, foxConn)

            foxConn.Open()

            Dim myReader As OleDbDataReader = cmd.ExecuteReader()
            Dim schema As DataTable = myReader.GetSchemaTable()

            For Each row As DataRow In schema.Rows
                For Each col As DataColumn In schema.Columns
                    Console.WriteLine(col.ColumnName & " = " & row(col).ToString())
                Next
            Next
            myReader.Close()
            foxConn.Close()
        Else
            'Table exists
            tableExists = True
            Console.WriteLine(tableName & " exists")
        End If

        dbTbl.Dispose()
        sConn.Close()
        sConn.Dispose()

    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

Joe*_*orn 5

由于您已经验证了新表确实存在,因此您只想执行如下查询:

 SELECT TOP 0 * INTO NewTable FROM OriginalTable
Run Code Online (Sandbox Code Playgroud)

这将创建一个新表,其中没有任何行的结构与原始表相匹配。