2014-11-06 65 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