Currently, database server-based Desktop ManagementProgramThere are already too many applications with web programs, especially the wide spread of networks. The isolated database management system cannot be competent for distributed management applications, however, we cannot completely discard existing desktop applications based on access databases. We use. net
The remote processing function encapsulates the connection and access behavior as a remote object for other clients in the network to access the actual access database by calling this remote object. We use C #2005
To implement the above functions for the development language.
I. Key Technical Points
We all know that a Windows application starts a process at runtime, which includes several threads. Communication between different processes is necessary to develop distributed applications. Traditionally, this requires an in-depth understanding of the objects of processes on both ends of the Communication stream, as well as the host of the low-level protocol, application programming interfaces, and configuration tools. In short, it is a complex task that requires a lot of professional knowledge and experience.
Fortunately,. NET provides us with the remote processing function. The communication method provided by it can quickly and conveniently complete the preceding communication tasks. Therefore
. NET Framework provides support for applications that require more time to generate critical enterprise-wide applications. Through. net
Remote processing allows client applications to use objects in other processes on the same computer or any other computer on its network.
To use. NET remote processing to create an application that allows two objects to communicate directly across the application, you only need to generate the following objects:
1. objects that can be processed remotely.
2. The application listening for requests to the remote object is the server program.
3. Client Applications that send requests to the remote object.
There are two communication methods for objects in different applications under. Net: one is to transmit object copies across application domain boundaries, and the other is to exchange messages using a proxy. Marshalbyrefobject
It is the base class of the object for communication by using the proxy to exchange messages. When a remote object is used across applications, the base class of the object must be inherited from marshalbyrefobject.
Ii. Program Implementation
(1) We first create a new solution named "testremoteaccess" in vs ide to accommodate the three projects used for remote processing, first, add the class library named "remoteobject" to the solution, change the default class name to "cremoteaccess", and inherit from "delealbyrefobject ",CodeAs follows:
Using system;
Using system. Collections. Generic;
Using
System. text;
Namespace remoteobject
{
Public class cremoteaccess:
Marshalbyrefobject
{}
}
In this object, we need to create all the functions used to connect to and access the local access database for the server client to call at the same time. The method for connecting to and accessing the ACCESS database is not described here. See the source code of the attachment.
First, the visibility of all functions to be made public to the client must be set to public. The variable m_connstring must be set to public.
Static: when the client calls setremoteaccessconnstring, it saves the database connection string for access during this connection. The Code is as follows:
......
Public static string m_connstring;
......
Public void
Setremoteaccessconnstring (string connstr)
{
M_connstring =
Connstr;
}
......
After successfully connecting to the Access database, we need to return the dataset to the requesting client for display and editing. In the remote object, we declare several related functions:
Private void loaddata (string sqlstr, string tablename)
Public
Void savedata (datatable clientdatatable)
Public datatable getusertable (string
Sqlstr, string tablename)
The client can pass an SQL query script by calling
Getusertable is used to obtain the data of the relevant database table and return a able. Then, you can attach the datatable value to the datagridview dview to display the data. Getusertable
Function to obtain the data. The savedata function is used to save the edited dataset back to the local access database file. The Code is as follows:
......
M_connection.open ();
M_adapter.update (clientdatatable );
......
(2) After the remote object is created, we need to create a server application to listen on the Remote Object Request. In the "testremoteaccess" solution, create a Windows form project named "testserver" and drag the following components from the toolbox:
In addition to the ability to remotely access objects, the server program testserver obtains the actual access database file path and sets the database connection string of the remote object. We must add references to remote objects and class libraries such as remote processing and network communication protocols. When the server program starts, you need to create an instance for the remote object and register the communication port, and then call remotingconfiguration. registerwellknownservicetype
Method. The description of this method in msdn is as follows: by initializing a new instance of wellknownservicetypeentry with a given parameter, the type of the object on the server is
Registered as a known type. All customers who know the URI of the registered known object can obtain the proxy of this object. The so-called URI is the Uniform Resource Identifier (Uniform Resource
Identifier ). The Code is as follows:
......
Remotableobject = new
Remoteobject. cremoteaccess ();
Tcpchannel channel = new
Tcpchannel (8080 );
Channelservices. registerchannel (Channel );
Remotingconfiguration. registerwellknownservicetype (typeof (remoteobject. cremoteaccess ),
"Rithiatestaccessserver", wellknownobjectmode. Singleton );
......
After selecting the ACCESS database file to be accessed, we need to call the setremoteaccessconnstring method of the remote object, this method will save the connection string connecting to the ACCESS database file during this connection with the server program. The Code is as follows:
......
Providerstr = providerstrpart + txtaccessmdbfilename. Text +
"; Jet oledb: Database Password =" +
Txtaccesspassword. text;
......
Remotableobject. setremoteaccessconnstring (providerstr );
......
(3) Finally, we create a client program for connecting to and requesting services. It will obtain the relevant dataset by calling the remote object registered by the server program testserver, save the edited data back to the actual database file. In the "testremoteaccess" solution, create a Windows form project named "testclient" and drag the following components from the toolbox:
The client program needs to know the name or IP address of the computer where the server program runs and the listening port number, create an instance of the remote object, and create a datatable to receive the returned data. The Code is as follows:
......
String remoteurl;
Host = txthost. text;
Port =
Txtport. text;
Remoteurl = "TCP: //" + host + ":" + port +
"/Rithiatestaccessserver ";
Try
{
Tcpchannel Chan = new
Tcpchannel ();
Channelservices. registerchannel (Chan );
Remoteobject =
(Remoteobject. cremoteaccess) activator. GetObject (typeof (remoteobject. cremoteaccess ),
Remoteurl );
Remotedatatable = new datatable ();
Button3.enabled =
False;
}
Catch (exception
E)
{
MessageBox. Show (E. Message. tostring ());
}
Finally
{
}
......
The client registers the corresponding channel and port number based on whether the channel listening by the service program is TCP or HTTP, and combines them into the URL of the remote object, that is, url =
Channel: // host name: Port Number/object Uri, and then create an instance for a remote object to access it just like using a local object, you can call the getusertable method of the remote object to obtain the result set of the specified query script. The Code is as follows:
......
Remotedatatable = remoteobject. getusertable (txtsql. Text,
"Test ");
Datagridview1.datasource = remotedatatable;
......
You only need to call the savedata method to save the result set. The Code is as follows:
......
Remoteobject. savedata (remotedatatable );
......
Iii. Results
The program was successfully run in Visual Studio. NET 2005 and Windows XP SP2.