添加数据库连接:相对路径 [英] Add Database connect: Relative path

查看:81
本文介绍了添加数据库连接:相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我在向Visual Studio添加Access数据库时遇到了一些麻烦。我不想从"本地存储"复制数据库,而是希望立即查看\bin\Debug \ dbUsers.accdb。


此软件必须运行另一个系统因此具有文件C:\ Users \Default \Desktop \ etc的绝对路径将不起作用。 


我希望它访问的数据库位于项目Debug / output文件夹(\bin\Debug \ dbUsers.accdb)。 


项目文件夹是VicRoads Logging Application \ bin\Debug



As如上所示,我获得了测试连接成功,但是当我点击Okay和Next时,我收到此错误:



请注意,我应该只有3个"VicRoads Logging Application"。文件夹。



<有没有人知道为什么第一个连接开始在第二个测试的不同文件夹中查找,以及如何解决这个问题?


干杯!

解决方案

以这种方式处理数据的默认行为是将数据库放在与可执行文件相同的文件夹中。以这种方式完成时,app.config将具有如下连接字符串,在运行时解析为当前文件夹。



假设我希望数据来自C:\ Data(或其他现有文件夹),在表单加载中我可以使用

 Public Class Form1 
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object,e As EventArgs)_
Handles CustomersBindingNavigatorSaveItem.Click
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet1)

End Sub
Private Sub Form1_Load(sender) As Object,e As EventArgs)处理MyBase.Load

如果IO.Directory.Exists(" C:\ Data")那么
如果IO.File.Exists(" C) :\ Data\NorthWind_1.accdb")然后
AppDomain.CurrentDomain.Set数据("DataDirectory","C:\ Data")

'TODO:这行代码将数据加载到'DataSet1.Customers'
'表中。您可以根据需要移动或删除它。
Me.CustomersTableAdapter.Fill(Me.DataSet1.Customers)
否则
'数据库文件不存在
结束如果
否则
'路径不存在存在
结束如果
结束子
结束等级





Hello,

I am having some trouble adding an Access database to Visual studio. Instead of copying the database from "local storage", I would like to have it look immediately towards \bin\Debug\dbUsers.accdb.

This software must be run on another system and therefore having an absolute path to the file C:\Users\Default\Desktop\etc will not work. 

The database I would like it to access is located in the project Debug/output folder (\bin\Debug\dbUsers.accdb). 

The project folder is VicRoads Logging Application\bin\Debug

As seen above, I get a test connection successful, however when I hit Okay and Next, I get this error:

Please note that I should only have 3 "VicRoads Logging Application" Folders.

Does anyone know why the first connection start looking in a different folder to the second test, and how to fix this?

Cheers!

解决方案

The default behavior for working with data this way is to place the database in the same folder as the executable. When done this way the app.config will have a connection string as follows which at runtime resolves to the current folder.

Suppose I want the data to come from C:\Data (or another existing folder), in form load I can use

Public Class Form1
    Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
        Handles CustomersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.CustomersBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DataSet1)

    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If IO.Directory.Exists("C:\Data") Then
            If IO.File.Exists("C:\Data\NorthWind_1.accdb") Then
                AppDomain.CurrentDomain.SetData("DataDirectory", "C:\Data")

                ' TODO: This line of code loads data into the 'DataSet1.Customers' 
                ' table. You can move, or remove it, as needed.
                Me.CustomersTableAdapter.Fill(Me.DataSet1.Customers)
            Else
                ' database file does not exists
            End If
        Else
            ' path does not exist
        End If
    End Sub
End Class


这篇关于添加数据库连接:相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆