Introduction to Data
The last article in this series. How to describe the first part, two parts how to obtain the description. Now we're going to create the DAL library to make our title workable.
Design the DAL library
The class library that I want to create supports SQL Server and OLE DB. I divided the library into the following sections:
Utility classes
Class Dalquerybuilder
Generates an SQL statement to update an object.
Class Dalparameter
The build parameters are saved in the stored procedure.
Class Dalexception
Inherited from System.Exception, more information will be provided when there is an exception to the database.
Attribute classes
See the first article.
DAL itself
Class Dalengine
This abstract class is used for database operations and is simpler for database programs. Its virtual and abstract methods have different implementations.
Class Dalsqlengine
Class Daloledbengine
Columbine One eye Dalengine class
Public abstract class Dalengine:idisposable
{
//
Private data members
//
IDbConnection conn = null;
String connectionString = "";
ArrayList parameters = new ArrayList ();
bool Canclose = true;
Constructor
Public Dalengine (string connectionString);
public bool Canclose;
public string ConnectionString;
//
Methods that must is override with a specific data provider
Implementation please implementation of Dalsqlengine
or Daloledbengine
//
Protected abstract IDbConnection getconnection ();
Protected abstract IDbCommand CreateCommand (string spname);
public abstract void Execsp_dataset (string spname, DataSet dataset,
String tablename);
public abstract void Execquery_dataset (string query, DataSet dataset,
String tablename);
//
Related to stored procedure parameters
//
Public Dalparameter getparameter (string name);
void Updateoutputparameters (IDbCommand cmd);
public void Addparameter (Dalparameter param);
public void clearparameters ();
//
For those this use stored procedures
//
Public IDataReader Execsp_datareader (string spname);
Public IDataReader Execsp_datareader (string spname,
CommandBehavior behavior);
public Object Execsp_scalar (string spname);
public int Execsp_nonquery (string spname);
//
Methods for those this use plain SQL statements
//
Public IDataReader Execquery_datareader (string query,
CommandBehavior behavior);
Public IDataReader Execquery_datareader (string query);
public object Execquery_scalar (string query);
public int Execquery_nonquery (string query);
//
Business Objects Methods
//
public static object Createfromreader (IDataReader Reader, Type ObjType);
public Object Retrieveobject (object KeyValue, Type objType);
public int Retrievechildobjects (object foreignkeyvalue, ArrayList objects,
Type Childtype);
void Updateobjectsql (Object o, Datatableattribute dataTable);
void Updateobjectstoredprocedure (Object o, Datatableattribute dataTable);
public void Updateobject (object o);
public void Updateobjects (IEnumerable enumobjects);
}
public class Dal:dalsqlengine
{
Const string conn_string = "server=localhost;uid=sa;pwd=;d atabase=pubs";
Public DAL (): Base (conn_string)
{
}
Public ArrayList getcustomerdependents (customer customer)
{
ArrayList result = new ArrayList ();
}
Finally
{
Dal. Dispose ();
}
}
Conclusion
There are many limitations, we need to further implement thinking, but after you understand these, we can conduct nhibernate understanding research and application, wish you good luck.
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.