2014-11-06 59 views
0

無論如何,我們可以從表拉SQL數據庫模式嗎?如何使用數據讀取器拉SQL模式

以下是我的代碼,但now​​ay我可以拉整個模式一樣

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() 
+0

您可以使用['SqlDataReader.GetSchemaTable'(http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getschematable(v = VS.90)的.aspx)。 – 2014-11-06 09:18:48

+0

readerX.GetSchemaTable()我使用這一行,但它僅返回表名 – Kirk 2014-11-06 09:19:46

+0

,因爲您已經使用了'SELECT name FROM sys.tables',但是您必須從'sys返回的真正表中選擇*'。表「代替。 – 2014-11-06 09:21:12

回答

0

獲取從數據庫架構信息與模式發現的過程來完成。

模式發現允許應用程序請求託管提供程序查找並返回有關給定數據庫的數據庫模式(也稱爲元數據)的信息。

不同的數據庫模式元素(如表,列和存儲過程)通過模式集合公開。

每個模式集合都包含特定於所用提供程序的各種模式信息。

每個.NET Framework託管提供程序都在Connection類中實現GetSchema方法,並且從GetSchema方法返回的模式信息以DataTable的形式出現。

GetSchema方法是一種重載方法,它提供了可選參數,用於指定要返回的模式集合並限制返回的信息量。

更多信息:MSDN LINK

例子:

Imports System.Data.SqlClient 

Module Module1 
    Sub Main() 
     Dim connectionString As String = GetConnectionString() 
     Using connection As New SqlConnection(connectionString) 
     'Connect to the database then retrieve the schema information. 
     connection.Open() 
     Dim table As DataTable = connection.GetSchema("Tables") 

     ' Display the contents of the table. 
     DisplayData(table) 
     Console.WriteLine("Press any key to continue.") 
     Console.ReadKey() 
     End Using 
    End Sub 

    Private Function GetConnectionString() As String 
     ' To avoid storing the connection string in your code, 
     ' you can retrieve it from a configuration file. 
     Return "Data Source=(local);Database=AdventureWorks;" _ 
     & "Integrated Security=true;" 
    End Function 

    Private Sub DisplayData(ByVal table As DataTable) 
     For Each row As DataRow In table.Rows 
     For Each col As DataColumn In table.Columns 
      Console.WriteLine("{0} = {1}", col.ColumnName, row(col)) 
     Next 
     Console.WriteLine("============================") 
     Next 
    End Sub 
End Module 
+0

讓我試一試,謝謝你的幫助 – Kirk 2014-11-06 20:34:53

1

這裏是我已經從頭開始編寫工作方法,所以它不是真正的考驗。它使用DataReader.GetSchemaTable()列出每個表中的所有列:

您可以使用這些類映射架構的信息,您可以添加更多的根據this list

Public Class Table 
    Public Property DatabaseName As String 
    Public Property TableName As String 
    Public Property Schema As String 
    Public Property FullName As String 
    Public Property AllColumns As New List(Of TableColumn) 

    Public Overrides Function ToString() As String 
     Return FullName 
    End Function 
End Class 

Public Class TableColumn 
    Public Property ColumnName As String 
    Public Property DataType As Type 
    Public Property Size As Int32 
    Public Property ColumnOrdinal As Int32 
    Public Property AllowDBNull As Boolean 
    Public Property IsAutoIncrement As Boolean 

    Public Overrides Function ToString() As String 
     Return String.Format("{0}({1})", ColumnName, DataType.ToString()) 
    End Function 
End Class 

此代碼讀取所有表及其所有列到一個列表:

Dim allTables As New List(Of Table) 

Using con As New SqlConnection(My.Settings.RM2ConnectionString) ' use your connection-string ' 
    Using sysTblCommand As New SqlCommand("SELECT [Database]=DB_NAME(DB_ID()),FullName='['+SCHEMA_NAME(schema_id)+'].['+name+']',[Schema]=SCHEMA_NAME(schema_id),Name FROM sys.tables ORDER BY schema_id,name", con) 
     con.Open() 
     Using readerSys = sysTblCommand.ExecuteReader() 
      While readerSys.Read() 
       Dim table As New Table() 
       table.DatabaseName = readerSys.GetString(0) 
       table.FullName = readerSys.GetString(1) 
       table.Schema = readerSys.GetString(2) 
       table.TableName = readerSys.GetString(3) 
       allTables.Add(table) 
      End While 
     End Using 
     For Each table In allTables 
      Using tblCommand As New SqlCommand("SELECT * FROM " & table.ToString(), con) 
       tblCommand.CommandTimeout = 0 
       Using rd = tblCommand.ExecuteReader() 
        Dim schemaTable As DataTable = rd.GetSchemaTable() 
        For Each row As DataRow In schemaTable.Rows 
         Dim col As New TableColumn() 
         col.ColumnName = row.Field(Of String)("ColumnName") 
         col.DataType = row.Field(Of Type)("DataType") 
         col.Size = row.Field(Of Int32)("ColumnSize") 
         col.ColumnOrdinal = row.Field(Of Int32)("ColumnOrdinal") 
         col.AllowDBNull = row.Field(Of Boolean)("AllowDBNull") 
         col.IsAutoIncrement = row.Field(Of Boolean)("IsAutoIncrement") 
         table.AllColumns.Add(col) 
        Next 
       End Using 
      End Using 
     Next 
    End Using 
End Using 
+0

讓我試試,謝謝你的幫助 – Kirk 2014-11-06 20:34:35

+0

我在試這個代碼,但是AllTables.Add(table)是什麼?數據集還是什麼?我找不到 – Kirk 2014-11-08 06:31:50

+0

@kirk:對不起,我沒有複製粘貼它,它是一個List(Of Table)。我已經將它添加到了現在。 – 2014-11-08 07:15:35