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 (): 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 ();
}
}
}
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
//
}
==============================================================
of which "inttotal = Objdbcontrol.recordtotal;" The total number of records is returned.
You can insert it into the DataGrid. Such as:
==============================================================
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.