Home > Net >  How to make a new class can support
How to make a new class can support

Time:09-24

Have been writing the management system, the need to deal with a database to read and write for a long time, before the use of all is the old-fashioned way, suddenly found common libraries such as sqlhelper recently, make several use, also very good,

But now I have a idea, these libraries are not need new can use, I want to change to need new, then the input parameters, so that I can define a number, a program can be convenient access to multiple databases,

The source code is as follows:

 using System; 
using System.Data;
Using System. The Configuration. Assemblies;
using System.Configuration;
using System.Web;
Using System. The Data. The OleDb;


The namespace draw texturing machine process display system
{
///
///AccessHelper summary description
///

Public class AccessHelper
{
Protected the static OleDbConnection conn=new OleDbConnection ();
Protected the static OleDbCommand comm=new OleDbCommand ();

Public AccessHelper ()
{
//
//TODO: add a constructor logic here
//

}

///
///open the database
///

Private static void openConnection ()
{
If (conn. State==ConnectionState. Closed)
{
Try
{
Conn. The Open ();
}
The catch (Exception e)
{throw new Exception (e.M essage); }

}

}
///
///close the database
///

Private static void closeConnection ()
{
Conn. The ConnectionString=$" Provider=Microsoft. ACE. The OLEDB. 12.0; The Data Source={AppDomain. CurrentDomain. BaseDirectory} \ \ {FileName} accdb; The Jet OLEDB: Database PassWord={"} ";
Comm. Connection=conn;
If (conn. State==ConnectionState. Open)
{
Conn. Close ();
Conn. The Dispose ();
Comm. The Dispose ();
}
}
///
///executing SQL statements
///

///
Public static void excuteSql (string SQLSTR)
{
Try
{
OpenConnection ();
Comm.Com mandType=CommandType. Text;
Comm.Com mandText=SQLSTR;
Comm. ExecuteNonQuery ();
}
The catch (Exception e)
{
Throw new Exception (e.M essage);
}
The finally
{closeConnection (); }
}
///
///returns the specified SQL statements OleDbDataReader object, please pay attention to close the object when using,
///

///
///
Public static OleDbDataReader dataReader (string SQLSTR)
{
OleDbDataReader Dr=null;
Try
{
OpenConnection ();
Comm.Com mandText=SQLSTR;
Comm.Com mandType=CommandType. Text;

Dr=comm. ExecuteReader (CommandBehavior. CloseConnection);
}
Catch
{
Try
{
Dr. Close ();
CloseConnection ();
}
Catch {}
}
The return of Dr;
}
///
///returns the specified SQL statements OleDbDataReader object, please pay attention to when using closed
///

///
///
Public static void dataReader (string SQLSTR, ref OleDbDataReader Dr)
{
Try
{
OpenConnection ();
Comm.Com mandText=SQLSTR;
Comm.Com mandType=CommandType. Text;
Dr=comm. ExecuteReader (CommandBehavior. CloseConnection);
}
Catch
{
Try
{
If (Dr!=null & & ! Dr. IsClosed)
Dr. Close ();
}
Catch
{
}
The finally
{
CloseConnection ();
}
}
}
///
///returns the specified SQL statements dataset
///

///
///
Public static DataSet DataSet (string SQLSTR)
{
The DataSet ds=new DataSet ();
OleDbDataAdapter da=new OleDbDataAdapter ();
Try
{
OpenConnection ();
Comm.Com mandType=CommandType. Text;
Comm.Com mandText=SQLSTR;
Da. SelectCommand=comm;
Da. The Fill (ds);

}
The catch (Exception e)
{
Throw new Exception (e.M essage);
}
The finally
{
CloseConnection ();
}
Return the ds;
}
///
///returns the specified SQL statements dataset
///

///
///
Public static void dataSet (string SQLSTR, ref the dataSet ds)
{
OleDbDataAdapter da=new OleDbDataAdapter ();
Try
{
OpenConnection ();
Comm.Com mandType=CommandType. Text;
Comm.Com mandText=SQLSTR;
Da. SelectCommand=comm;
Da. The Fill (ds);
}
The catch (Exception e)
{
Throw new Exception (e.M essage);
}
The finally
{
CloseConnection ();
}
}
///
///returns the specified SQL statements datatable
///

///
///
Public static DataTable DataTable (string SQLSTR)
{
DataTable dt=new DataTable();
OleDbDataAdapter da=new OleDbDataAdapter ();
Try
{
OpenConnection ();
Comm.Com mandType=CommandType. Text;
Comm.Com mandText=SQLSTR;
Da. SelectCommand=comm;
Da. The Fill (dt);
}
The catch (Exception e)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related