Use C # remote Access to the Access Database

Source: Internet
Author: User

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, whether you need to quickly develop Web applications or spend more time generating critical enterprise-wide applications, the. NET Framework provides support. Through. NET remote processing, client applications can use objects in other processes on the same computer or any other available computer in 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. Externalbyrefobject is the base class of the object for communication by using a 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". The Code is as follows:

Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace RemoteObject
{
Public class CRemoteAccess: externalbyrefobject
{}
}

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 needs to be set to public static. The purpose is to save the database connection string after the client calls SetRemoteAccessConnString for access during the 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 to call GetUserTable to obtain the data of the relevant database table and return a able. Then, the client can attach a value to the DataGridView dview to display the data. GetUserTable obtains data by calling the private LoadData function. 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: initialize a new instance of WellKnownServiceTypeEntry by using the given parameter, and register the object Type on the server 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 tcpchannels (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: the port number/The Uri of the object, and then create an instance of the remote object, which can be accessed just like 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.
Source code download: http://d.download.csdn.net/down/401452/q520525745

Related Article

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.