C#连接字符串 [英] C# connection string

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

问题描述

我正在开发一个c#应用程序,我需要从SQL数据库服务器获取数据。在我的应用程序中,我不想制作硬编码连接字符串,因为有时我必须将它连接到不同的数据库,否则我将在不同的位置进行测试。



为此我有一个计划,我使用此表单创建一个xml文件和一个新表单(具有管理员权限)我在xml文件中更新/插入数据库凭据,或者我可以使用microsoft数据链接指向不同的数据库。



你能帮我创建新的xml文件或有任何击球手的想法吗?



我试过的:



我没有尝试任何东西。我什么都不知道......

I am developing an c# application in which I need to fetch data from SQL database server. In my application I do not want to make a hard coded connection string because sometimes i have to connect it with different DB or i will test in different locations.

For this I have a plan that I create an xml file and a new form(with admin rights) using this form I update/insert database credentials in xml file,or i can use microsoft data link to point different DB.

Can you please help me out for creating new xml file or have any batter idea?

What I have tried:

I didnt try anything.I dont have any idea...

推荐答案

.net已经有一个框架,可以让你在配置文件中存储连接字符串



ConfigurationManager.ConnectionStrings属性(System.Configuration) [ ^ ]
.net already has a framework that lets you store connection strings in config files

ConfigurationManager.ConnectionStrings Property (System.Configuration)[^]


作为替代方案,您可能会发现这很有用:实例存储 - 在应用程序之间共享配置数据的简单方法 [ ^ ] - 这是我用来在各种Windows应用程序之间共享数据的方法,包括单个连接字符串。
As an alternative, you might find this useful: Instance Storage - a simple way to share configuration data among applications[^] - It's what I use to share data between my various Windows apps, including a single connection string.


为什么不能在应用程序中使用web.config或app.config文件并在其中包含connectionstring。通过这种方式,您可以避免编写不必要代码的开销。



以下示例(web.config)

Why can't you use the web.config or app.config file in your application and have the connectionstring in it. this way you will avoid overhead of writing unnecessary code.

example below (web.config)
<connectionStrings>
    <add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>


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

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