我是一个asp.net新手,遇到了一个问题“ConnectionString尚未初始化 ”,希望各位高手前辈帮我解决下。在这里谢谢你们了 。我的e-mail:[email protected]
//出现问题的页面:
public class SqlData
{
// 类中全局变量
private SqlConnection sqlCon;//声明一个SqlConnection对象
private SqlCommand sqlCmd;//声明一个SqlCommand对象
private SqlDataAdapter sda;//声明SqlDataAdapter对象
//构造方法
public SqlData()
{
//
// TODO: 在此处添加构造函数逻辑
//
sqlCon = new SqlConnection(ConfigurationManager.AppSettings["strCon"]);
sqlCon.Open();//打开连接
}
//执行SQL语句
public bool ExceSQL(string strSqlCom)//该方法用来执行SQL语句
{
sqlCmd = new SqlCommand(strSqlCom, sqlCon);
try
{
sqlCmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
throw (new Exception(e.Message));
return false;
} finally
{
sqlCon.Close();
}
}
//返回一个DataSet类型数据
public DataSet ExceDS(string strCom)//该方法返回一个DataSet类型
{
try
{
sqlCmd = new SqlCommand(strCom, sqlCon);
sda = new SqlDataAdapter();
sda.SelectCommand = sqlCmd;
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
finally
{
sqlCon.Close();
}
} public DataSet ExceDS2(string strCom, string TableName)
{
try
{
SqlDataAdapter sda = new SqlDataAdapter(strCom, sqlCon);
DataSet ds = new DataSet();
sda.Fill(ds, TableName);
return ds;
}
finally
{
sqlCon.Close();
}
}
//返回一个SqlDataReader类型数据 public SqlDataReader ExceRead(string strCom)//该方法返回一个SqlDataReader类型
{
sqlCmd = new SqlCommand(strCom, sqlCon);
SqlDataReader sdr = sqlCmd.ExecuteReader();
return sdr;
sqlCon.Close();
}
}
//配置文件web.config的部分内容:
<configuration>
<appSettings>
<add key="ConnectionString" value="server=localhost;database=db_news;uid=sa;pwd=;"/>
</appSettings>
</configuration>
//出现问题的页面:
public class SqlData
{
// 类中全局变量
private SqlConnection sqlCon;//声明一个SqlConnection对象
private SqlCommand sqlCmd;//声明一个SqlCommand对象
private SqlDataAdapter sda;//声明SqlDataAdapter对象
//构造方法
public SqlData()
{
//
// TODO: 在此处添加构造函数逻辑
//
sqlCon = new SqlConnection(ConfigurationManager.AppSettings["strCon"]);
sqlCon.Open();//打开连接
}
//执行SQL语句
public bool ExceSQL(string strSqlCom)//该方法用来执行SQL语句
{
sqlCmd = new SqlCommand(strSqlCom, sqlCon);
try
{
sqlCmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
throw (new Exception(e.Message));
return false;
} finally
{
sqlCon.Close();
}
}
//返回一个DataSet类型数据
public DataSet ExceDS(string strCom)//该方法返回一个DataSet类型
{
try
{
sqlCmd = new SqlCommand(strCom, sqlCon);
sda = new SqlDataAdapter();
sda.SelectCommand = sqlCmd;
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
finally
{
sqlCon.Close();
}
} public DataSet ExceDS2(string strCom, string TableName)
{
try
{
SqlDataAdapter sda = new SqlDataAdapter(strCom, sqlCon);
DataSet ds = new DataSet();
sda.Fill(ds, TableName);
return ds;
}
finally
{
sqlCon.Close();
}
}
//返回一个SqlDataReader类型数据 public SqlDataReader ExceRead(string strCom)//该方法返回一个SqlDataReader类型
{
sqlCmd = new SqlCommand(strCom, sqlCon);
SqlDataReader sdr = sqlCmd.ExecuteReader();
return sdr;
sqlCon.Close();
}
}
//配置文件web.config的部分内容:
<configuration>
<appSettings>
<add key="ConnectionString" value="server=localhost;database=db_news;uid=sa;pwd=;"/>
</appSettings>
</configuration>
解决方案 »
- 再来 有份
- 有用ASP.NET做的邮件系统吗?
- 面向对象编程要点
- 高分求,“滑块效果”代码要JS的代码 100分 答谢大侠了。
- (经典)怎么能将treeview 和 iframe 关联起来呢?点击节点总是在新窗口打开
- 如何在C#取showmodeless打开时窗口传递的参数?
- 可用JAVASCRIPT的代码来控制后台生成的.net代码吗?
- 怎么样可以做到用客户端脚本激活服务端脚本?
- 想在用户点击DropDownlist的下拉按钮时弹出一个Calender控件,选择日期之后,将该日期显示于DropDownlist中,该如何实现?
- 请进来看看,数据库中的图片如何邦定到datalist的Itemtemplate中?
- GridView 问题
- 配置节 configSections 问题
ConfigurationManager.AppSettings["ConnectionString"]
<add key="ConnectionString" value="server=localhost;database=db_news;uid=sa;pwd=;"/>
这两个名字得一样啊
建议单步调试一下。。
这样你有助于你能理解asp.net他的调用过程对于新手来说