如何动态设置连接字符串. [英] how to set connection string dynamically.

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

问题描述

我正在Windows应用程序上使用Access数据库开发一个项目.如何动态设置连接字符串,以便用户在其计算机上安装软件时可以运行?

i am develope a project on Windows application and using access database.how to set connection string dynamically so that when user install software on his machine it will run?

推荐答案

类似的线程在这里
我们如何在C#.net中动态设置SQL Connection字符串( Windows应用程序) [ ^ ]
similar thread is here
How we set SQL Connection string dynamically in C#.net(Windows Application)[^]


为此,您可以在以下路径中将访问数据库文件复制到用户的AppData文件夹中
For this, you may copy the access database file inside the user''s AppData folder place at following path
//SystemDrive/Users/UserProfile/AppData or you may say C:/Users/ali/AppData/Roaming



要在C#中访问此文件夹,请写以下内容



to access this folder in C# write following

Environment.SpecialFolder.ApplicationData



将访问文件放置在安装程序安装或应用程序首次加载时. (取决于您的逻辑或偏好)并从该路径获得连接.



Place your access file here on either setup installation or at your application first time load. (depends on your logic or preference) and get connection from that path.


首先将应用程序配置文件添加到您的解决方案&在< configuration>之间添加以下内容:标签

First to add Application configuration file into your solution & add the following in between <configuration> tag

<appSettings>
    <add key="Your connection name" value="Your connection string"/>
      </appSettings>


对于Access连接字符串,请点击以下链接.
http://www.connectionstrings.com/access [


For Access connection string follow the below link.
http://www.connectionstrings.com/access[^]

Add
using System.Configuration;
namespace in to your solution.
getting connection string from App.config

string connection_string=ConfigurationSettings.AppSettings["your connection name"]


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

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