Access database Operations Classes

Source: Internet
Author: User

Access database Operations Class Accesshelper

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Data.OleDb;
Using System.Collections;
<summary>
Summary description of Accehelper
</summary>
public static Class Accesshelper
{
Database connection string
public static readonly String conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + HttpContext.Current.Request.PhysicalApplicationPath + system.configuration.configurationmanager.connectionstrings["ConnectionString"]. ConnectionString;
Hash table for caching parameters
private static Hashtable Parmcache = hashtable.synchronized (New Hashtable ());
<summary>
A given connected database executes an SQL command with a hypothetical parameter (does not return a dataset)
</summary>
<param name= "connectionString" > a valid connection string </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> the number of rows affected by the execution of the command </returns>
public static int ExecuteNonQuery (string connectionString, String cmdtext, params oledbparameter[] commandparameters)
{
OleDbCommand cmd = new OleDbCommand ();
using (OleDbConnection conn = new OleDbConnection (connectionString))
{
PrepareCommand (CMD, conn, null, Cmdtext, commandparameters);
int val = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
return Val;
}
}
<summary>
Executes an SQL command with an existing database connection (does not return a dataset)
</summary>
<remarks>
Example:
int result = ExecuteNonQuery (connstring, "Publishorders", New OleDbParameter ("@prodid", 24));
</remarks>
<param name= "Conn" > an existing database connection </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> the number of rows affected by the execution of the command </returns>
public static int ExecuteNonQuery (OleDbConnection connection, string cmdtext, params oledbparameter[] Commandparameters )
{
OleDbCommand cmd = new OleDbCommand ();
PrepareCommand (cmd, connection, NULL, Cmdtext, commandparameters);
int val = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
return Val;
}
<summary>
Executes an SQL command using an existing SQL transaction (does not return a dataset)
</summary>
<remarks>
Example:
int result = ExecuteNonQuery (trans, "Publishorders", New OleDbParameter ("@prodid", 24));
</remarks>
<param name= "Trans" > An existing transaction </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> the number of rows affected by the execution of the command </returns>
public static int ExecuteNonQuery (OleDbTransaction trans, string cmdtext, params oledbparameter[] commandparameters)
{
OleDbCommand cmd = new OleDbCommand ();
PrepareCommand (cmd, trans.) Connection, trans, Cmdtext, commandparameters);
int val = cmd. ExecuteNonQuery ();
Cmd. Parameters.clear ();
return Val;
}
<summary>
Executes a SQL command that returns a dataset with a database connection that is executed
</summary>
<remarks>
Example:
OleDbDataReader r = ExecuteReader (connstring, "Publishorders", New OleDbParameter ("@prodid", 24));
</remarks>
<param name= "connectionString" > a valid connection string </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> Reader </returns> containing results
public static OleDbDataReader ExecuteReader (String connectionString, String cmdtext, params oledbparameter[] Commandparameters)
{
Create a SqlCommand object
OleDbCommand cmd = new OleDbCommand ();
Create a SqlConnection object
OleDbConnection conn = new OleDbConnection (connectionString);
Here we execute the SQL text command/stored procedure with a try/catch structure, because if this method produces an exception we want to close the connection because there is no reader present,
So Commandbehaviour.closeconnection will not execute
Try
{
Calling the PrepareCommand method, setting parameters on the SqlCommand object
PrepareCommand (CMD, conn, null, Cmdtext, commandparameters);
Call the SqlCommand ExecuteReader method
OleDbDataReader reader = cmd. ExecuteReader (commandbehavior.closeconnection);
Clear parameters
Cmd. Parameters.clear ();
return reader;
}
Catch
{
Close connection, throw exception
Conn. Close ();
Throw
}
}
<summary>
Returns a DataSet data set
</summary>
<param name= "connectionString" > a valid connection string </param>
<param name= "cmdtext" > stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> data set containing results </returns>
public static DataSet ExecuteDataset (String connectionString, String cmdtext, params oledbparameter[] Commandparameters )
{
Creates a SqlCommand object and initializes it
OleDbCommand cmd = new OleDbCommand ();
using (OleDbConnection conn = new OleDbConnection (connectionString))
{
PrepareCommand (CMD, conn, null, Cmdtext, commandparameters);
Creating SqlDataAdapter objects and datasets
OleDbDataAdapter da = new OleDbDataAdapter (cmd);
DataSet ds = new DataSet ();
Try
{
Fill DS
Da. Fill (DS);
Clear the parameter collection for CMD
Cmd. Parameters.clear ();
Back to DS
return DS;
}
Catch
{
Close connection, throw exception
Conn. Close ();
Throw
}
}
}
<summary>
Executes a command with the specified database connection string and returns the first column of a dataset
</summary>
<remarks>
For example:
Object obj = ExecuteScalar (connstring, "Publishorders", New OleDbParameter ("@prodid", 24));
</remarks>
<param name= "connectionString" > a valid connection string </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> convert the type to the desired </returns> with Convert.to{type}
public static Object ExecuteScalar (String connectionString, String cmdtext, params oledbparameter[] commandparameters)
{
OleDbCommand cmd = new OleDbCommand ();
using (OleDbConnection connection = new OleDbConnection (connectionString))
{
PrepareCommand (cmd, connection, NULL, Cmdtext, commandparameters);
Object val = cmd. ExecuteScalar ();
Cmd. Parameters.clear ();
return Val;
}
}
<summary>
Executes a command with the specified database connection and returns the first column of a dataset
</summary>
<remarks>
For example:
Object obj = ExecuteScalar (connstring, "Publishorders", New OleDbParameter ("@prodid", 24));
</remarks>
<param name= "Conn" > an existing database connection </param>
<param name= "CommandText" > Stored procedure name or SQL command statement </param>
<param name= "Commandparameters" > The set of parameters used to execute the command </param>
<returns> convert the type to the desired </returns> with Convert.to{type}
public static object ExecuteScalar (OleDbConnection connection, string cmdtext, params oledbparameter[] Commandparameters)
{
OleDbCommand cmd = new OleDbCommand ();
PrepareCommand (cmd, connection, NULL, Cmdtext, commandparameters);
Object val = cmd. ExecuteScalar ();
Cmd. Parameters.clear ();
return Val;
}
<summary>
To add a parameter collection to the cache
</summary>
<param name= "CacheKey" > Add to Cached Variables </param>
<param name= "Cmdparms" > A collection of SQL parameters that will be added to the cache </param>
public static void Cacheparameters (string cachekey, params oledbparameter[] commandparameters)
{
Parmcache[cachekey] = commandparameters;
}
<summary>
Retrieve Cached Parameter Collection
</summary>
<param name= "CacheKey" > keywords for retrieving parameters </param>
<returns> Cached parameters Set </returns>
public static oledbparameter[] Getcachedparameters (string cachekey)
{
Oledbparameter[] cachedparms = (oledbparameter[]) Parmcache[cachekey];
if (cachedparms = null)
return null;
oledbparameter[] clonedparms = new Oledbparameter[cachedparms.length];
for (int i = 0, j = cachedparms.length I < J; i++)
Clonedparms = (oledbparameter[]) ((icloneable) cachedparms). Clone ();
return clonedparms;
}
<summary>
Prepare to execute a command
</summary>
<param name= "cmd" >sql command </param>
<param name= "conn" >sql connection </param>
<param name= "Trans" >sql Affairs </param>
<param name= "cmdtext" > Command text, for example: Select * from products</param>
<param name= "cmdparms" > Execute command Parameters </param>
private static void PrepareCommand (OleDbCommand cmd, OleDbConnection conn, oledbtransaction Trans, string cmdtext, OLEDBP Arameter[] cmdparms)
{
Determine the status of the connection. If the state is turned off, open the
IF (Conn. State!= ConnectionState.Open)
Conn. Open ();
Cmd property Assignment
Cmd. Connection = conn;
Cmd.commandtext = Cmdtext;
Do you need to use transaction processing
if (trans!= null)
Cmd. Transaction = trans;
Cmd.commandtype = CommandType.Text;
Add the stored procedure parameters required by cmd
if (cmdparms!= null)
{
foreach (OleDbParameter parm in cmdparms)
Cmd. Parameters.Add (Parm);
}
}
}



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.