C # What to do when accessing data (4)

Source: Internet
Author: User
Tags oracleconnection

Saturday. When I got home, I washed a bunch of clothes first, but my hands could not be frozen, but it was quite a sense of accomplishment!

Next we chatted yesterday.

I mentioned in the previous article that 'father 'left some things for the 'son' To Do, right. Even if 'son' is to do things, there is no modernization tool'

In modern society, it is so easy. To do this, you must first sharpen your tools. OK. Let's talk about tools first.

Today we will talk about two basic database classes: dataaccess and tabledataaccess <t, u>.

The first class is very simple, that is, passing in a connection string and then generating a connection:

Public static class dataaccess
{
Public static string connectionstring;

Public static oracleconnection getconnection ()
{
If (connectionstring = NULL)
{
Throw new applicationexception ("Please provide a database connection string ");
}
Return new oracleconnection (connectionstring );
}
}

 

Of course, this dataaccess class is written in many ways and can be written according to your own habits, such as the single-piece mode. ", this

Class.

Tabledataaccess <t, u> is a generic class, where T is restricted to tableitem,

U is restricted to tablecollection <t> [T is tableitem]. If you are not familiar with generic and generic constraints, refer to other materials. Here I

Let's not talk about it. OK, let's take a look at the specific implementation:

 

Public abstract class tabledataaccess <t, u>
Where T: tableitem
Where u: tablecollection <t>
{}

 

What this abstract class does is to retrieve data and store data. In fact, it only sorts data sets. The real implementation is also a subclass.

Let's take a look at the following data:

 

Public void getdata (u Data)
{
// Clear existing data
Data. Clear ();
// Here I will use ORACLE as an example. If sqlsvr is used, it will be sqldatareader. The same is true below.
// Obtain the data reader.
Oracledatareader reader = getreader ();
While (reader. Read ())
{
T item = getitemfromreader (Reader );
Data. Add (item );
}
Data. acceptchanges ();
Reader. Close ();
}

Add the Retrieved Data to tablecollection. This tablecollection is an object, implemented on the user end, and then

The user end saves the data in this entity to the database. This is a problem to be discussed later. For data. acceptchanges ()

Let's look at what we have mentioned before, so we will not be so embarrassed.

Let's look at another method to save the data:

 

Public void savedata (u Data)
{
Oracleconnection conn = dataaccess. getconnection ();
Conn. open ();

// Classify each data entry, whether to add, delete, or modify it
Foreach (T item in data. allitems ())
{
Oraclecommand cmd = NULL;
If (item. isdeleted &&! Item. isnew)
{
Cmd = getdeletecommand (item, Conn );
}
Else if (item. isnew &&! Item. isdeleted)
{
Cmd = getinsertcommand (item, Conn );
}
Else if (item. isdirty)
{
Cmd = getupdatecommand (item, Conn );
}

If (CMD! = NULL)
{
Cmd. executenonquery ();
}
}
Conn. Close ();
}

 

Speaking of this, the strange thing is, where are several getxxxcommand (item, Conn) implemented? Undoubtedly, it is in the subclass:

 

Protected abstract oracledatareader getreader ();

Protected abstract t getitemfromreader (oracledatareader reader );

Protected abstract oraclecommand getupdatecommand (T item, oracleconnection conn );

Protected abstract oraclecommand getinsertcommand (T item, oracleconnection conn );

Protected abstract oraclecommand getdeletecommand (T item, oracleconnection conn );

 

This should be the basic principle in the current development method-separation of design and implementation.

Of course, you can also use interfaces to implement isolation. You may wish to give it a try. I will also do it. Let's take a look at it later ~

Okay. So far, all the basic classes have been finished. You can create a table to see if you want to [do whatever you want ]!

The following section describes how to create an instance.

Let's talk about it today. You can make common progress by making comments at will !! Haha ....

 

 

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.