Application for object-oriented in database applications (DOTNET)

xiaoxiao2021-03-06  39

Application for object-oriented in database applications (DOTNET)

A large part of the current application is a program related to the database, and the writing database program involves many data tables, access, and manipulating data tables that form the most common action of the database application, so write an efficient program for the program. It is said that it has to be considered. This article will discuss this topic and hope to help readers.

Object-oriented is a general programming idea of ​​today's program, he has three most basic features: packages, inheritance and polymorphism. Inheritance is very effective for the multiplexing of the code, the polymorphism is a variety of forms of function (method), and can change the behavior of the object through the method of rewriting the parent class, and play a very important role in object-oriented programming. . So, can you play in practical applications? Perhaps the use of examples can best express this idea.

For example, we have to write a simple forum program. After analysis, we can get the table below: Users, Tablets (BBSBLOCK), Reply, Topic, etc. (for convenient code Inherit, I set the ID number of all tables into the same name: ID). For these tables, there are some same operations: browsing, deleting, adding, and modifying. So, is we implemented for each table? This method is awkward. Inherit, it will play an extremely important role here. Thought is: Write a parent class, write these basic operations, then abstract each table into a class and inherit the parent class just created. At this time, all subclasses have these basic operations.

We can define the parent class like this:

Public Class Dbbaseclass

{

protected string Tablename; // Table name

Protected SqlConnection Con; // Connection Object

Public dbaseclass (): this ("users")

{

}

///

/// Constructor

///

/// Table name

Public dbbaseclass (String Tablename)

{

THIS.TABLENAME = TABLENAME;

IF (con == null)

{

Con = New SqlConnection ("Server = ACCP-LZH; Database = MISSBBS; UID = SA; PWD = SA");

}

Else

{

IF (con.state == connectionState.open)

C. close ();

}

}

///

/// Get the data set

///

/// If count is 0, get all the datasets, otherwise get the records of the specified number (from the top)

/// Return Dataset

Public DataSet Select (int Count)

{

String SQL;

IF (count == 0)

SQL = "Select * from" this.tablename; Else

SQL = "SELECT TOP" count.toString () "* from" this.tablename "Orader By ID DESC";

Sqlcommand selectcmd = new SQLCOMMAND (SQL, CON);

SqldataAdapter adapter = new sqldataadapter ();

Adapter.selectCommand = SELECTCMD;

DataSet DS = New DataSet ();

Try

{

C.Open ();

Adapter.Fill (DS, "BBSTABLE");

C. close ();

}

Catch (Exception)

{

Return NULL;

}

Return DS;

}

}

In this class, we define two overloaded constructor and a method for obtaining the data set, and define two fields that have a large field, one is a table name, one is a connection object. When other classes inherit this class, they no longer need to define the tables and connection objects, the most important thing, these two fields have played a key role for our better implementation.

Next, we create a subclass: users. This class is an abstraction of tables for tables:

Public Class User: DBBaseClass

{

///

/// No argument constructor

///

Public user (): Base ("users")

{

}

///

/// Constructor

///

/// Table name

Public user (String Tablename): Base (TableName)

{

}

}

Now, everyone can see that we have written the two constructor of such a class, which has the function of returning all the datasets in the table because the table inherits dbbaseclass.

Similarly, we write a subclass: Topic, this class is an abstraction of Topic.

Public Class Topic: DBBaseClass

{

Public Topic (): Base ("Topic")

{

}

Public Topic (String TableName): Base (TableName)

{

}

}

Like User, the class also has functions that return all datasets.

When instances, use a simple object factory design mode to return different types of objects.

Public Class Factory

{

Public factory () PUBLIC FACTORY

{

}

Public static dbbaseclass getObject (String TableName)

{

Switch (TableName)

{

Case "Users":

Return New User ();

Case "Topic":

Return new Topic ();

Case "bbsblock":

Return new bbsblock ();

Case "reply":

Return new reply ();

Case "bbsmaster": return new bbsmaster ();

DEFAULT:

Return new dbbaseclass ();

}

}

}

Let's take a look at how to use:

User User = (user) factory.getObject ("Users");

DataSet DS1 = New Dataset ();

DS1 = User.select (0);

Topic Topic = (Topic) Factory.GetObject ("Topic");

DataSet DS2 = New Dataset ();

DS2 = Topic.select (0);

After reading it, what do you think? If you are an experienced programmer, this method will definitely use it, if you have just contacted, understand this idea is still a great advantage.

转载请注明原文地址:https://www.9cbs.com/read-118582.html

New Post(0)