如何构建正确的connectionString? [英] How do I build correct connectionString?

查看:82
本文介绍了如何构建正确的connectionString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我目前正在使用ASP.net c#开发一个简单的Web应用程序。



我正在建立一个连接字符串并检查web.config文件中是否有有效或现有的连接字符串。



Hello,

I am currently developing a simple web application with using ASP.net c#.

I am building a connectionstring and to check if if have a valid or existing connectionstring in web.config file.

class WebVacationManagerDac
{
   Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
   ConnectionStringSettings connString;

   if (config.ConnectionStrings.ConnectionStrings.Count > 0)
   {
      connString = config.ConnectionStrings.ConnectionStrings["FTDP"];
   }
}





问题是我声明'config'并尝试检查是否存在是web.config中的一个connectionString。但是,我得到一个错误,说WebVacationManagerDac.config它是'字段',但在IF语句中用作'类型'



任何建议或建议? ?请



The problem is have is that I declared 'config' and try to check if there is one connectionString in web.config. however, I get an error saying "WebVacationManagerDac.config it is 'field' but is used like a 'type'" within IF Statement

Any suggestion or advice??? Please

推荐答案

您必须在配置部分下的Web.config中编写连接字符串如下:

< connectionstrings>

< add name =ConStrconnectionstring =Server =。; DataBase = test; integrated Security = trueprovidername =System.Data.SqlClient>



并在后面的代码中访问连接字符串如下:



You have to write connection String in Web.config under Configuration Section Like following:
<connectionstrings>
<add name="ConStr" connectionstring="Server=.;DataBase=test;integrated Security=true" providername="System.Data.SqlClient">

And Access the connection String in code behind like following:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);








OR

string str=ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

现在你可以使用str作为连接字符串

Now you can use "str" as connection String any where


ConnectionStrings.ConnectionStrings


这篇关于如何构建正确的connectionString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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