程序总是出现“ConnectionString 属性尚未初始化”咋回事阿?
前台代码是这样的:(只是放了个DataList控件)
<%@ Page Language= "C# "Debug=true AutoEventWireup= "true " CodeFile= "Default2.aspx.cs " Inherits= "Default2 " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataList ID= "DataList2 " runat= "server ">
</asp:DataList> </div>
</form>
</body>
</html>
后台代码如下 :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
DB sdb = new DB();
DataList2.DataSource = sdb.dt( "select * from student ");
DataList2.DataKeyField = "no ";
DataList2.DataBind();
Response.Write( "绑定2成功 ");
}
}
其中用到的类文件代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
/// <summary>
/// DB 的摘要说明
/// </summary>
public class DB
{
public static string strConnection;
private int pagesize;
public DB()
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source= ";
//strConnection+=
strConnection += "F:/我的作品/aspnet2005/DataList/App_Data/temp.mdb "; //*就是数据库的名字
OleDbConnection conn = new OleDbConnection(strConnection);
conn.Open();
//
// TODO: 在此处添加构造函数逻辑
//
}
//根据传入的SQL语句 返回一个数据表(name)
public DataTable dt(string query)
{
OleDbConnection con = new OleDbConnection(strConnection);
OleDbDataAdapter oda = new OleDbDataAdapter(query, con);
DataSet ds = new DataSet();
oda.Fill(ds, "name ");
return ds.Tables[ "name "];
}
程序运行时总是抱错,怎么回事啊 ,高手指点阿!!!!!!
[解决办法]
public static string strConnection;
private int pagesize;
public DB()
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source= ";
前面定义了静态字符串strConnection,在函数里就不用再定义了,直接使用strConnection就可以了,去掉string
[解决办法]
“ConnectionString 属性尚未初始化”的另类解决办法
现在稍微熟悉Asp.net的朋友都习惯把数据库连接配置写到web.config中,这样的优点主要是能随时更改数据库配置(比如帐号密码)而不用再编译,web.config中的数据库中的配置如下:
1 <appSettings>
2 <add key= "ConnString " value= "user id=sa;password=sasasa;Data Source=(local);Initial Catalog=51aspx "/>
3 </appSettings>
.cs文件中利用
string strConn = ConfigurationManager.AppSettings[ "ConnString "];
来读取,值得注意的是:这个是Asp.net2.0的读取方式,1.1中是
string strConn=ConfigurationSettings.AppSettings[ "ConnString "];
很多朋友也许都遇到过“ConnectionString 属性尚未初始化”的提示,其中的根本原因是没有能读到key的值,解决方法是校正一下ConnString。
我今天也遇到了这个提示“ConnectionString 属性尚未初始化”,死活读不到web.config中的key值。
最后发现原因竟然是我当前建立的是http://localhost/test虚拟目录,默认读取的是站点http://localhost中的web.config的值,那里根本没有这个key,把test设置为站点(重新建立一个站点指向test或者把http://locanhost指向test)就OK了,希望这里能给大家提个醒,少走弯路哦!
http://www.cnblogs.com/liudao/archive/2007/05/15/747148.html