通过c#中的app.config文件连接到sql [英] connecting to sql through app.config file from c#
本文介绍了通过c#中的app.config文件连接到sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友
我想通过使用C#窗口形式的配置文件来连接SQL。但我不能使用configurationManager。你可以请你建议我任何其他方式连接因为我需要在一个地方连接sql然后想从很多地方使用它。
谢谢
Dear Friends
I want to connect with SQL by using config file in C# window form . but i can not use configurationManager. Can u all please suggest me any other way to connect becaue i need sql connection in one place and then want to use it from many place.
thanks
推荐答案
将连接字符串存储在您的设置文件或用户的数据文件中数据文件夹(请参阅此处了解如何使用该文件夹:我应该在哪里存储数据? [ ^ ])
要使用设置文件,只需在解决方案资源管理器中打开项目,打开属性分支,然后双击设置。设置
给新字符串命名为DatabaseConnection,将其设置为string,user,并设置合理的默认值(与SQL实例的连接字符串)
并保存并关闭页面。
然后当你想要使用它时:
Store the connection string in your settings file, or in a datafile in the User Data folder (see here for how to use that: Where should I store my data?[^])
To use the settings file, just open your project in the Solution Explorer, open the "Properties" branch, and double click on "Settings.Settings"
Give a new string a name "DatabaseConnection", set it as "string", "user", and set a sensible default (the connection string to your SQL instance)
and save and close the page.
Then when you want to use it:
string conStr = Properties.Settings.Default.DatabaseConnection;
保存新的值:
To save a new value:
Properties.Settings.Default.DatabaseConnection = conStr;
Properties.Settings.Default.Save();
这篇关于通过c#中的app.config文件连接到sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文