Visual Basic数据库开发疑难问题解
2005-09-28 17:06
253 查看
问:如何显示格式为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。 问:处理文本文件是导入数据库还是直接读写文件呢? 解决方法:
说明 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 数据库内每个表的名称和类型。
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
问:如何通过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 值
说明 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 数据库内每个表的名称和类型。
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
问:如何通过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 值
说明 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 数据库内每个表的名称和类型。
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
解决方法:
select * from table for xml auto
问:如何终止一个被其它进程打开的Access文件?
解决方法:
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的数据库的备份和恢复? 解决方法:SchemaID OLE DB 规范没有定义用于提供者模式查询的 GUID。如果 QueryType 设置为 adSchemaProviderSpecific,则需要该参数,否则不使用它。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 值 [code]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
说明 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_NAMESchemaID 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文件?
解决方法:
[code]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_NAMESchemaID 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文件?
解决方法:
[code]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
相关文章推荐
- MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求
- Zurmo开发中的注意问题1-数据库管理工具的选择
- Oracle新手笔记(1)- Oracle 数据库命名问题及使用Oracle数据库开发的软件的命名问题
- Hibernate开发实践笔记--对象在set属性时更新了数据库问题
- 关于使用Yii框架开发时数据库增加字段后保存无效的问题
- DELPHI6开发数据库发现的一个问题
- WM开发的数据库问题
- 开发中遇到的并发和数据库问题
- Django开发的数据库连接问题。
- Delphi XE8中开发DataSnap程序常见问题和解决方法 (二)想对DBExpress的TSQLDataSet写对数据库操作的SQL语句出错了!
- dreamweaver开发jsp问题连接数据库
- java报表软件FineReport数据库连接疑难问题解决方案汇总
- Android开发中数据库(sqlite)的查看及一些问题的解决方法
- SAP PI接收开发中的几个疑难问题!
- 数据库连接监控组件,避免日常开发中因为数据库连接长时间占用或业务完成后忘记关闭连接所带来的数据库问题
- 使用JSP开发应用程序时的编码(中文等乱码)问题的建议解决方案(包括接受request和数据库操作)
- 基于Grove的.NET应用程序开发提示1--数据库主键设置问题
- 使用heroku开发应用时连接数据库遇到的问题及解决方案
- 【SQL疑难问题】--跨域访问数据库(未解决)
- 数据库开发基本操作-关于sql server 2005 未开放1433端口的问题