使用C#winform中的app.config访问数据库连接字符串

时间:2011-12-12 14:48:51

标签: c# sql winforms database-connection app-config

我似乎无法在我的c#winforms app中访问app.config数据库连接字符串。

app.config代码

   <connectionStrings>
      <add name="MyDBConnectionString" providerName="System.Data.SqlClient"
            connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
   </connectionStrings>  

C#代码:

SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["MyDBConnectionString"];    

当我尝试C#代码时,我收到一条消息:
警告1'System.Configuration.ConfigurationSettings.AppSettings'已过时:' 此方法已过时,已被System.Configuration取代!System.Configuration.ConfigurationManager.AppSettings'

但是,当我尝试使用时:

conn.ConnectionString = System.Configuration!System.Configuration.ConfigurationManager.AppSettings["MyDBConnectionString"];  

我收到错误:只有赋值,调用,递增,递减和新对象表达式才能用作语句

10 个答案:

答案 0 :(得分:28)

这就是你所需要的:

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

答案 1 :(得分:8)

使用ConfigurationManager代替ConfigurationSettings。它具有ConnectionStrings属性,您应该将其用于connectionStrings部分中的连接字符串:

ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

答案 2 :(得分:6)

您正在使用ConnectionStrings集合,而不是AppSettings。

ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString;

答案 3 :(得分:4)

试试这个

 ConfigurationManager.ConnectionStrings["MyDbConnectionString"].ConnectionString; 

答案 4 :(得分:4)

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ToString()))
{
....(your code here) ...
}

答案 5 :(得分:4)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>

<add key="ConnectionString" value="Data Source=MY-PC;Initial Catalog=DB2013;User ID=sa;Password=MYSQL123" />

</appSettings>

</configuration>
using System.Configuration;
using System.Data.SqlClient;

namespace OnlineDelete_W2013
{
public partial class CommodityEdit : Form
{
   SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);

    public CommodityEdit()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
           MyConnection.Open();
        }
        catch (Exception)
        {

            throw;
        }

答案 6 :(得分:2)

SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager
    .ConnectionStrings["MyDBConnectionString"].ConnectionString;
try
{
    conn.Open();                
}
catch (Exception)
{
    throw;                    
}

答案 7 :(得分:1)

说明使用该行的答案

ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

是对的。

如果出现错误,指出ConfigurationManager不存在,那是因为您的项目没有引用 System.Configuration

要在.NET Framework中,在解决方案资源管理器中执行此操作,在要使用此行代码的项目中,右键单击引用,选择< strong>添加引用... ,然后在左侧选择程序集,在其下选择框架。在列表中选择 System.Configuration ,然后点击确定

答案 8 :(得分:0)

关于此:

  

我得到一个错误:只有赋值,调用,递增,递减和新的对象表达式都可以用作语句

我刚刚这样声明了一个变量并解决了问题:

var strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

答案 9 :(得分:-1)

请尝试以下代码。这就是您所期望的:

Range