asp.net--自定义数据库连接类(OLEDB)

类库代码superConn.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;

namespace 练习
{
    public class superConn
    {
        private OleDbConnection cnn;
        private OleDbCommand cmd;
        private OleDbDataReader datar;
        public superConn(string mdbFileName){
            string _path = "~\\App_Data\\" + mdbFileName;
            string str_conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
                + System.Web.HttpContext.Current.Server.MapPath(_path);//调用MapPath的完整命名空间
            cnn = new OleDbConnection(str_conn);    
    }
        //打开连接
        public void open() {
            cnn.Open();
        }
        //关闭连接
        public void close() {
            cnn.Close();
        }
        public OleDbDataReader getDateReader(string _sql) {
            cmd = new OleDbCommand(_sql, cnn);
            datar = cmd.ExecuteReader();
            return datar;
        
        }
    }
}
.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="测试自定义数据库连接类.aspx.cs" Inherits="练习.测试自定义数据库连接类" %>

<!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:Button ID="Button1" runat="server" Text="Test" οnclick="Button1_Click" />
    
    </div>
    </form>
</body>
</html>

.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace 练习
{
    public partial class 测试自定义数据库连接类 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            superConn  mycnn= new superConn("data.mdb");
            mycnn.open();
               string _sql="SELECT* FROM T_IMGS";
            OleDbDataReader dr=mycnn.getDateReader(_sql);
            while (dr.Read()) {
                Response.Write(dr["img_url"].ToString()+"<br>"); 
            }
            mycnn.close();
            
        }
    }
}

测试运行时出现如下错误:

<span style="color:#ff0000;">“/”应用程序中的服务器错误。

在 ConnectionString 中未指定 OLE DB 提供程序。示例为:“Provider=SQLOLEDB;”。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentException: 在 ConnectionString 中未指定 OLE DB 提供程序。示例为:“Provider=SQLOLEDB;”。

源错误: 


行 16:             string str_conn = "Provide=Microsoft.Jet.OLEDB.4.0; Data Source="
行 17:                 + System.Web.HttpContext.Current.Server.MapPath(_path);//调用MapPath的完整命名空间
行 18:             cnn = new OleDbConnection(str_conn);    
行 19:     }
行 20:         //打开连接

源文件: C:\Users\Administrator\documents\visual studio 2010\Projects\练习\练习\superConn.cs    行: 18 

堆栈跟踪: 


[ArgumentException: 在 ConnectionString 中未指定 OLE DB 提供程序。示例为:“Provider=SQLOLEDB;”。]
   System.Data.OleDb.OleDbConnectionString.ValidateProvider(String progid) +1044395
   System.Data.OleDb.OleDbConnectionString.ValidateConnectionString(String connectionString) +252
   System.Data.OleDb.OleDbConnectionString..ctor(String connectionString, Boolean validate) +563
   System.Data.OleDb.OleDbConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +36
   System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +150
   System.Data.OleDb.OleDbConnection.ConnectionString_Set(String value) +59
   System.Data.OleDb.OleDbConnection.set_ConnectionString(String value) +4
   System.Data.OleDb.OleDbConnection..ctor(String connectionString) +26
   练习.superConn..ctor(String mdbFileName) in C:\Users\Administrator\documents\visual studio 2010\Projects\练习\练习\superConn.cs:18
   练习.测试自定义数据库连接类.Button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\练习\练习\测试自定义数据库连接类.aspx.cs:20
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
</span>

根据错误提示进行代码检查,发现如下拼写错误:


修改后调试,得到如下正确结果:




发布了500 篇原创文章 · 获赞 123 · 访问量 128万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览