ASP. NET Custom database connection class (OLE DB)

Source: Internet
Author: User
Tags ole

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.