如何使用c#从asp.net中的web.config文件中读取连接字符串 [英] how to read the connection string from web.config file in asp.net using c#

查看:67
本文介绍了如何使用c#从asp.net中的web.config文件中读取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Data;

命名空间 Sample2
{
public class MyFunctions
{
string connstring = ConfigurationManager.ConnectionStrings [ EpfConnectionString]。ConnectionString;
SqlConnection con = new SqlConnection(connstring); // connstring中的错误

}
}





#Above是我的类,它显示错误为错误12字段初始化程序无法引用非静态字段,方法或属性。

@error at SqlConnection con = new SqlConnection( connstring );



以下是我的网站。配置代码



< connectionStrings> 
< add name = EpfConnectionString connectionString = 数据源= ACS-MUMADHD-25 \SQLEXPRESS;初始目录= epf;持久安全信息=真;用户ID = sa;密码= sa2008 providerName = System.Data.SqlClient />
< / connectionStrings >





专家请帮助...

提前数百万的谢谢

解决方案

如果你收到一条你不理解的错误信息,请将其复制并粘贴到Google中 - 其他人之前遇到此问题的可能性非常大。

在你的情况下,很多人都对此感到困惑!

字段初始化程序无法引用非静态字段 [ ^ ]



如果您查看链接,它会解释问题 - 您无法声明并将类级变量设置为非静态值。



con 不在方法内,因此无法访问初始化程序中的非静态信息(因为这一切都必须在课程之前完成)构造函数被调用)

将初始化移动到类构造函数中,你会没事的:



  public   class  MyFunctions 
{
string connstring = ConfigurationManager.ConnectionStrings [ EpfConnectionString]。ConnectionString;
SqlConnection con = null ;
public MyFunctions()
{
con = new SqlConnection( CONNSTRING);
}
}


 SqlConnection con =  new  SqlConnection(ConfigurationManager.ConnectionStrings [  EpfConnectionString]。ConnectionString); 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Sample2
{
    public class MyFunctions
    {
        string connstring = ConfigurationManager.ConnectionStrings["EpfConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(connstring);  //error in connstring 
       
}
}



#Above is my class where it is showing error as Error 12 A field initializer cannot reference the non-static field, method, or property.
@error at SqlConnection con = new SqlConnection(connstring);

Below is my web.config code

<connectionStrings>
    <add name="EpfConnectionString" connectionString="Data Source=ACS-MUMADHD-25\SQLEXPRESS;Initial Catalog=epf;Persist Security Info=True;User ID=sa;Password=sa2008" providerName="System.Data.SqlClient" />
  </connectionStrings>



Experts Please help...
Millions of thanks in advance

解决方案

If you get an error message you don't understand, copy and paste it into Google - the chances are very good that someone else has met this before.
In your case loads of people have been confused by this one!
"A field initializer cannot reference the non-static field"[^]

If you have a look at the links, it explains the problem - you can't declare and set a class-level variable to a non-static value.

con is not inside a method, so it can't access non-static information in an initialiser (because it all has to be done before the class constructor is called)
Move the initialization into your class constructor, and you'll be fine:

public class MyFunctions
    {
        string connstring = ConfigurationManager.ConnectionStrings["EpfConnectionString"].ConnectionString;
        SqlConnection con = null;
        public MyFunctions()
        {
            con = new SqlConnection(connstring);
        }
    }


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EpfConnectionString"].ConnectionString);


这篇关于如何使用c#从asp.net中的web.config文件中读取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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