本文主要是介绍总算找到了问题的原因了,爽,不过感觉自己太粗心了。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这段代码一直找不到什么地方错了,编译能通过,但是就是没有办法取得数据库中的值,十分郁闷。
到csdn上拜了个高手为师,结果都是指出了一些无关紧要的错误,不过还是得到了一些检查bug的基本方法。
最后在查网页信息的时候,对照人家写的连接Access的源代码一条一条看,才发现原来是红色部分出了问题,而且是两个问题:
1.provider的值给错了Microsoft.Jet.OleDb.4.0写成了Microsoft.Jet.Ole.4.0
2.Server.MapPath(("hqcs.mdb")取出的值居然是D:/hqcs/page/hqcs.mdb,而实际的路径是D:/hqcs/DataSource/hqcs.mdb,这个问题我现在还没找到原因,msdn上关于server.mappath()有好几个版本,我还不能确定到底应该使用那个版本。
namespace hqcs.ascx
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//database
using System.Data.OleDb;
using System.Data.SqlClient;
//Global funstions
//using hqcs.Global;
/// <summary>
/// Summary description for hqcsNavigationBar.
/// </summary>
public class dtgNavigationBar : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid dtgNBar;
protected System.Web.UI.WebControls.Label Label1;
private OleDbConnection conNBar;
private OleDbCommand cmdNBar;
private OleDbDataAdapter dapNBar;
private DataSet dstNBar;
private DataView dtvNBar;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//connect to database
//string strConnAccess = "Provider = Microsoft.Jet.Ole.4.0;Data Source = " + Server.MapPath("hqcs.mdb");
string strConnAccess = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnAccess += "Data Source = D://hqcs//DataSouce//hqcs.mdb";
conNBar = new OleDbConnection();
conNBar.ConnectionString = strConnAccess;
try
{
conNBar.Open();
//cmdNBar = new OleDbCommand("Select * from hqcs_NavigationBarItems",conNBar);
cmdNBar = new OleDbCommand();
cmdNBar.CommandText = "SELECT * FROM hqcs_NavigationBarItems";
cmdNBar.Connection = conNBar;
dapNBar = new OleDbDataAdapter(cmdNBar);
dstNBar = new DataSet();
dapNBar.Fill(dstNBar,"NavigationBar");
dtvNBar = dstNBar.Tables["NavigationBar"].DefaultView;
int nRecords = 0;
nRecords = dstNBar.Tables["NavigationBar"].Rows.Count;
Label1.Text = nRecords.ToString();
dtgNBar.DataSource = dtvNBar;
dtgNBar.DataBind();
conNBar.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//this.ID = "dtgNBar";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
接下来我想对代码进行一些优化,把连接数据库的字段写到web.config中去。
在web.config中添加了以下代码:
<appSettings>
<!-- the connect to database-->
<!-- connect to Access database -->
<add key = "connAccess" value = "Provider = Microsoft.Jet.OleDb.4.0; Data Source =D:/hqcs/DataSouce/hqcs.mdb"></add>
<!--connect to mssql2000-->
<add key="conSql" value="server=Localhost;UID=sa;PWD=sa; database=hqcs;"></add>
</appSettings>
然后再代码中去除原来的字段,加入以下代码:
using System.Configuration;
..............
/*
string strConnAccess = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnAccess += @"Data Source = D:/hqcs/DataSouce/hqcs.mdb";
*/
string strConnAccess = ConfigurationSettings.AppSettings["connAccess"];
用来获得连接数据库的string。
这篇关于总算找到了问题的原因了,爽,不过感觉自己太粗心了。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!