在app.config中连接到connectionString [英] Connecting to connectionString in app.config

查看:242
本文介绍了在app.config中连接到connectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的连接字符串存储在App.Config中

 < connectionStrings> 
< clear />
< add name =CTaC_Information_System.Properties.Settings.CIS_beConn
connectionString =Provider = Microsoft.ACE.OLEDB.12.0;数据源=& quot; \\server\file \CIS Data\Database\CIS_be.accdb& quote ;; Jet OLEDB:Database Password = 123
providerName =System.Data.OleDb/>

然后当我访问main.xaml.cs时,输入以下命令:

  string cisconn = ConfigurationManager.ConnectionStrings [CTaC_Information_System.Properties.Settings.CIS_beConn]。ConnectionString;`

我发现在搜索时Stack Overflow的答案,但有些人说 var 但是当我键入 var 它不会识别它,所以我去与 string 方法。



当我去键入 cisconn.Open(); 的选项不存在。我引用 System.Configuartion; System.Data.Sql; System.Data。 SqlClient; System.Data.OleDb;



我怎么可以从c#连接到数据库?我尝试在我的应用程序运行时测试连接,但我无法确定。

解决方案

只是一个字符串,它的意思是用在你的连接,所以你应该做:

  public void DoSomeDatabaseOp()
{
string cisconn = ConfigurationManager.ConnectionStrings [CTaC_Information_System.Properties.Settings.CIS_beConn]。ConnectionString;

使用(OleDbConnection conn = new OleDbConnection(cisconn))
{
conn.Open();
//在此创建命令或执行SQL。
}
}

您应该在方法中创建/销毁连接正在使用它。不要在类对象的根中保留对它的引用。



如果你真的想这样做,你可以这样做:

  class MyClass 
{
OleDbConnection _rootConn;
string _connStr;

public MyClass()
{
_connStr = string cisconn = ConfigurationManager.ConnectionStrings [CTaC_Information_System.Properties.Settings.CIS_beConn]。ConnectionString;
_rootConn = new OleDbConnection(_connStr);
}

public void DoSomeDatabaseOp()
{
//使用_rootConn here
}
}



但是类应该实现IDisposable,以便它可以正确处理连接!如何实现IDisposable是超越答案的范围,但是查找如何正确实现它。


I have my connection string stored in App.Config

<connectionStrings>
 <clear />
 <add name="CTaC_Information_System.Properties.Settings.CIS_beConn"
     connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=&quot;\\server\file\CIS Data\Database\CIS_be.accdb&quote;;Jet OLEDB:Database Password=123" 
     providerName="System.Data.OleDb" />

Then when I go to my main.xaml.cs I type in the following:

string cisconn = ConfigurationManager.ConnectionStrings["CTaC_Information_System.Properties.Settings.CIS_beConn"].ConnectionString;`

I found that answer on Stack Overflow when searching, but some were say to put var but when I typed var it wouldn't recognize it so I went with the string method.

When I go to type cisconn.Open(); the option isn't there. I am referencing System.Configuartion;,System.Data.Sql; System.Data.SqlClient; and System.Data.OleDb;.

Can someone show / tell me how I can connect to the database from c#? I'm trying to test the connection when my application runs but I can't figure it out.

解决方案

The connection string is just a string, its meant to be used in your connection, so you should do:

public void DoSomeDatabaseOp()
{
    string cisconn = ConfigurationManager.ConnectionStrings["CTaC_Information_System.Properties.Settings.CIS_beConn"].ConnectionString;

    using (OleDbConnection conn = new OleDbConnection(cisconn))
    {
        conn.Open();
        //Create your commands or do your SQL here.
    }
}

You should create/destroy the connection inside the method you are using it in. Don't keep a reference to it in the root of the class object. This keeps the connections clean and open if you aren't doing database operations.

If you really wanted to though, you could do this:

class MyClass
{
    OleDbConnection _rootConn;
    string _connStr;

    public MyClass()
    {
        _connStr = string cisconn = ConfigurationManager.ConnectionStrings["CTaC_Information_System.Properties.Settings.CIS_beConn"].ConnectionString;
        _rootConn = new OleDbConnection(_connStr);
    }

    public void DoSomeDatabaseOp()
    {
        //Use _rootConn here
    }
}

BUT the class should implement IDisposable so that it can dispose of the connection properly! How to implement IDisposable is beyond the scope of the answer, but look up how to implement it properly.

这篇关于在app.config中连接到connectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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