[推荐]设置对Visual Basic数据库连接的动态路径
<P>VB在数据库链接的时候,一般是静态,即数据库存放的路径是固定的,如用VB的DATA,adodc,DataEnvironment 等到作数据库链接时,如果存放数据库的路径被改变的话,就会找不到路经,真是一个特别烦的事。 <BR>我的解决方法是:</P><P>一、用data控件进行数据库链接,可以这样: <BR>在form_load()过程中放入: <BR>private form_load() <BR>Dim str As String '定义 <BR>str = App.Path <BR>If Right(str, 1) <> "\" Then <BR>str = str + "\" <BR>End If <BR>data1.databasename=str & "\数据库名" <BR>data1.recordsource="数据表名" <BR>data1.refresh <BR>sub end <BR>这几句话的意为,打开当前程序运行的目录下的数据库。 <BR>你只要保证你的数据库在你程序所在的目录之下就行了。 </P>
<P>二、利用adodc(ADO Data Control)进行数据库链接: <BR>private form_load () <BR>Dim str As String '定义 <BR>str = App.Path <BR>If Right(str, 1) <> "\" Then <BR>str = str + "\" <BR>End If <BR>str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb" <BR>Adodc1.ConnectionString = str <BR>Adodc1.CommandType = adCmdText <BR>Adodc1.RecordSource = "select * from table3" <BR>Adodc1.Refresh <BR>end sub </P>
<P>三、利用DataEnvironment进行数据库链接 <BR>可在过程中放入: <BR>On Error Resume Next <BR>If DataEnvironment1.rsCommand1.State <> adStateClosed Then <BR>DataEnvironment1.rsCommand1.Close '如果打开,则关闭 <BR>End If <BR>'i = InputBox("请输入友人编号:", "输入") <BR>'If i = "" Then Exit Sub <BR>DataEnvironment1.Connection1.Open App.Path & "\userdatabase\tsl.mdb" <BR>DataEnvironment1.rsCommand1.Open "select * from table3 where 编号='" & i & "'" <BR>'Set DataReport2.DataSource = DataEnvironment1 <BR>'DataReport2.DataMember = "command1" <BR>'DataReport2.show <BR>end sub </P>
<P>四、利用ADO(ActiveX Data Objects)进行编程: <BR>建立连接: <BR>dim conn as new adodb.connection <BR>dim rs as new adodb.recordset <BR>dim str <BR>str = App.Path <BR>If Right(str, 1) <> "\" Then <BR>str = str + "\" <BR>End If <BR>str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "\tsl.mdb" <BR>conn.open str <BR>rs.cursorlocation=aduseclient <BR>rs.open "数据表名",conn,adopenkeyset.adlockpessimistic <BR>用完之后关闭数据库: <BR>conn.close <BR>set conn=nothing</P>
<P>不知道对大家有没有用,请大家多指教~~</P>
页:
[1]