Class library. cs code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Data.OleDb;///< Summary>///myclass Summary description///</summary>///namespace test{public class Supercnn {public Oledbconnec tion CNN; Private OleDbCommand cmd; Private OleDbDataReader Datar; Public supercnn (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 (_path); CNN = new OleDbConnection (Str_conn); } public void Open () {CNN. Open (); } public void Close () {CNN. Close (); } Public OleDbDataReader Getdatar (string _sql) {cmd = new OleDbCommand (_sql, CNN); Datar = cmd. ExecuteReader (); return Datar; } }}
The. aspx code is as follows:
<%@ page language= "C #" autoeventwireup= "true" codefile= "SuperCnn_test.aspx.cs" inherits= "Supercnn_test"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "btn_test" runat= "Server" text= "Test supercnn"
onclick= "Btn_test_click"/>
</div>
</form>
</body>
The. Aspx.cs code is as follows:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using test;using System.data.oledb;public Partial class supercnn_test:system.web.ui.page{ protected void Page_Load (object sender, EventArgs e) { } protected void Btn_test_click (object sender, EventArgs e) { supercnn cnn = new Supercnn ("Data.mdb") ; Cnn.open (); OleDbDataReader Datar = Cnn.getdatar ("SELECT * from T_imgs"); while (Datar. Read ()) { Response.Write (datar["Img_url"). ToString () + "<br>"); } Cnn.close (); }}
The effect is as follows:
ASP. NET Custom database connection class (OLE DB)