我在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
答案 0 :(得分:4)
由于您已经验证了新表确实存在,您只想执行如下查询:
SELECT TOP 0 * INTO NewTable FROM OriginalTable
这将创建一个没有行结构与原始行匹配的新表。
答案 1 :(得分: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