connectionstring属性尚未初始化 [英] The connectionstring property has not been initialized

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

问题描述

每当我运行我的应用程序时,我得到ConnectionString属性尚未初始化。这个错误最近我改变了我的连接字符串首先它是一个本地连接字符串,但现在我使用web.config文件创建了dyanamic连接字符串,而当它的本地连接应用程序运行正常但现在它抛出异常



whenever i run my application i got "The ConnectionString property has not been initialized." this error recently i changed my connection string first it was a local connection string but now im created dyanamic connection string using web.config file , while when its a local connection application running fine but now its throw above exception

web.config file : 

<configuration>
  <connectionstrings>
    <add name="AccessConnectionString" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|BillAndReceipt.mdb;Persist Security Info=False;" providername="System.Data.OleDb" />
  </connectionstrings>
</configuration>





我的尝试:



my以前的连接字符串是:

OleDbConnection cnn = new OleDbConnection();

string connectionString = null;

connectionString = @provider = Microsoft。 ACE.OLEDB.12.0;数据源= E:\Database \ BillAndReceipt.accdb; Persist Security Info = False;;



我的最新连接字符串:



connectionString = ConfigurationSettings.AppSettings [AccessConnectionString];





cnn.ConnectionString = connectionString;

cnn.Open();





exeception是给cnn.open();行



What I have tried:

my previous connection string is :
OleDbConnection cnn=new OleDbConnection ();
string connectionString = null;
connectionString = @" provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\Database\BillAndReceipt.accdb;Persist Security Info=False;";

my latest connection string :

connectionString = ConfigurationSettings.AppSettings["AccessConnectionString"];


cnn.ConnectionString = connectionString;
cnn.Open();


exeception is give cnn.open(); line

推荐答案

1.字符串文件路径应包含一个\ after | DataDirectory |,如...

Provider = Microsoft.ACE.OLEDB。 12.0;数据源= | DataDirectory | \ BillAndReceipt.mdb;持久安全信息=错误;



2.此外,您似乎有本地和DataDirectory的不同扩展名,另一个用accdb和mdb。

也许你想查看你的文件扩展名。



3.最后,有不同的方法来检索连接stri使用c#从web.config文件中取决于你存储它的方法。

有一篇很好的文章在

Web.config中的存储连接字符串 - ConnectionStrings.com [ ^ ]

在c#代码中也似乎有一个ptoblem。只确保你的c#标题有

1. String file path should contain a \ after |DataDirectory| as in...
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.mdb;Persist Security Info=False;

2. Also, you seem to have different extensions for local and DataDirectory, with accdb and mdb on the other one.
Maybe you want to look into your filename extension as well.

3. Finally, there are different ways to retrieve the connection string from a web.config file using c# depending on the method you store it.
There is a good article on that at
Store Connection String in Web.config - ConnectionStrings.com[^]
There seems to be a ptoblem with that too in the c# code. only make sure too your c# header has
using System.Configuration;


尝试使用以下代码:



添加以下连接字符串 web.config

Try with below code:

Add following connection string web.config
<configuration>
  <connectionstrings>
    <add name="AccessConnectionString"             connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|BillAndReceipt.mdb;Persist Security Info=False;" providername="System.Data.OleDb" />
  </connectionstrings>
</configuration>



添加 System.Configuration 作为当前项目的参考。添加引用后,从web.config中获取连接字符串,如下所示:


Add System.Configuration as a reference in your current project. Once reference is added, fetch connection string from web.config like below:

// Add a using directive at the top of your code file
using System.Configuration;

// Get connection string value from web.config
string connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

// Create OleDbConnection object with connection string
OleDbConnection cn = new OleDbConnection(); 
cn.ConnectionString = connectionString;
cn.Open();


add name =AccessConnectionStringconnectionString =Provider = Microsoft.ACE.OLEDB.12.0; Data Source = | DataDirectory | \ BillAndReceipt.accdb; Persist Security Info = False; providerName =System.Data.OleDb





伙计这个字符串我认为工作得很好....



谢谢你manas_kumar先生和homero先生
add name="AccessConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\BillAndReceipt.accdb;Persist Security Info=False;providerName="System.Data.OleDb"


guys this string i think working fine hopefully....

thank you manas_kumar sir and homero sir


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

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