使用并调用表单/ c#中的连接字符串 [英] Use and call the connection string in the forms/ c#

查看:64
本文介绍了使用并调用表单/ c#中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0
下来投票

最爱

我正在使用c#构建桌面应用程序,我把连接字符串放在app.config文件中,就像这样



0 down vote
favorite
I''m building desktop application using c# , I put the connection string in the app.config file like this

<connectionStrings>
       <add name="ComputerManagement"
       connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=...;Initial Catalog=Computersh;Integrated Security=True"/>
     </connectionStrings>





如何在表单中调用连接字符串?或者为连接字符串创建一个类以及如何做到这一点更好?



How I can call the connection string in the forms ? or is it better to make a class for connection string and how I can do that ?

推荐答案

你可以用app.config从下面的代码中调用连接



1.)使用System.Configuration添加命名空间





2.)添加System.Configuration的项目参考



3.)您将在表单中获得以下代码的连接字符串

string conntr = ConfigurationManager .ConnectionStrings [ComputerManagement]。ConnectionString;







快乐编码:)
You can call the connection from app.config with below code

1.)add namespace
using System.Configuration

2.)Add reference to your project for System.Configuration

3.)You will get connection string in your form with below code
string conntr = ConfigurationManager.ConnectionStrings["ComputerManagement"].ConnectionString;



Happy coding :)


我相信你在发布问题之前没有搜索网页。试试这个 - 使用app.config中的连接字符串 [ ^ ]
I am sure you didn''t search the web before posting the question here. Try this - using connection string in app.config[^]


这篇关于使用并调用表单/ c#中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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