0

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

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

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
4

2 回答 2

5

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

 SELECT TOP 0 * INTO NewTable FROM OriginalTable

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

于 2013-09-18T15:53:32.993 回答
0

我终于能够弄清楚这一点。这是我必须做的。注释块将向您显示表模式中的不同行。Case 语句也尚未完成,但您可以在遇到更多需要转换的数据类型时添加此语句。

Imports System.Data.OleDb

Module prm01_up
    Dim sConn As New OleDbConnection("Provider=SQLNCLI10;Server=;Database=;Uid=;Pwd=;")

Sub Main()
    Dim foxTables() As String = {"dwprm01", "lkpbrnch", "add_me", "empmastr"}

    For Each tableName As String In foxTables
        seekAndCreate(tableName)
    Next

    Console.WriteLine()
    Console.WriteLine("Press any key to continue...")
    Console.ReadKey()
End Sub

Private Sub seekAndCreate(ByRef tableName As String)
    Try
        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 foxDs As New DataSet
            Dim fSQL As String = "USE " & tableName
            Dim fCmd As New OleDbCommand(fSQL, foxConn)

            foxConn.Open()

            Dim objDR As OleDbDataReader
            objDR = fCmd.ExecuteReader(CommandBehavior.CloseConnection)

            Dim schemaTable = objDR.GetSchemaTable()
            Dim colName As String = String.Empty
            Dim colSize As String = String.Empty
            Dim colDataType As String = String.Empty
            Dim newDataType As String = String.Empty
            Dim allColumns As String = String.Empty
            Dim colPrecision As String = String.Empty
            Dim colScale As String = String.Empty
            Dim createTable As New OleDbCommand

            'For Each x As DataRow In schemaTable.Rows
            '    For Each y As DataColumn In schemaTable.Columns
            '        Console.WriteLine(y.ColumnName)
            '    Next
            '    Console.WriteLine()
            'Next

            For Each myField As DataRow In schemaTable.Rows
                colName = myField(0).ToString
                colSize = myField(2).ToString
                colDataType = myField(5).ToString
                colPrecision = myField(3).ToString
                colScale = myField(4).ToString

                Select Case colDataType
                    Case "System.String"
                        newDataType = "varchar" & "(" & colSize & "), "
                    Case "System.Decimal"
                        newDataType = "numeric(" & colPrecision & ", " & colScale & "), "
                    Case "System.DateTime"
                        newDataType = "datetime, "
                    Case "System.Int32"
                        newDataType = "int,"
                    Case Else
                        newDataType = colDataType.ToString()
                End Select
                allColumns += "[" & colName & "]" & " " & newDataType
            Next
            Console.WriteLine(allColumns.Substring(0, allColumns.Length - 2))

            createTable.Connection = sConn
            createTable.CommandType = CommandType.Text
            createTable.CommandText = "CREATE TABLE " & tableName & " (" & allColumns & ")"
            createTable.ExecuteNonQuery()
            foxConn.Close()
        Else
            'Table exists
            tableExists = True
            Console.WriteLine(tableName & " exists")
            Console.WriteLine()
        End If

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

    Catch ex As Exception
        Console.WriteLine(ex.ToString())
    End Try
End Sub
End Module
于 2013-09-19T18:45:51.637 回答