[ASP.net (C #)] Custom Database operations Class (i)

Source: Internet
Author: User
Tags ole tostring
asp.net| Data | Database The two days have written a class. Written for SQL Server and OLE databases. The source code is as follows:

\classes\dbcontrol.cs
==========================================================

Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Data.OleDb;

Namespace Guestbook
{
<summary>
A summary description of the Dbcontrol.
</summary>
public class DbControl:Classes.DataBaseType.DbOpen
{
The definition of an employee.
private int record_total = 0;
protected string Dbtype,sql_select;
protected SqlConnection sqlconn;
protected OleDbConnection oleconn;
protected SqlCommand SQLCMD;
protected OleDbCommand Olecmd;
Protected DataSet ds = new DataSet ();

public int Recordtotal
{
Get
{
Switch (DBTYPE)
{
Case "SQL":
Sqlcmd.cancel ();
Sqlcmd.commandtext = Sql_select;
SqlDataReader Sqldr;
Sqldr = Sqlcmd.executereader ();
while (Sqldr.read ())
{
record_total++;
}
Sqldr.close ();
Break
Case "OLE":
Olecmd.cancel ();
Olecmd.commandtext = Sql_select;
OleDbDataReader Oledr;
Oledr = Olecmd.executereader ();
while (Oledr.read ())
{
record_total++;
}
Oledr.close ();
Break
}
return record_total;
}
}

Public Dbcontrol (String dbtype,string dbname)
{
Re-loading the structure function.
DBTYPE = Dbtype.toupper ();
Switch (Dbtype.toupper ())
{
Case "SQL":
Sqlconn = this. SqlConnect (dbname);
Oleconn.close ();
Oleconn.dispose ();
Break
Case "OLE":
Oleconn = this. Oleconnect (dbname);
Oleconn.close ();
Oleconn.dispose ();
Break
}
}

Public Dbcontrol (): Base ()
{
//
TODO: Add the code for the build function here
//
}

public void Open (String dbtype,string dbname)
{
The data in the library is open.
DBTYPE = Dbtype.toupper ();
Switch (Dbtype.toupper ())
{
Case "SQL":
Sqlconn = this. SqlConnect (dbname);
Break
Case "OLE":
Oleconn = this. Oleconnect (dbname);
Break
}
}

Public SqlDataReader Sqlgetreader (string strquery)
{
Returns a SqlDataReader. For SQL Server
Sql_select = strquery;
SQLCMD = new SqlCommand (strquery,sqlconn);
SqlDataReader Dr;
Try
{
SqlCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
Dr = Sqlcmd.executereader ();
Return Dr;
}

Public OleDbDataReader Olegetreader (string strquery)
{
Returns a OleDbDataReader. for OLE DB.
Sql_select = strquery;
Olecmd = new OleDbCommand (strquery,oleconn);
OleDbDataReader Dr;
Try
{
OleCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
Dr = Olecmd.executereader ();
Return Dr;
}

public int Sqlruncommand (string strquery)
{
Execute a SQL sentence. Includes recording inserts, updates, and erasure. For SQL Server
Sql_select = strquery;
SQLCMD = new SqlCommand (strquery,sqlconn);
Try
{
SqlCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
return Sqlcmd.executenonquery ();
}

public int Oleruncommand (string strquery)
{
Execute a SQL sentence. Includes recording inserts, updates, and erasure. for OLE DB.
Sql_select = strquery;
Olecmd = new OleDbCommand (strquery,oleconn);
Try
{
OleCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
return Olecmd.executenonquery ();
}

Public DataView Sqlgetdataset (string strquery)
{
Returns a DataSet. For SQL Server
Sql_select = strquery;
SQLCMD = new SqlCommand (strquery,sqlconn);
Try
{
SqlCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
SqlDataAdapter da = new SqlDataAdapter ();
Da. SelectCommand = SQLCMD;
Da. Fill (ds, "defaulttable");
Return DS. tables["Defaulttable"]. DefaultView;
}

Public DataView Olegetdataset (string strquery)
{
Returns a DataSet. for OLE DB.
Sql_select = strquery;
Olecmd = new OleDbCommand (strquery,oleconn);
Try
{
OleCmd.Connection.Open ();
}
catch (Exception e)
{
Throw e;
}
OleDbDataAdapter da = new OleDbDataAdapter ();
Da. SelectCommand = Olecmd;
Da. Fill (ds, "defaulttable");
Return DS. tables["Defaulttable"]. DefaultView;
}

public void Close ()
{
The data library closes.
Switch (DBTYPE)
{
Case "SQL":
Sqlcmd.cancel ();
Sqlcmd.dispose ();
Sqlconn.close ();
Sqlconn.dispose ();
Break
Case "OLE":
Olecmd.cancel ();
Olecmd.dispose ();
Oleconn.close ();
Oleconn.dispose ();
Break
}
Ds. Clear ();
Ds. Dispose ();
}
}
}

============================================================

\classes\databasetype\dbopen.cs
============================================================

Using System;
Using System.Data.SqlClient;
Using System.Data.OleDb;
Using System.Configuration;

Namespace GuestBook.Classes.DataBaseType
{
<summary>
A summary description of the dbopen.
</summary>
public class dbopen
{
Public dbopen ()
{
//
TODO: Add the code for the build function here
//
}

Protected SqlConnection SqlConnect (string dbname)
{
SqlConnection conn = new SqlConnection (Configurationsettings.appsettings[dbname]);
Return conn;
}

Protected OleDbConnection Oleconnect (string dbname)
{
OleDbConnection conn = new OleDbConnection (Configurationsettings.appsettings[dbname]);
Return conn;
}
}
}

==============================================================

The calling method for the class:
==============================================================

Dbcontrol Objdbcontrol = new Dbcontrol ();
String SQL = "SELECT * from Content";

Objdbcontrol.open ("SQL", "SqlConnection");
Dglist.datasource = Objdbcontrol.sqlgetdataset (SQL);
inttotal = Objdbcontrol.recordtotal;
Dglist.databind ();
Objdbcontrol.close ();

==============================================================
of which "inttotal = Objdbcontrol.recordtotal;" The total number of records is returned.
You can insert it into the DataGrid. Such as:
==============================================================

private void Dglist_itemcreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype = = Listitemtype.pager)
{
System.Text.StringBuilder pagerstring = new System.Text.StringBuilder ();
Pagerstring.append ("Overall" + inttotal.tostring () + "Total" + dglist.pagecount + "page per page" + dglist.pagesize + "pen");
E.item.cells[0]. Controls.addat (0,new LiteralControl (pagerstring.tostring ()));
}
}



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.