如何在app.config文件中为access数据库写连接字符串 [英] How to write connection string for access database in app.config file

查看:205
本文介绍了如何在app.config文件中为access数据库写连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我有疑问,我在 app.config 文件中写了以下连接字符串,



Hi i have a doubt, i written the following connection string in app.config file,

add name="constr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\sw3\Desktop\Invoicebill.mdb;Persist Security Info=True"





form1.cs 我将连接字符串称为以下格式,





in form1.cs i called the connection string as the following format,

OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["constr"]);

con.open();





它显示以下错误,



ConnectionString属性尚未初始化。



怎么麻烦拍这个问题你能帮帮我吗?



it's showing the following error,

"The ConnectionString property has not been initialized."

how to trouble shoot this problem can you help me?

推荐答案

使用此

in your app.config file use this
<configuration>
  <appsettings>
    <add key="constr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\sw3\Desktop\Invoicebill.mdb;Persist Security Info=True" />   
  </appsettings>
</configuration>



然后在form1.cs


and then in form1.cs

OleDbConnection con=new OleDbConnection(ConfigurationSettings.AppSettings["constr"]);
 
con.open();


亲爱的Vivek,



请为您的问题找到以下代码。



App.config文件



< configuration>

< connectionstrings>

< add name =MSSConStr>

connectionString =数据源= Sithi-PC;初始目录= mssdb2;用户ID = XXXXX;密码= YYYYYYY

providerName =System.Data.SqlClient/>









var myConnectionString = ConfigurationManager.ConnectionStrings [MSSConStr]。ConnectionString;



问候,

Praveen Nelge
Dear Vivek,

Please find the below code for your Problem.

App.config file

<configuration>
<connectionstrings>
<add name="MSSConStr">
connectionString="Data Source=Sithi-PC;Initial Catalog=mssdb2;User ID=XXXXX;Password=YYYYYYY"
providerName="System.Data.SqlClient" />




var myConnectionString = ConfigurationManager.ConnectionStrings["MSSConStr"].ConnectionString;

Regards,
Praveen Nelge


<configuration>
 <appsettings>
   <add key="ApplicationTitle" value="Sample Console Application" />
   <add key="ConnectionString">
       value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\sw3\Desktop\Invoicebill.mdb;Persist Security Info=True";Database=Northwind;Integrated
              Security=false;User Id=sa;Password=;" />
</add></appsettings>
</configuration>


这篇关于如何在app.config文件中为access数据库写连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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