当前位置:编程学习 > C#/ASP.NET >>

大家帮忙给我修改下路径,修改为相对的,vb.net连接access数据库


Imports System.Net.Mime.MediaTypeNames

Public Class 登录
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (TextBox1.Text = "" Or TextBox2.Text = "") Then
            MsgBox("用户名和密码不能为空")
        ElseIf (TextBox1.Text = "admin" And TextBox2.Text = "admin") Then
            
            On Error GoTo ErrorHandler

            Dim oAccess As Access.Application
            Dim oForm As Access.Form
            Dim oCtls As Access.Controls
            Dim oCtl As Access.Control
            Dim sDBPath As String '数据库路径
            Dim sForm As String '要显示的窗体名字

            sForm = "管理员界面"


            oAccess = New Access.ApplicationClass()


            If Not oAccess.Visible Then oAccess.Visible = True



            sDBPath = "C:\Users\yuhua\Desktop\图书管理系统数据库.mdb"

            ' 打开数据库
            oAccess.OpenCurrentDatabase(filepath:=sDBPath, Exclusive:=False)


            ' 显示窗体
            oAccess.DoCmd.OpenForm(FormName:=sForm, _
                View:=Access.AcFormView.acNormal)

            ' 使用集合控制打开编辑数据库
            oForm = oAccess.Forms(sForm)
            oCtls = oForm.Controls



            ' 隐藏数据库其他窗口
            oAccess.DoCmd.SelectObject(ObjectType:=Access.AcObjectType.acForm, _
                ObjectName:=sForm, InDatabaseWindow:=True)
            oAccess.RunCommand(Command:=Access.AcCommand.acCmdWindowHide)


            oForm.SetFocus()

            ' 释放控制和窗体
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oCtls)
            oCtls = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oForm)
            oForm = Nothing

            ' 释放应用目标并允许用户关闭数据库
            If Not oAccess.UserControl Then oAccess.UserControl = True
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
            oAccess = Nothing

            Exit Sub
ErrorCleanup:
            ' 离开数据库
            On Error Resume Next
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oCtl)
            oCtl = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oCtls)
            oCtls = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oForm)
            oForm = Nothing
            oAccess.Quit(Option:=Access.AcQuitOption.acQuitSaveNone)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
            oAccess = Nothing

            Exit Sub
ErrorHandler:
            MsgBox(Err.Number & ": " & Err.Description, _
                MsgBoxStyle.MsgBoxSetForeground, "Error Handler")
            Resume ErrorCleanup
        ElseIf (TextBox1.Text = "user" And TextBox2.Text = "user") Then
            
            On Error GoTo ErrorHandler

            Dim oAccess As Access.Application
            Dim oForm As Access.Form
            Dim oCtls As Access.Controls
            Dim oCtl As Access.Control
            Dim sDBPath As String
            Dim sForm As String

            sForm = "用户界面"


            oAccess = New Access.ApplicationClass()


            If Not oAccess.Visible Then oAccess.Visible = True



            sDBPath = "C:\Users\zhangzhizhong\Desktop\数据库课程设计\db1.mdb"


            oAccess.OpenCurrentDatabase(filepath:=sDBPath, Exclusive:=False)



            oAccess.DoCmd.OpenForm(FormName:=sForm, _
                View:=Access.AcFormView.acNormal)


            oForm = oAccess.Forms(sForm)
            oCtls = oForm.Controls




            oAccess.DoCmd.SelectObject(ObjectType:=Access.AcObjectType.acForm, _
                ObjectName:=sForm, InDatabaseWindow:=True)
            oAccess.RunCommand(Command:=Access.AcCommand.acCmdWindowHide)


            oForm.SetFocus()


            System.Runtime.InteropServices.Marshal.ReleaseComObject(oCtls)
            oCtls = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oForm)
            oForm = Nothing

            If Not oAccess.UserControl Then oAccess.UserControl = True
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess)
            oAccess = Nothing

            Exit Sub

        Else
            MsgBox("无此用户名或密码错误!请重新输入")
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub


    Private Sub 登陆_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
vb.net access数据库 绝对路径修改相对路径 图书管理系统 --------------------编程问答-------------------- 将图书管理系统数据库.mdb放到exe同目录下

"图书管理系统数据库.mdb" --------------------编程问答-------------------- 不行啊,能不能帮我修改下代码 --------------------编程问答--------------------
引用 2 楼 wangyuhua901113 的回复:
不行啊,能不能帮我修改下代码

如1l所说,将图书管理系统数据库.mdb放到exe同目录下
然后数据库路径是application.startpath&"\数据库名.mdb"; --------------------编程问答-------------------- 我这样修改了  App.Path = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.startpath("\1图书管理系统数据库.mdb") & ";Persist Security Info=False"
报错说 “startpath”不是“System.Net.Mime.MediaTypeNames.Application”的成员。
--------------------编程问答-------------------- App.Path = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.startpath & ("\1图书管理系统数据库.mdb") & ";Persist Security Info=False" --------------------编程问答-------------------- 按您所述,任出现错误, Application.startpath属于什么组件 --------------------编程问答-------------------- C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll

属于这个组件 

Public NotInheritable Class Application
          Inherits System.Object
     System.Windows.Forms 的成员
摘要:
提供 static 方法和属性以管理应用程序,例如启动和停止应用程序、处理 Windows 消息的方法和获取应用程序信息的属性。无法继承此类。

--------------------编程问答-------------------- App.Path = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.startpath & ("\1图书管理系统数据库.mdb") & ";Persist Security Info=False"



你怎么把路径(App.Path这个在NET里有用吗?就算有用,能赋值吗?)直接写到数据库的连接字符串里去了。。?

还有,你这怎么有个App.Path,这个是vb6.0的写法,net的写法是Application.startpath
这两者在各自的环境里面,作用是一样的。都是取本EXE所在的目录。

你前面那个app.path绝对不对。

一般,连接字符串,我给你看看我的,现成的!
 LianJie = "provider=microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\HM.mdb"   '/*定义连接字符串*/

用的时候,直接

Dim CN As OleDbConnection
       
CN = New OleDbConnection(LianJie)

然后cn.open,就连上去了。
--------------------编程问答-------------------- 但是你在这么做之前,必须先把你那个1图书管理系统数据库.mdb放在你的EXE相同的位置,当你调试的时候,那个EXE是在debug文件夹里面的。。 --------------------编程问答-------------------- 积分不够,真难受,来评价一下,赚点积分。 --------------------编程问答-------------------- 前面好像有错误:应该是StartupPath而不是startpath
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,