Use C # for Distributed Database Query

Source: Internet
Author: User

With the rapid development of traditional databases, computer networks, and digital communication technologies, the research and development of distributed database systems featuring data distributed storage and distribution processing have attracted more and more attention. However, its development is complex, which restricts its development to a certain extent. Based on this, this paper proposes to use a new development language C # In the. NET environment combined with the ADO. NET data access model to develop a distributed database system, which greatly simplifies the development process.

1. Distributed Database System

In essence, the data in the distributed database system is logically unified, but physically scattered. Compared with a centralized database, it has the following advantages:

· Solve the problem that organizations are scattered and data needs to be interconnected.

· Load balancing. Load is shared among various processors to avoid critical bottlenecks.

· High reliability. Data is distributed across different sites and there are multiple copies. Even if some sites fail, the system will not be paralyzed.

· Good scalability. When a new relatively independent organizational unit needs to be added, it can be expanded with minimal impact on the current organization.

Although the distributed database system has many advantages, it also brings about many new problems. Such as data consistency, remote data transfer, and reduced communication overhead, which makes the development of distributed database systems more complex. Fortunately, Microsoft's. NET development environment provides us with the C # development language and the ADO. NET data access model. Combining the two to develop a distributed database system can greatly simplify the development work.

2 Remote Processing Framework and ADO. net

Two important issues to be solved when developing a distributed database system are data communication between sites and database operations and management. Using C # and ADO. Net can efficiently and Reliably Solve these two problems. Specifically, it is used in C. NET remote processing framework can easily solve the problem of remote transmission of data and commands; C # Through ADO. net to make the distributed database system more efficient and reliable, and easy to solve the data consistency problem.

2.1. NET remote processing framework

There are three ways to remotely transmit data and commands. The first is to convert the data to be transmitted into a stream format by means of packets or messages, and then send the data to the remote host through socket programming in the form of packets. This method is difficult to implement. The second is to use web services, that is, each remote host provides a Web Service for database query services. In this way, you can only query a single site and cannot perform joint queries for multiple sites. The third is to use the. NET remote processing framework (. NET remoting framework) technology, which hides the technical details of remote calls andProgramWith simple settings, you can change the local object to a remote object that provides remote services. The client can access the remote object transparently as it accesses the local object, all messages and messages are delivered. net remoting object processing greatly simplifies development. The general process of remote processing is as follows:

Figure 1 remote processing

First, the server creates a server-class instance, the remote processing system creates a proxy object that represents the class, and returns a reference to the client object. When the client calls a method, it remotely processes the Basic Structure Connection check type information and sends the call to the server process through the channel. The listener channel obtains the request and forwards it to the remote processing system of the server. The Server Remote Processing System Searches (or creates it if necessary) and calls the requested object. Then, the process is reversed. The remote server processing system binds the response to a message and sends it to the client channel through the server channel. Finally, the client remote processing system returns the call result to the client object through the proxy.

2.2 ADO. net

With XML as the core, ADO. Net is a solution for. NET database applications. It uses an offline data structure, and the data in the data source is cached in the DataSet object. You do not need to lock the data source, and the data is saved in XML format.

2.2.1 ADO. Net Management Data Consistency

In a distributed database system, multiple users may access and modify data at the same time. Therefore, data consistency is indispensable for distributed database systems. ADO. net controls data consistency by using an optimistic consistency solution (in fact, dataset objects are designed to support the use of optimistic consistency control mechanism ), that is, the data row is locked only when the database is actually updated. In the pessimistic consistency solution, data rows are locked for a period of time from being extracted to being updated in the database. Therefore, using ADO. Net can respond to a large number of users in less time.

In addition, in the distributed database system, it is often encountered that when the user modifies the row that has been modified since the extraction, it violates the consistency principle. ADO. net is also well solved, that is, the DataSet object is used to maintain two versions for each modified record: the original version and the updated version. Before the updated record is written back to the database, first, compare the original version recorded in the dataset with the current version in the database. If the two versions match, update the records in the database. Otherwise, an error that violates the consistency principle will occur.

3. instance Development

A household appliance chain store has a headquarters and many branches. The headquarters and branches and the branches often need to query various information (such: the organization establishes a distributed database query system to share information between the headquarters and stores and facilitate unified management.

3.1 System Design

3.1.1 System Structure

System Structure 2:


Figure 2 System Structure

A server with a fixed IP address is configured for the headquarters and each branch. Other computers are connected to the server through the hub, and the servers of the headquarters and each branch are connected through the communication network.

3.1.2 system implementation steps

System implementation is divided into three main steps. First, design databases for the headquarters and each branch. Because of the large data volume, SQL Server is used to create a sales and inventory database for each branch, and an employee database, an inventory database for the entire chain store, a credit card customer database, and a supplier information database for the headquarters. Second, you need to create a dynamic link library (DLL) that provides the Database Service (dbserver) to query some services (such as publishing and obtaining remote objects) and functions (such as local Remote Data Table query, data table remote creation and deletion, table connection and merger) are placed in this DLL, each branch needs to use this DLL, to call some services and functions during query. Finally, develop the client query interface as needed.

3.2 Key Technologies for system implementation

3.2.1 publish and obtain remote objects

The first task after the system runs is to publish local remote objects and obtain the remote objects published by other stores. When releasing a remote object, you must first set a network port number, create and register a channel, and then publish the activation object on the server. Servers in other sites can conveniently obtain published remote objects based on their IP addresses and network port numbers. Key to achieving remote object publishing and RetrievalCodeAs follows:

Remote Object publishing:

// Create a channel instance. The port is the specified network port number.
Tcpchannel mychannel = new tcpchannel (int32.parse (port ));
// Registration Channel
Channelservices. registerchannel (mychannel );
// Publish the server activation object
Remotingconfiguration. registerwellknownservicetype (typeof (dbserver), "Store", wellknownobjectmode. Singleton );
Remote Object acquisition:
// Obtain the corresponding remote object based on the IP address and port number
Try
{Mydbserver = (dbserver) activator. GetObject (typeof (dbserver), "TCP: //" + IP + ":" + P + "/Store ");}
// Catch exceptions
Catch (nullreferenceexception nullexp)
{MessageBox. Show ("the specified URL address cannot be reached" + nullexp. Message );}
Catch (remotingexception remexp)
{MessageBox. Show ("the specified object definition is incorrect" + remexp. Message );}

3.2.2 Database Access

Through ADO. net to access the database, you can easily connect to the database, import data from the data source to the DataSet object, perform various operations on the data table in the DataSet object, and the DataSet object itself can also be remotely transmitted. This greatly facilitates the development of distributed database systems. The key code for database access is as follows:

// Establish a database connection
String sqlconn = "Initial catalog = store; Data Source = localhost; userid = sa; Password = ;";
Sqlconnection conn = new sqlconnection
(Sqlconn );
Conn. open (); // open the database
// Import data from the data source to the DataSet object
Try {
Dataset DS = new dataset ();
Datatable dt = new datatable ("result ");
Sqldataadapter adapter = new sqldataadapter ();
Sqlcommand mysqldatasetcmd = new sqlcommand
(Optional string, Conn); // optional string indicates the command to be executed.
Adapter. selectcommand = mysqldatasetcmd;
Adapter. Fill (DT );
DS. Tables. Add (DT );}
Finally
{Conn. Close (); // close the database connection}

3.2.3 Query

Queries in distributed database systems are generally divided into local queries, remote queries, and joint queries. There is no difference between local queries and centralized database queries. For remote queries, you only need to obtain remote objects and call the query function to conveniently implement them. The most complex is joint queries, it involves data query between multiple sites, remote table creation, transfer, connection, and merge among other technologies. The following example describes the implementation of the Union query.

To query the inventory information of air conditioners supplied by all suppliers in Beijing in the third and fourth chain stores that are closer to them, follow these steps. First, obtain the remote objects published by the Headquarters and the third and fourth chain stores. Then, create a temporary data table T1 at the Headquarters through a remote object to store all the supplier information in Beijing in Table T1 (each branch has only the supplier name and does not know its location, only the headquarters has detailed supplier information), and then Stores Table T1 to the third and fourth chain stores. Then, let table T1 be connected to the inventory tables of the two stores to find out the air conditioning inventory information (such as the name, model, number, price, and other information) provided by all Beijing suppliers ), and return the connection result T2 and T3 to the second chain store. Finally, the T2 and T3 tables are merged and displayed using the DataGrid Control. The above implementation includes the replication, transfer, and connection of data tables between different sites, and some functions used (such: create Data Tables remotely, connect tables to tables remotely, and merge tables) are stored in DLL for convenient calling.

4 Conclusion

The C #. Net remoting technology can be used to conveniently solve the data communication problem between sites. In addition, C # accesses the database through ADO. net, making database operations and management more efficient and reliable. The use of these two technologies effectively solves the main problems of developing distributed database systems, greatly reduces the workload of system development, and improves the reliability and security of the system.

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.