ConnectionString属性尚未初始化错误 [英] The ConnectionString property has not been initialized error

查看:9304
本文介绍了ConnectionString属性尚未初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了MySQL数据库的asp.net web应用程序,具有以下connetcion字符串的web.config:

 <添加名称=techConnectionString的connectionString =服务器= VM-tmysql01;用户ID =用户;数据库=高科技,密码=传的providerName =MySql.Data.MySqlClient />

这是code我用得到的web.config中的连接字符串:

<$p$p><$c$c>System.configuration.configurationmanaget.connectionstrings[\"techConnectionString\"].connectionstring;

由于某种原因,我得到的错误:


  

ConnectionString属性尚未初始化。未处理
  在当前web的执行过程中生成的异常
  请求。有关的起源和位置信息
  除了可以使用异常堆栈跟踪下面来识别。


和堆栈跟踪是:

  [出现InvalidOperationException:ConnectionString属性尚未初始化]
   System.Data.SqlClient.SqlConnection.PermissionDemand()6316916
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory的)6296606
   System.Data.SqlClient.SqlConnection.Open()+300

我能够通过数据源连接到数据库在Visual Studio,并采取了ConnectionString的从那里。
我真的需要一些帮助它,没有任何问题,我的连接字符串?


解决方案

这个问题是不是与引黄联接字符串,它是用C#code试图从web.config文件中实现它。

我试图用

<$p$p><$c$c>System.Configuration.ConfigurationManager.ConnectionStrings[\"techConnectionString\"].ConnectionString;

但我需要使用:

<$p$p><$c$c>System.Web.Configuration.WebConfigurationManager.ConnectionStrings[\"techConnectionString\"].ConnectionString;

I'm using an asp.net web application with a MySql DB, with the following connetcion string the web.config:

<add name="techConnectionString" connectionString="server=vm-tmysql01;User Id=user;database=tech;password=pass" providerName="MySql.Data.MySqlClient"/>

And this is the code I'm using the get the connection string from the web.config:

System.configuration.configurationmanaget.connectionstrings["techConnectionString"].connectionstring;

For some reason I get the error:

The ConnectionString property has not been initialized. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

And the stack trace is:

[InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +6316916
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6296606
   System.Data.SqlClient.SqlConnection.Open() +300

I'm able to connect to the DB via the Data Sources in Visual Studio and took the ConnectionString from there. I really need some help with it, is there any problem with my connection string?

解决方案

The problem wasn't with the connetion string, it was with the c# code trying to reach it from the web.config file.

i was trying to use:

System.Configuration.ConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;

But i needed to use:

System.Web.Configuration.WebConfigurationManager.ConnectionStrings["techConnectionString"].ConnectionString;

这篇关于ConnectionString属性尚未初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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