使用C#在app.config中动态创建connectionString [英] Dynamically create connectionString in app.config using C#

查看:360
本文介绍了使用C#在app.config中动态创建connectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个项目,我想动态地找到服务器的连接字符串。那个必须在app.config上,因为来自app.config的每一个连接..



所以我需要动态地在app.config上找到连接字符串,

这是可能的..







问候

AR

i was developed a project, i wants to find connection string of server at dynamically. and thats must be on app.config, because of the each and every connection from app.config..

so i need to find connectionstring on app.config at dynamically,
that is possible..



Regards
AR

推荐答案

嗨Rajan,



首先你要设置连接App.Config文件中的字符串。例如,我为你的数据库设置连接字符串,如下所示:

Hi Rajan,

First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here:
<configuration>
  <connectionstrings>
    <add name="TestConnectionstring"

    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </add></connectionstrings>
</configuration> 



之后使用此代码在表单中使用连接字符串:



在表单中设置所需的引用使用:


After that you use the connection string in your forms using this code:

In your forms you set references that you want to use:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;



然后你可以从App获取连接字符串.Config使用ConnectionStrings属性。


Then you can get the Connection String from the App.Config by using the ConnectionStrings property.

var connectionString=ConfigurationManager.ConnectionStrings["TestConnectionstring"].ConnectionString;



你可以使用这个方法在Windows窗体和ASP.NET项目中。



希望这对你有所帮助。



问候,

RK


You can use this method in both Windows Forms and ASP.NET projects.

Hope this helps you a bit.

Regards,
RK


这篇关于使用C#在app.config中动态创建connectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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