如何拆分连接字符串 [英] How to split a connection string

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

问题描述

我正在使用C#,实体数据模型(.edmx)在ASP.Net MVC3上做一个项目,我想分割连接字符串,如

数据源= xxx- PC; 
初始目录= xxx;
用户ID = sa;
密码= xxxx;





但是我无法做到这一点。



这是我在web.config中的连接字符串



 <   connectionStrings  >  
< ; add name < span class =code-keyword> = VidyaDaanEntities connectionString = metadata = res://*/VidyaDaanEntities.csdl | res: //*/VidyaDaanEntities.ssdl|res://*/VidyaDaanEntities.msl;provider=System.Data.SqlClient;provider connection string = 数据 来源 = xxx-PC;初始 目录 = xxx;用户 ID = sa;密码= xxxx; MultipleActiveResultSets = True providerName = < span class =code-keyword> System.Data.EntityClient / >
< / connectionStrings >

解决方案

嗨harshal



你可以得到这个子代码的连接字符串:



 string strConnectionString = ConfigurationManager.AppSettings [ConnectionString]; 



ConnectionString是连接字符串形式的名称 web.config 之后你可以用

 strConnectionString.Split() strConnectionString  $ p>方法。



如果您想获得有关连接字符串的更多信息,请查看以下链接:

如何从c#中的App.Config获取连接字符串 [ ^ ]



祝你好运



祝你好运。


使用System.Configuration命名空间来访问连接字符串



string myconnection = ConfigurationManager.AppSettings [ConnectionString]



之后使用split()分割连接字符串



myconnection.Split('';'')使用索引来获取数据源,初始目录等。



再次用''=''拆分以获得最终值。


i am doing a project on ASP.Net MVC3 using C#,Entity data model(.edmx),i Want to split connection string like

Data Source=xxx-PC;
Initial Catalog=xxx;
User ID=sa;
Password=xxxx;



but i am unable to do that.

Here is my connection string in web.config

<connectionStrings>
 <add name="VidyaDaanEntities" connectionString="metadata=res://*/VidyaDaanEntities.csdl|res://*/VidyaDaanEntities.ssdl|res://*/VidyaDaanEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=xxx-PC;Initial Catalog=xxx;User ID=sa;Password=xxxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>

解决方案

Hi harshal

You can get the connection string follow of this sub code:

string strConnectionString=ConfigurationManager.AppSettings["ConnectionString"];


ConnectionString is the name of your connection string form web.config after that you can split the strConnectionString with

strConnectionString.Split()

method.

If you want to the get more information about connection string please review this link:
How to get Connection String from App.Config in c#[^]

Good luck

Best regards.


Use Namespace using System.Configuration to access connectionstring

string myconnection=ConfigurationManager.AppSettings["ConnectionString"]

after that use split() to split the connectionstring

myconnection.Split('';'') use index to get Data Source, Initial Catalog etc.

and again split with ''=''to get final value.


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

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