首页 新闻 会员 周边 捐助

sqlconnection 无法加参数

0
[已解决问题] 解决于 2011-01-24 17:46

1 using System;
2  using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Web;
6 using System.Data;
7 using System.Data.Sql;
8 using System.Data.SqlClient;
9 using System.Configuration;
10 using System.Web.Security;
11 using System.Web.UI;
12
13 namespace test
14 {
15 public class operater
16 {
17 string strConn = ConfigurationManager.ConnectionStrings["ucssex_conn"].ConnectionString;
18 SqlConnection conn = new SqlConnection(strConn);
19
20 }
21 }
22

 

SqlConnection conn = new SqlConnection(strConn)这句会报错,提示找不strConn,为什么呢?

soarymz的主页 soarymz | 初学一级 | 园豆:0
提问于:2011-01-24 16:34
< >
分享
最佳答案
0

public class operater
{
public operater()
{
string strConn = ConfigurationManager.ConnectionStrings["ucssex_conn"].ConnectionString;
SqlConnection conn
= new SqlConnection(strConn);
}
}

 

Localhost | 菜鸟二级 |园豆:443 | 2011-01-24 16:40
谢谢,用了你的方法可以了,请问我的代码为什么会不行?
soarymz | 园豆:0 (初学一级) | 2011-01-24 16:45
strConn是非静态的字段,所以不能被引用.下面这段代码也可以. public class operater { static string strConn = ConfigurationManager.ConnectionStrings["ucssex_conn"].ConnectionString; SqlConnection conn = new SqlConnection(strConn); }
Localhost | 园豆:443 (菜鸟二级) | 2011-01-24 16:59
非常感谢!
soarymz | 园豆:0 (初学一级) | 2011-01-24 17:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册