無論如何,我們可以從表拉SQL數據庫模式嗎?如何使用數據讀取器拉SQL模式
以下是我的代碼,但noway我可以拉整個模式一樣
Create Table Employee(code int, name blah blah...)
Dim sqlQuery As String
Dim textInsertQueryLine As String
Dim tableSchema As DataTable
Dim tableField As DataRow
Dim tableProperty As DataColumn
Dim SourceConn As New SqlConnection(sourceDBPath)
Dim DestinationConn As New SqlConnection(destinationDBPath)
SourceConn.Open()
DestinationConn.Open()
sqlQuery = "SELECT name FROM sys.tables"
Dim cmdX As New SqlCommand(sqlQuery, SourceConn)
Dim readerX As SqlDataReader = cmdX.ExecuteReader
Do While readerX.Read
Dim cmdY As New SqlCommand(sqlQuery, DestinationConn)
Dim readerY As SqlDataReader = cmdY.ExecuteReader
Do While readerY.Read
If readerX.GetString(0) = readerY.GetString(0) Then
txtConsoleView.AppendText(readerX.GetString(0) + "Matched. " + vbCrLf)
Else
tableSchema = readerX.GetSchemaTable()
txtConsoleView.AppendText(tableSchema.ToString + vbCrLf)
End If
Loop
readerY.Close()
Loop
readerX.Close()
SourceConn.Close()
DestinationConn.Close()
您可以使用['SqlDataReader.GetSchemaTable'(http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable(v = VS.90)的.aspx)。 – 2014-11-06 09:18:48
readerX.GetSchemaTable()我使用這一行,但它僅返回表名 – Kirk 2014-11-06 09:19:46
,因爲您已經使用了'SELECT name FROM sys.tables',但是您必須從'sys返回的真正表中選擇*'。表「代替。 – 2014-11-06 09:21:12