Delphi7 is equivalent to the implementation of the ADO.NET class

zhaozj2021-02-11  196

Delphi7 is equivalent to the implementation of the ADO.NET class

Kaneboy@163.net

Recently, I'm learning .NET, discovery. ADO.NET data components under .NET (mode and philosophy) are much more advanced than ADO, naturally available for Multitier environment, can be easily created at all layers (mainly by inheriting DataSet) DataAccess layer and businessRule layers During the transmissive entity object, and the disconnected connection method can save the database server connection resource, while the client can easily process data in the object.

Since I have been using Delphi, I feel a lot of methods that I feel binding in Delphi. Through ADO.NET, it can be very convenient to solve, so they can be implemented by imitation ADO.NET, in Delphi.

The following is a simple explanation of each unit and all kinds, and do not include all public interfaces in the class. Please see the source code.

First, Datacolumn Unit

A TDATACOLUMNCOLLECTION class, TDATACOLUMNSTATE class, TDATACOLUMNCOLLECTIONSTATE class.

TDATACOLUMN is used to save information (including data) in the data table (including data), a columnName property public column name (custom, different physical field name in the database), fieldname attribute public database, DataType property public field data Type, ORIGINVALUE, OLDVALUE, CURRENTVALUE attributes Open this column initial values ​​(values ​​when extracting data from the database), old value, and current values, the value attribute directly returns CurrentValue, and these attributes return to the value of the Variant type, for easy use , Asstring, asinteger, and other similar properties directly return the value of the corresponding type.

TDATACOLUMNCOLLECTION is a collection class that inherits from TOBJECTLIST to save multiple TDATACOLUMN objects, overloaded the Add, INSERT method to implement type check.

TDATACOLUMNSTATE is an enumeration class that represents a status of TDATACOLUMN, with CsunkNown, Csunchanged, CSModified, which is used to represent unknown, not changed, and have changed three states.

TDATACOLUMNCOLLECTIONSTATE is also an enumeration class, used to represent a status of TDATACOLUMNCOLLECTION, with CCSUnknown, CCSunchanged, CCSModified.

Second, DATAROW unit

Implement TDATAROW class, TDATAROWCOLLECTION class, TDATAROWSTATE class.

TDATAROW is used to save a line of data in the data table, and the columns property is a value of a TDATACOLUMNCOLLECTION type, returns all columns included in this row.

TDATAROWCOLLECTION is a collection class that maintains multiple TDATAROW objects.

The TDATAROWSTATE enumeration class is used to represent a state of a data line, there is RSUnknown, Rsunchanged, Rsnew, RSModified, RSDeleded.

Third, Datacommand Unit and DatacommandParameter Unit

Since Tadocommand failed to meet the requirements, it is inherited to expand its characteristics.

These two units implement TDatacommand class, TDatacommandparameter class, TDatacommandparameterCollection class, TDatacommandparameterType class.

The TDATACMMANDPARAMETER class is used to represent a TDATACMMAND parameter, which is used to assist TDATACMMand's CommandText implementation of various operations. DataType Property Public Parameter Data Type, FieldName Property Open Field Name in the Physical Database, the Name Property Annual Name, the paraMType property is a value of a TDATACMMANDPARAMETYPE enumeration type, which can be ptupdatefield or ptwherefield, indicating that the parameter is an update The value is still a positioning value. TDATACMMANDPARAMETERCOLLECTION is a collection class that represents multiple TDATACMAMANDPARAMETER objects.

TDATACMMAND inherits from Tadocommand, the DataParameters property publishes a value of a TDATACMMANDPARAMETERCOLLECTION type, indicating a set of parameters.

Four, DataTable unit

Implement TDATABLE and TDATATABLECOLLECTION classes.

TDATATABLE represents a collection of data tables, column sets of columns attribute public tables, rows collection of the ROWS attribute public table, newrow () is used to add a TDATAROW belonging to this table and returns, the TableName property returns a table name, bindto () method You can bind this TDATABLE to a TStringGrid control, and another overloaded bindto () binds a column of data to a TStringList.

TDATATABLECOLLECTION is a set class that represents multiple tables.

V. DataseTPLUS unit

A TDataSetPlus class is implemented. (Why not use the name of TDataSet directly? Because you have used yourself in Delphi :)

TDataSetPlus represents a dataset that can contain multiple data tables. The Tables property publishes the TDATABLE objects they contain, and the newTable () method adds a data sheet that belongs to itself.

6. DataAdapter unit

Implement TDataAdapter class to connect TDataSetPlus and databases.

TDataAdapter class, you can populate or update a dataset, selectcommand, insertcommand, updateCommand, deleteCommand attributes to disclose four TDATACMMAND types, used to take data, insert data, update data, and delete data, Fill () method can According to CommandText in SelectCommand, the corresponding data fills in the TDataSetPlus object in the parameter, the update () method can update the TDataSetPlus object in the parameter back to the database according to INSERTCOMMAND, UPDATECMMAND, DeleteCommand.

Finally, we can use this:

VAR

DS: TDataSetPlus;

Ada: TDataAdapter;

Cmdselect, cmdinsert, cmdupdate, cmddelete: tdatacommand;

TADOCONNECTION;

i: integer;

Begin

/ / Define database connections

CONN: = tadoconnection.create (nil);

Conn.connectionstring: = 'file name =. / ADO .UDL';

Conn.loginprompt: = FALSE;

// The following defines four TDATACMMAND, used for SELECT, INSERT, UPDATE, DELETECMDSELECT: = TDATACMMAND.CREATE (NIL);

CMDSelect.Connection: = conn;

cmdselect.commandtext: = 'SELECT UserName, Password from Userdb';

CmdInsert: = TDATACMMAND.CREATE (NIL);

CmdInsert.connection: = conn;

CmdInsert.commandtext: = 'INSERT INTO Userdb (Username, Password) Values ​​(@USERNAME, @password)';

// Add parameters to indicate the value to be updated or positioned.

CmdInsert.dataParameters.add (TDatacommandParameter.create ('@ username', 'username', ftstring));

Cmdinsert.dataParameters.add (TDatacommandParameter.create ('@ Password', 'Password', ftstring);

Cmddelete: = tdatacommand.create (nil);

Cmddelete.connection: = conn;

Cmddelete.comMandtext: = 'delete from userdb where username = @ username';

CMDDelete.dataParameters.add (TDatacommandParameter.create ('@ username', ', ftstring);

cmdupdate: = tdatacommand.create (nil);

CMDUPDATE.CONNECTION: = conn;

cmdupdate.commandtext: = 'Update Userdb Set Password = @ Password Where UserName = @ UserName';

cmdupdate.dataParameters.add (TDatacommandParameter.create ('@ password', 'password', ftstring));

cmdupdate.dataParameters.add (TDatacommandParameter.create ('@ username', 'username', ftstring, ptwherefield);

/ / Define a data adapter to initialize its Command property

Ada: = tdataadapter.create ();

Ada.selectCommand: = cmdselect;

Ada.insertCommand: = cmdinsert;

Ada.UpdateCommand: = cmdupdate;

Ada.deleteCommand: = cmddelete;

/ / Define a data set

DS: = tdatasetplus.create ();

Cn.open ();

// Fill the recordset with an adapter, automatically add a data sheet called 'useertable' to put into the record set

Ada.fill (DS, 'UserTable'); // Demonstrate Data in Record Concentration

For i: = 0 to ds.tables.itembyname ['useertable']. Rows.count-1 DO

Begin

DS.Tables.Itemns. Rows [i] .Columns.ItemByfieldName ['password']. Value: = '12345';

END;

// update the change in the record set back to the database with the adapter

Ada.Update (DS.Tables [0]);

CONN.CLOSE ();

Freeandnil (conn);

Freeandnil (ADA);

Freeandnil (DS);

Freeandnil (cmdselect);

Freeandnil (cmdinsert);

Freeandnil (cmdupdate);

Freeandnil (cmddelete);

END;

Everyone can see, there are many defects, and hidden bugs (I basically didn't do the action of bugs), I just tried the first trial, realized, if there is time, then DEBUG and improvement.

I have already thought about it:

1. Implement CommandBuilder, if you only do action for a single physical table, you can automatically generate InsertCommand, UpdateCommand, and DeleteCommand according to SELECTCOMMAND. This is the way ADO.NET, in many cases, can save a lot of code and time.

2, more data binding. For example, you can bind the data of one column in the specified data table to a ComboBox.

3, ...

The entire source code can be downloaded at http://www.86soft.com/clsoft/kaneboy/adoplus.zip.

Is there any idea, please contact, Kaneboy @ 163.net, Thanks for opinions and criticism.

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

New Post(0)