Visual Basic數據庫開發疑難問題解

問:如何顯示格式爲03-3-13的日期?

解決的方法:

1

Cmd.CommandText = "select * from 支出 where 日期=03-3-13" 中 03-3-13=-13。

日期實際上是Double型數字。0 是 1899-12-30,-13 是 1899-12-17。你當然沒有這樣日期的記錄,所以只有大於才行。

2

Cmd.CommandText = "select * from 支出 where 日期=#03-3-13#"

凡是沒有明示,文字型日期是按美國習慣解釋的,#03-3-13# 是 0013-03-03。

或者使用長日期格式:

Cmd.CommandText = "select * from 支出 where 日期=#2003-3-13#"

用格式化函數:

Cmd.CommandText = "select * from 支出 where 日期=#" & format(mydate,"yyyy-mm-dd") & "#"

問: 如何判斷DNS是否存在?怎樣才能列舉出所有的DNS?

解決方法:

1、通過利用ODBC API中的SQLDataSource函數可以取得ODBC API中數據源的列表。 判斷DNS是否存在:

2、使用API函數Private Declare Function SQLDataSources Lib "ODBC32.DLL" (ByVal henv As Long, ByVal fDirection As Integer, ByVal szDSN As String, ByVal cbDSNMax As Integer, pcbDSN As Integer, ByVal szDescription As String, ByVal cbDescriptionMax As Integer, pcbDescription As Integer) As Integer Private Declare Function SQLAllocEnv Lib "ODBC32.DLL" (ByRef env As Long) As Long 列舉出所有DNS。

問:處理文本文件是導入數據庫還是直接讀寫文件呢?

解決方法:

Set main = bumony.OpenRecordset("main")
Open App.Path & "/sources/" & Text1.Text & "/′úàíòμ??" & Text1.Text & ".txt" For Input As #1
Do While Not EOF(1)
Line Input #1, str1
With main
.AddNew
!code = Mid(str1, 1, 5)
!date = Text1.Text
If Mid(str1, 1, 5) = "21310" Or Mid(str1, 1, 5) = "21311" Or Mid(str1, 1, 5) = "21410" Or Mid(str1, 1, 5) = "21411" Then
!Money = Trim(Mid(str1, 7, 10))
Else
!Money = Trim(Mid(str1, 7, 10)) & "0000"
End If
!whao = "1102"
!ywhao = "1102"
.Update
End With
Loop
Close #1
main.Close

問:調用SQL存儲後有參數返回,應該怎麼賦值?

解決方法:

Dim ADOCmd As New ADODB.Command
Dim ADOPrm As New ADODB.Parameter
Dim ADORs As ADODB.Recordset
'....
Set ADOCmd.ActiveConnection = ADOCon
With ADOCmd
.CommandType = adCmdStoredProc
.CommandText = "ADOTestRPE"
End With
sParmName = "Output"
Set ADOPrm = ADOCmd.CreateParameter(sParmName, adInteger, adParamOutput)
ADOCmd.Parameters.Append ADOPrm
ADOCmd.Parameters(sParmName).Value = 999
Set ADORs = ADOCmd.Execute
'.....
Debug.Print "Output: " & ADOCmd.Parameters("Output").Value

問: SQL Server 2000中如何存取圖片信息?

解決方法:

新建一個工程,添加 ado 控件,2個 Command ,1個 Picture,1個 Image

Dim Chunk() As Byte
Dim lngLengh As Long
Dim intChunks As Integer
Dim intFragment As Integer
Const ChunkSize = 1000
Const lngDataFile = 1
Private Sub cmdBrowse_Click()
On Error Resume Next
With cmdlFilePath
.Filter = "JPG Files|*.JPG|Bitmaps|*.BMP"
.ShowOpen
txtFilePath.Text = .filename
End With
End Sub
Private Sub Savepic()
Open "c:/colordraw0094_m.jpg" For Binary Access Read As lngDataFile
lngLengh = LOF(lngDataFile)
If lngLengh = 0 Then Close lngDatafile: Exit Sub
intChunks = lngLengh / ChunkSize
intFragment = lngLengh Mod ChunkSize
'OpenData 打開數據庫
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ As String
If rs.State = adStateOpen Then rs.Close
strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic
On Error Resume Next
rs.AddNew
ReDim Chunk(intFragment)
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For i = 1 To intChunks
Get lngDataFile, , Chunk()
rs.Fields("rs_photo1").AppendChunk Chunk()
Next i
rs.Update
rs.Close
Close lngDataFile
Call ShowPic
End Sub
Public Sub ShowPic()
'OpenData 打開數據庫
Dim i As Integer
Dim rs As New ADODB.Recordset
Dim strQ, filename As String
If rs.State = adStateOpen Then rs.Close
strQ = "Select * From [mydata]"
rs.Open strQ, conn, adOpenStatic, adLockOptimistic
If rs.EOF <> True Then
rs.MoveLast
Else
Exit Sub
End If
On Error Resume Next
Open "pictemp" For Binary Access Write As lngDataFile
lngLengh = rs.Fields("rs_photo1").ActualSize
intChunks = lngLengh / ChunkSize
intFragment = lngLengh Mod ChunkSize
ReDim Chunk(intFragment)
Chunk() = rs.Fields("rs_photo1").GetChunk(intFragment)
Put lngDataFile, , Chunk()
For i = 1 To intChunks
ReDim Buffer(ChunkSize)
Chunk() = rs.Fields("rs_photo1").GetChunk(ChunkSize)
Put lngDataFile, , Chunk()
Next i
Close lngDataFile
filename = "pictemp"
Picture1.Picture = LoadPicture(filename)
Image1.Stretch = True
Image1.Picture = Picture1.Picture
Kill filename
End Sub
Private Sub Command1_Click()
Savepic
End Sub
Private Sub Command2_Click()
ShowPic
End Sub

問:如何在VB裏做SQL的數據庫的備份和恢復?

解決方法:

Option Explicit
Private Const cnstr As String = "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;" _
& "User ID=*****;Initial Catalog=master;Data Source=******"
Private DBCn As ADODB.Connection
Private Sub cmdBackup_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "backup database pubs to disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub
Private Sub cmdRestore_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "restore database pubs from disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub

問:如何通過ADO獲得數據庫的字段名,字段類型?
解決方法:
名: recordset.fields(index).name
類型:recordset.fields(index).type

類型是用數字表示的,msdn上有對應表

---------------------------------------------------------------

OpenSchema 方法

從提供者獲取數據庫模式信息。

語法

Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)

返回值

返回包含模式信息的 Recordset 對象。Recordset 將以只讀、靜態遊標打開。

參數

QueryType 所要運行的模式查詢類型,可以爲下列任意常量。

Criteria 可選。每個 QueryType 選項的查詢限制條件數組,如下所列:

QueryType 值 Criteria 值

AdSchemaAsserts CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCatalogs CATALOG_NAME
AdSchemaCharacterSets CHARACTER_SET_CATALOG
CHARACTER_SET_SCHEMA
CHARACTER_SET_NAME
AdSchemaCheckConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCollations COLLATION_CATALOG
COLLATION_SCHEMA
COLLATION_NAME
AdSchemaColumnDomainUsage DOMAIN_CATALOG
DOMAIN_SCHEMA
DOMAIN_NAME
COLUMN_NAME
AdSchemaColumnPrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
GRANTOR
GRANTEE
adSchemaColumns TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintColumnUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintTableUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaForeignKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
FK_TABLE_CATALOG
FK_TABLE_SCHEMA
FK_TABLE_NAME
adSchemaIndexes TABLE_CATALOG
TABLE_SCHEMA
INDEX_NAME
TYPE
TABLE_NAME
adSchemaKeyColumnUsage CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaPrimaryKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
adSchemaProcedureColumns PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
COLUMN_NAME
adSchemaProcedureParameters PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PARAMTER_NAME
adSchemaProcedures PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PROCEDURE_TYPE
adSchemaProviderSpecific 參見說明
adSchemaProviderTypes DATA_TYPE
BEST_MATCH
adSchemaReferentialConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
adSchemaSchemata CATALOG_NAME
SCHEMA_NAME
SCHEMA_OWNER
adSchemaSQLLanguages <無>
adSchemaStatistics TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaTableConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
CONSTRAINT_TYPE
adSchemaTablePrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
GRANTOR
GRANTEE
adSchemaTables TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
adSchemaTranslations TRANSLATION_CATALOG
TRANSLATION_SCHEMA
TRANSLATION_NAME
adSchemaUsagePrivileges OBJECT_CATALOG
OBJECT_SCHEMA
OBJECT_NAME
OBJECT_TYPE
GRANTOR
GRANTEE
adSchemaViewColumnUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViewTableUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViews TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
SchemaID OLE DB 規範沒有定義用於提供者模式查詢的 GUID。如果 QueryType 設置爲 adSchemaProviderSpecific,則需要該參數,否則不使用它。
說明

OpenSchema 方法返回與數據源有關的信息,例如關於服務器上的表以及表中的列等信息。

Criteria 參數是可用於限制模式查詢結果的值數組。每個模式查詢有它支持的不同參數集。實際模式由 IDBSchemaRowset 接口下的 OLE DB 規範定義。ADO 中所支持的參數集已在上面列出。

如果提供者定義未在上面列出的非標準模式查詢,則常量 adSchemaProviderSpecific 將用於 QueryType 參數。在使用該常量時需要 SchemaID 參數傳遞模式查詢的 GUID 以用於執行。如果 QueryType 設置爲 adSchemaProviderSpecific 但是沒有提供 SchemaID,將導致錯誤。

提供者不需要支持所有的 OLE DB 標準模式查詢,只有 adSchemaTables、adSchemaColumns 和 adSchemaProviderTypes 是 OLE DB 規範需要的。但是對於這些模式查詢,提供者不需要支持上面列出的 Criteria 條件約束。

遠程數據服務用法 OpenSchema 方法在客戶端 Connection 對象上無效。

注意 在 Visual Basic 中,在由 Connection 對象的 OpenSchema 方法所返回的 Recordset 中有 4 字節無符號整型 (DBTYPE UI4) 的列無法與其他變量比較。有關 OLE DB 數據類型的詳細信息,請參閱“Microsoft OLE DB 程序員參考”的第十章和附錄 A。

---------------------------------------------------------------

OpenSchema 方法範例

該範例使用 OpenSchema 方法顯示 Pubs 數據庫內每個表的名稱和類型。

Public Sub OpenSchemaX()
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn
Set rstSchema = cnn1.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn1.Close
End Sub
該範例在 OpenSchema 方法的 Criteria 參數中指定 TABLE_TYPE 查詢約束。因此,只返回在 Pubs 數據庫中指定視圖的模式信息。然後該範例顯示每個表的名稱和類型。

Public Sub OpenSchemaX2()
Dim cnn2 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn2 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn2.Open strCnn
Set rstSchema = cnn2.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "VIEW"))
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn2.Close
End Sub
---------------------------------------------------------------

ado的recordset 對象在返回記錄集時,同時也有各字段的名字和類型。

具體是:recordset.Fields.Item(i).Name

recordset.Fields.Item(i).Type

類型是枚舉型,你可以查對應的值

還有其它屬性:

recordset.Fields.Item(i).DefinedSize 字段的定義寬度

recordset.Fields.Item(i).ActualSize 字段的實際寬度

....

---------------------------------------------------------------

其他我不想再說了,看我用過的:

for i=0 to rs.recordcount-1
print rs.Fields(i).Name & " " & rs.Fields(i).Type "(" & rs.Fields(i).ActualSize & ")"
next
---------------------------------------------------------------
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 * 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

問:怎麼把Sqlserver 的數據轉換並輸出成Xml格式文件?

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

Option Explicit
Private Const cnstr As String = "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;" _
& "User ID=*****;Initial Catalog=master;Data Source=******"
Private DBCn As ADODB.Connection
Private Sub cmdBackup_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "backup database pubs to disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub
Private Sub cmdRestore_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "restore database pubs from disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub

問:如何通過ADO獲得數據庫的字段名,字段類型?
解決方法:
名: recordset.fields(index).name
類型:recordset.fields(index).type

類型是用數字表示的,msdn上有對應表

---------------------------------------------------------------

OpenSchema 方法

從提供者獲取數據庫模式信息。

語法

Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)

返回值

返回包含模式信息的 Recordset 對象。Recordset 將以只讀、靜態遊標打開。

參數

QueryType 所要運行的模式查詢類型,可以爲下列任意常量。

Criteria 可選。每個 QueryType 選項的查詢限制條件數組,如下所列:

QueryType 值 Criteria 值

AdSchemaAsserts CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCatalogs CATALOG_NAME
AdSchemaCharacterSets CHARACTER_SET_CATALOG
CHARACTER_SET_SCHEMA
CHARACTER_SET_NAME
AdSchemaCheckConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCollations COLLATION_CATALOG
COLLATION_SCHEMA
COLLATION_NAME
AdSchemaColumnDomainUsage DOMAIN_CATALOG
DOMAIN_SCHEMA
DOMAIN_NAME
COLUMN_NAME
AdSchemaColumnPrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
GRANTOR
GRANTEE
adSchemaColumns TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintColumnUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintTableUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaForeignKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
FK_TABLE_CATALOG
FK_TABLE_SCHEMA
FK_TABLE_NAME
adSchemaIndexes TABLE_CATALOG
TABLE_SCHEMA
INDEX_NAME
TYPE
TABLE_NAME
adSchemaKeyColumnUsage CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaPrimaryKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
adSchemaProcedureColumns PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
COLUMN_NAME
adSchemaProcedureParameters PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PARAMTER_NAME
adSchemaProcedures PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PROCEDURE_TYPE
adSchemaProviderSpecific 參見說明
adSchemaProviderTypes DATA_TYPE
BEST_MATCH
adSchemaReferentialConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
adSchemaSchemata CATALOG_NAME
SCHEMA_NAME
SCHEMA_OWNER
adSchemaSQLLanguages <無>
adSchemaStatistics TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaTableConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
CONSTRAINT_TYPE
adSchemaTablePrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
GRANTOR
GRANTEE
adSchemaTables TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
adSchemaTranslations TRANSLATION_CATALOG
TRANSLATION_SCHEMA
TRANSLATION_NAME
adSchemaUsagePrivileges OBJECT_CATALOG
OBJECT_SCHEMA
OBJECT_NAME
OBJECT_TYPE
GRANTOR
GRANTEE
adSchemaViewColumnUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViewTableUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViews TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
SchemaID OLE DB 規範沒有定義用於提供者模式查詢的 GUID。如果 QueryType 設置爲 adSchemaProviderSpecific,則需要該參數,否則不使用它。
說明

OpenSchema 方法返回與數據源有關的信息,例如關於服務器上的表以及表中的列等信息。

Criteria 參數是可用於限制模式查詢結果的值數組。每個模式查詢有它支持的不同參數集。實際模式由 IDBSchemaRowset 接口下的 OLE DB 規範定義。ADO 中所支持的參數集已在上面列出。

如果提供者定義未在上面列出的非標準模式查詢,則常量 adSchemaProviderSpecific 將用於 QueryType 參數。在使用該常量時需要 SchemaID 參數傳遞模式查詢的 GUID 以用於執行。如果 QueryType 設置爲 adSchemaProviderSpecific 但是沒有提供 SchemaID,將導致錯誤。

提供者不需要支持所有的 OLE DB 標準模式查詢,只有 adSchemaTables、adSchemaColumns 和 adSchemaProviderTypes 是 OLE DB 規範需要的。但是對於這些模式查詢,提供者不需要支持上面列出的 Criteria 條件約束。

遠程數據服務用法 OpenSchema 方法在客戶端 Connection 對象上無效。

注意 在 Visual Basic 中,在由 Connection 對象的 OpenSchema 方法所返回的 Recordset 中有 4 字節無符號整型 (DBTYPE UI4) 的列無法與其他變量比較。有關 OLE DB 數據類型的詳細信息,請參閱“Microsoft OLE DB 程序員參考”的第十章和附錄 A。

---------------------------------------------------------------

OpenSchema 方法範例

該範例使用 OpenSchema 方法顯示 Pubs 數據庫內每個表的名稱和類型。

Public Sub OpenSchemaX()
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn
Set rstSchema = cnn1.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn1.Close
End Sub
該範例在 OpenSchema 方法的 Criteria 參數中指定 TABLE_TYPE 查詢約束。因此,只返回在 Pubs 數據庫中指定視圖的模式信息。然後該範例顯示每個表的名稱和類型。

Public Sub OpenSchemaX2()
Dim cnn2 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn2 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn2.Open strCnn
Set rstSchema = cnn2.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "VIEW"))
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn2.Close
End Sub
---------------------------------------------------------------

ado的recordset 對象在返回記錄集時,同時也有各字段的名字和類型。

具體是:recordset.Fields.Item(i).Name

recordset.Fields.Item(i).Type

類型是枚舉型,你可以查對應的值

還有其它屬性:

recordset.Fields.Item(i).DefinedSize 字段的定義寬度

recordset.Fields.Item(i).ActualSize 字段的實際寬度

....

---------------------------------------------------------------

其他我不想再說了,看我用過的:

for i=0 to rs.recordcount-1
print rs.Fields(i).Name & " " & rs.Fields(i).Type "(" & rs.Fields(i).ActualSize & ")"
next
---------------------------------------------------------------
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 * 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

問:怎麼把Sqlserver 的數據轉換並輸出成Xml格式文件?

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

Option Explicit
Private Const cnstr As String = "Provider=SQLOLEDB.1;Password=****;Persist Security Info=True;" _
& "User ID=*****;Initial Catalog=master;Data Source=******"
Private DBCn As ADODB.Connection
Private Sub cmdBackup_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "backup database pubs to disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub
Private Sub cmdRestore_Click()
Set DBCn = New ADODB.Connection
DBCn.Open cnstr
DBCn.Execute "restore database pubs from disk='d:/pubs_backup.dat'"
DBCn.Close
End Sub

問:如何通過ADO獲得數據庫的字段名,字段類型?
解決方法:
名: recordset.fields(index).name
類型:recordset.fields(index).type

類型是用數字表示的,msdn上有對應表

---------------------------------------------------------------

OpenSchema 方法

從提供者獲取數據庫模式信息。

語法

Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)

返回值

返回包含模式信息的 Recordset 對象。Recordset 將以只讀、靜態遊標打開。

參數

QueryType 所要運行的模式查詢類型,可以爲下列任意常量。

Criteria 可選。每個 QueryType 選項的查詢限制條件數組,如下所列:

QueryType 值 Criteria 值

AdSchemaAsserts CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCatalogs CATALOG_NAME
AdSchemaCharacterSets CHARACTER_SET_CATALOG
CHARACTER_SET_SCHEMA
CHARACTER_SET_NAME
AdSchemaCheckConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
AdSchemaCollations COLLATION_CATALOG
COLLATION_SCHEMA
COLLATION_NAME
AdSchemaColumnDomainUsage DOMAIN_CATALOG
DOMAIN_SCHEMA
DOMAIN_NAME
COLUMN_NAME
AdSchemaColumnPrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
GRANTOR
GRANTEE
adSchemaColumns TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintColumnUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaConstraintTableUsage TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaForeignKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
FK_TABLE_CATALOG
FK_TABLE_SCHEMA
FK_TABLE_NAME
adSchemaIndexes TABLE_CATALOG
TABLE_SCHEMA
INDEX_NAME
TYPE
TABLE_NAME
adSchemaKeyColumnUsage CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaPrimaryKeys PK_TABLE_CATALOG
PK_TABLE_SCHEMA
PK_TABLE_NAME
adSchemaProcedureColumns PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
COLUMN_NAME
adSchemaProcedureParameters PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PARAMTER_NAME
adSchemaProcedures PROCEDURE_CATALOG
PROCEDURE_SCHEMA
PROCEDURE_NAME
PROCEDURE_TYPE
adSchemaProviderSpecific 參見說明
adSchemaProviderTypes DATA_TYPE
BEST_MATCH
adSchemaReferentialConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
adSchemaSchemata CATALOG_NAME
SCHEMA_NAME
SCHEMA_OWNER
adSchemaSQLLanguages <無>
adSchemaStatistics TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
adSchemaTableConstraints CONSTRAINT_CATALOG
CONSTRAINT_SCHEMA
CONSTRAINT_NAME
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
CONSTRAINT_TYPE
adSchemaTablePrivileges TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
GRANTOR
GRANTEE
adSchemaTables TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
adSchemaTranslations TRANSLATION_CATALOG
TRANSLATION_SCHEMA
TRANSLATION_NAME
adSchemaUsagePrivileges OBJECT_CATALOG
OBJECT_SCHEMA
OBJECT_NAME
OBJECT_TYPE
GRANTOR
GRANTEE
adSchemaViewColumnUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViewTableUsage VIEW_CATALOG
VIEW_SCHEMA
VIEW_NAME
adSchemaViews TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
SchemaID OLE DB 規範沒有定義用於提供者模式查詢的 GUID。如果 QueryType 設置爲 adSchemaProviderSpecific,則需要該參數,否則不使用它。
說明

OpenSchema 方法返回與數據源有關的信息,例如關於服務器上的表以及表中的列等信息。

Criteria 參數是可用於限制模式查詢結果的值數組。每個模式查詢有它支持的不同參數集。實際模式由 IDBSchemaRowset 接口下的 OLE DB 規範定義。ADO 中所支持的參數集已在上面列出。

如果提供者定義未在上面列出的非標準模式查詢,則常量 adSchemaProviderSpecific 將用於 QueryType 參數。在使用該常量時需要 SchemaID 參數傳遞模式查詢的 GUID 以用於執行。如果 QueryType 設置爲 adSchemaProviderSpecific 但是沒有提供 SchemaID,將導致錯誤。

提供者不需要支持所有的 OLE DB 標準模式查詢,只有 adSchemaTables、adSchemaColumns 和 adSchemaProviderTypes 是 OLE DB 規範需要的。但是對於這些模式查詢,提供者不需要支持上面列出的 Criteria 條件約束。

遠程數據服務用法 OpenSchema 方法在客戶端 Connection 對象上無效。

注意 在 Visual Basic 中,在由 Connection 對象的 OpenSchema 方法所返回的 Recordset 中有 4 字節無符號整型 (DBTYPE UI4) 的列無法與其他變量比較。有關 OLE DB 數據類型的詳細信息,請參閱“Microsoft OLE DB 程序員參考”的第十章和附錄 A。

---------------------------------------------------------------

OpenSchema 方法範例

該範例使用 OpenSchema 方法顯示 Pubs 數據庫內每個表的名稱和類型。

Public Sub OpenSchemaX()
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn
Set rstSchema = cnn1.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn1.Close
End Sub
該範例在 OpenSchema 方法的 Criteria 參數中指定 TABLE_TYPE 查詢約束。因此,只返回在 Pubs 數據庫中指定視圖的模式信息。然後該範例顯示每個表的名稱和類型。

Public Sub OpenSchemaX2()
Dim cnn2 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn2 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn2.Open strCnn
Set rstSchema = cnn2.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "VIEW"))
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn2.Close
End Sub
---------------------------------------------------------------

ado的recordset 對象在返回記錄集時,同時也有各字段的名字和類型。

具體是:recordset.Fields.Item(i).Name

recordset.Fields.Item(i).Type

類型是枚舉型,你可以查對應的值

還有其它屬性:

recordset.Fields.Item(i).DefinedSize 字段的定義寬度

recordset.Fields.Item(i).ActualSize 字段的實際寬度

....

---------------------------------------------------------------

其他我不想再說了,看我用過的:

for i=0 to rs.recordcount-1
print rs.Fields(i).Name & " " & rs.Fields(i).Type "(" & rs.Fields(i).ActualSize & ")"
next
---------------------------------------------------------------
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 * 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

問:怎麼把Sqlserver 的數據轉換並輸出成Xml格式文件?

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

解決方法:

SQL Server7的用法
Private Sub Command1_Click()
Dim strmResults As New ADODB.Stream
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim cnstr As String
cnstr = "Provider = SQLOLEDB.1;Password=121231;Persist Security Info=True;User ID=sa;Initial Catalog=zydb;Data Source=wzsswz"
cn.ConnectionString = cnstr
cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "select * from company for XML auto"
strmResults.Open
cmd.Properties("Output Stream").Value = strmResults
cmd.Properties("xml root") = "root"
cmd.Execute , , adExecuteStream
strmResults.Position = 0
strmResults.SaveToFile App.Path & "/outfile.xml"
strmResults.Close
Set strmResults = Nothing
End Sub
2000的用select * from company for XML auto就可以了
Option Explicit
Dim conDB As New ADODB.Connection
Private Sub cmdGetData_Click()
' Get inventory data
Dim rsInventory As New ADODB.Recordset
Dim stmInventory As New ADODB.Stream
' set db connection
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsInventory.Open "tblInventory", conDB, adOpenDynamic, adLockOptimistic
rsInventory.Save stmInventory, adPersistXML
' Save ADO XML to file
stmInventory.SaveToFile App.Path & "/inventory.xml", adSaveCreateOverWrite
' dispaly xml file to UI
wbXML.Navigate App.Path & "/inventory.xml"
' close database connection
conDB.Close
End Sub
Private Sub cmdGetDetail_Click()
' Get product detail
Dim rsProductDetail As New ADODB.Recordset
Dim stmProduct As New ADODB.Stream
' Set connection string
conDB.ConnectionString = ("Provider=SQLOLEDB.1;Server=Pinatubo;User ID=training; PWD=password;Initial Catalog=XMLTraining;")
conDB.Open
' set recordset
rsProductDetail.Open "SELECT *, tblProductDetail.* FROM tblInventory INNER JOIN " & _
"tblProductDetail ON tblInventory.fldProductID = tblProductDetail." & _
"fldProductID", conDB, adOpenDynamic, adPersistXML
rsProductDetail.Save stmProduct, adPersistXML
' Save ADO XML file to disk
stmProduct.SaveToFile App.Path & "/ProductDetail.xml", adSaveCreateOverWrite
' Load xml document in UI
wbXML.Navigate App.Path & "/ProductDetail.xml"
' Close database connection string
conDB.Close
End Sub
在.NET中就方面多了
select * from table for xml auto
問:如何終止一個被其它進程打開的Access文件?
解決方法:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameMSIExplorer = "IEFrame"
Const gcClassnameMSVBasic = "wndclass_desked_gsk"
Const gcClassnameNotePad = "Notepad"
Const gcClassnameMyVBApp = "ThunderForm"
Private Sub Form_Load()
Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
'Search the window
WinWnd = FindWindow(vbNullString, "Microsoft Access")
If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
'Show the window
ShowWindow WinWnd, SW_SHOWNORMAL
'Post a message to the window to close itself
PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub

發佈了9 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章