如何动态更改数据集类对象的连接字符串 [英] How to dynamically change a connection string for a dataset class object

查看:70
本文介绍了如何动态更改数据集类对象的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次,

我有一个问题,我需要你的专业知识。我正在创建一个Visual Basic应用程序,其中MS Access数据库作为主要数据连接。在这方面我已经配置了一个类型化的数据集将mdb文件的所有对象(表,
查询,关系)。在我的代码中,我可以轻松地引用我的类型化数据集中的表和其他项。我的mdb文件不在项目文件夹中,而是位于不同的位置(例如:D:\Projects \ Adminagement \NotaBene \Databases)。在
的应用程序设置中添加一个连接字符串

Provider = Microsoft.Jet.OLEDB.4.0; DataSource =" D:\Projects\Management\NotaBene\Databases\Accounts.mdb"

在设计中,连接到mdb文件没有问题。在我手动创建 之前,如果在安装程序包并安装到另一台计算机上时,与数据库的连接失败了
用户计算机上的相同文件夹结构并复制mdb文件。

At design there is no problem to connect to the mdb file. How when cerate an installation package and installed on another computer the connection to the database failed until I manually creat the  same folder structure on the user computer and copy the mdb file in.

我的问题是:如何在设计时配置连接字符串,以便用户可以在运行时通过对话框动态更改它以找到mdb文件?

我想提供一种在应用程序设置中更改连接字符串的方法,但它是应用程序级别设置,因此只读。

感谢您提供的任何帮助

问候

Ekoue

推荐答案

创建配置选项并在其中使用文件打开对话框(例如用于打开其他类型的文件),以便用户可以浏览到该文件。添加您想要的任何验证,以确保它是有效的数据库。将路径(和名称)保存在配置
设置中。由于你对语言含糊不清,所以我只能相应地含糊不清。然后每次打开数据库时,只需将路径和文件名放入连接字符串即可。在您的程序中,您只需要" Provider = Microsoft.Jet.OLEDB.4.0; DataSource = "
表示连接字符串,然后附加路径等。我假设您还必须添加双引号。
Create a configuration option and in it use a file open dialog (such as for opening other types of files) so the user can browse to the file. Add whatever validation you want to ensure it is a valid database. Save the path (and name) in a configuration setting. Since you are vague about the language and such I can only be correspondingly vague. Then every time the database is opened you simply put the path and filename into the connection string. In your program you wuold just need "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" for the connection string and then you append the path and such. I assume you must also add the double-quotes.


这篇关于如何动态更改数据集类对象的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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