如何设置对VB数据库连接的动态路径
1个回答
展开全部
一、用data控件进行数据库链接,可以这样:在form_load()过程中放入:private form_load()Dim str As String '定义str = App.PathIf Right(str, 1) <> "\" Thenstr = str + "\"End Ifdata1.databasename=str & "\数据库名"data1.recordsource="数据表名"data1.refreshsub end这几句话的意为,打开当前程序运行的目录下的数据库。你只要保证你的数据库在你程序所在的目录之下就行了。二、利用adodc(ADO Data Control)进行数据库链接:private form_load ()Dim str As String '定义str = App.PathIf Right(str, 1) <> "\" Thenstr = str + "\"End Ifstr = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"Adodc1.ConnectionString = strAdodc1.CommandType = adCmdTextAdodc1.RecordSource = "select * from table3"Adodc1.Refreshend sub三、利用DataEnvironment进行数据库链接可在过程中放入:On Error Resume NextIf DataEnvironment1.rsCommand1.State <> adStateClosed Then DataEnvironment1.rsCommand1.Close '如果打开,则关闭End If 'i = InputBox("请输入友人编号:", "输入")'If i = "" Then Exit SubDataEnvironment1.Connection1.Open App.Path & "\userdatabase\tsl.mdb"DataEnvironment1.rsCommand1.Open "select * from table3 where 编号='" & i & "'"'Set DataReport2.DataSource = DataEnvironment1'DataReport2.DataMember = "command1"'DataReport2.showend sub四、利用ADO(ActiveX Data Objects)进行编程:建立连接:dim conn as new adodb.connectiondim rs as new adodb.recordsetdim strstr = App.PathIf Right(str, 1) <> "\" Thenstr = str + "\"End Ifstr = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb"conn.open strrs.cursorlocation=aduseclientrs.open "数据表名",conn,adopenkeyset.adlockpessimistic用完之后关闭数据库:conn.closeset conn=nothing以上代码在VB6,win98上运行通过,如有问题请发邮件给我:E_mail:xingyun0615@sina.com
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |