SQL Server資料庫中所有表及欄位資訊

來源:互聯網
上載者:User
程式思想:用SELECT name From sysobjects WHERE xtype = 'u'得到所有表,然後迴圈開啟表,根據Rs_Colums.Fields(I).Name 得到欄位名,FieldType(Rs_Colums.Fields(I).Type) 得到欄位類型,Rs_Colums.Fields(I).DefinedSize '寬度

  由於Rs_Colums.Fields(I).Type傳回型別是數字,程式中寫了一個FieldType函數轉化成中文類型

  Private Sub Command1_Click()

  Dim Cn As New ADODB.Connection

  Dim Rs_Table As New ADODB.Recordset

  Dim Rs_Colums As New ADODB.Recordset

  With Cn '定義串連

  .CursorLocation = adUseClient

  .Provider = "sqloledb"

  .Properties("Data Source").Value = "LIHG"

  .Properties("Initial Catalog").Value = "NorthWind"

  .Properties("User ID") = "sa"

  .Properties("Password") = "sa"

  .Properties("prompt") = adPromptNever

  .ConnectionTimeout = 15

  .Open

  If .State = adStateOpen Then

  Rs_Table.CursorLocation = adUseClient '得到所有表名

  Rs_Table.Open "SELECT name From sysobjects WHERE xtype = 'u'", Cn, adOpenDynamic, adLockReadOnly

  Rs_Table.MoveFirst

  Do While Not Rs_Table.EOF

  Debug.Print Rs_Table.Fields("name")

  Rs_Colums.CursorLocation = adUseClient

  Rs_Colums.Open "select top 1 * from [" & Rs_Table.Fields("name") & "]", Cn, adOpenStatic, adLockReadOnly

  For I = 0 To Rs_Colums.Fields.Count - 1 ' 迴圈所有列

  Debug.Print Rs_Colums.Fields(I).Name '欄位名

  Debug.Print FieldType(Rs_Colums.Fields(I).Type) '欄位類型

  Debug.Print Rs_Colums.Fields(I).DefinedSize '寬度

  Next

  Rs_Colums.Close

  Rs_Table.MoveNext

  Loop

  Rs_Table.Close

  Set Rs_Colums = Nothing

  Set Rs_Table = Nothing

  Else

  MsgBox "資料庫連接失敗,請找系統管理員進行檢查 !", 16, cProgramName

  End

  End If

  End With

  End Sub

  '*********************************************************

  '* 名稱:FieldType

  '* 功能:返回欄位類型

  '* 用法:FieldType(nType as integer)

  '*********************************************************

  Function FieldType(nType As Integer) As String

  Select Case nType

  Case 128

  FieldType = "BINARY"

  Case 11

  FieldType = "BIT"

  Case 129

  FieldType = "CHAR"

  Case 135

  FieldType = "DATETIME"

  Case 131

  FieldType = "DECIMAL"

  Case 5

  FieldType = "FLOAT"

  Case 205

  FieldType = "IMAGE"

  Case 3

  FieldType = "INT"

  Case 6

  FieldType = "MONEY"

  Case 130

  FieldType = "NCHAR"

  Case 203

  FieldType = "NTEXT"

  Case 131

  FieldType = "NUMERIC"

  Case 202

  FieldType = "NVARCHAR"

  Case 4

  FieldType = "REAL"

  Case 135

  FieldType = "SMALLDATETIME"

  Case 2

  FieldType = "SMALLMONEY"

  Case 6

  FieldType = "TEXT"

  Case 201

  FieldType = "TIMESTAMP"

  Case 128

  FieldType = "TINYINT"

  Case 17

  FieldType = "UNIQUEIDENTIFIER"

  Case 72

  FieldType = "VARBINARY"

  Case 204

  FieldType = "VARCHAR"

  Case 200

  FieldType = ""

  End Select

  End Function

  此程式只是一個雛形,可以在此基礎上開發成一個工具使用

  本程式在:VB 6.0 ,SQL SERVER 2000下運行通過

  注程式中須引用ActiveX Data Objects (ADO)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.