A simple algorithm and implementation for database shunting

Source: Internet
Author: User
In 2003, I used this method when creating a province's college entrance examination registration system. It worked well, but the source code is based on. Today it is not suitable.
The method is simple:
1. Purpose: To distribute a large database into several small databases, on the one hand, to speed up the query, on the other hand, the most important thing is Database services are distributed to several different servers.To enhance the system's ability to reflect.
2. Method: distribute the database. If it is a continuous number, it is best to divide it. For example, the mobile phone number can be based on the number segment. The ID card can be used according to the region. If the string type, such as name, is more complex, you can hash it and try again later.
3. Reality: This method is stable and reliable. We have not encountered any problems in multiple systems for many years.
4. Insufficiency: It is difficult to merge statistics for multiple separate databases.
The code for this method is as follows:
1. Configure the Web. Config file <connectionStrings>
<Add name = "zy00" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ zhiyuan_Data.MDF; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
<Add name = "zy01" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ zhiyuan_Data.MDF; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
<Add name = "zy02" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ zhiyuan_Data.MDF; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
<Add name = "zy03" connectionString = "Data Source =. \ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ zhiyuan_Data.MDF; Integrated Security = True; User Instance = True"
ProviderName = "System. Data. SqlClient"/>
<! --
The following code is omitted: zy00 indicates that the volunteer 00 library is unavailable. Only to enhance program robustness, other databases cannot be correctly located before turning to this database.
The numbers of the remaining databases are displayed at the backend, and there can be any number of other databases.
-->
</ConnectionStrings>

2. Code for redirection Based on Input keywords. (The entered keyword contains two region codes, which are used for steering control.) 01 represents Shijiazhuang and 02... 1/** // <summary>
2 // determine which database to operate based on the passed student ID
3 // currently directed to the municipal level, which can be expanded in the future
4 /// </summary>
5 /// <param name = "ksh"/"bmddm"> </param>
6 /// <returns> connection string </returns>
7 public static string changeconbyksh (string ksh)
8 {
9/** // The 5th and 6 digits of ksh are the local code. You need to determine based on the local code and use the corresponding string.
10 ///
11 // In order to reuse the program, the input parameters here can be ksh or bmddm, which need to be determined. The total number of digits and the location of the region code are different.
12 string strdishi = "00 ";
13 if (ksh. Trim (). Length = 6)
14 {
15 strdishi = ksh. Trim (). Substring (0, 2 );
16}
17 else
18 {
19 strdishi = ksh. Substring (4, 2 );
20}
21 // database prefix. In this example, only the Volunteer database is operated.
22 string prefixes = "zy ";
23 // connection to be returned
24 string strcon = "";
25 switch (strdishi)
26 {
27 case "01 ":
28 strcon = ConfigurationManager. ConnectionStrings [prefixes + strdishi]. ToString ();
29 break;
30 case "02 ":
31 strcon = ConfigurationManager. ConnectionStrings [prefixes + strdishi]. ToString ();
32 break;
33 case "03 ":
34 strcon = ConfigurationManager. ConnectionStrings [prefixes + strdishi]. ToString ();
35 break;
36 // The following is omitted...
37 default: // if the location is not correct, a pre-defined garbage collection library is returned. 1. Avoid returning exceptions. 2. Collect data to identify the cause of the error.
38 strcon = ConfigurationManager. ConnectionStrings [prefixes + "00"]. ToString ();
39 break;
40}
41 return strcon;
42}

3. Call the redirection statement in the program

// This example uses the data adapter in the dataset.
ZhiyuansTableAdapters. tzhiyuanyxTableAdapter zyapt = new zhiyuansTableAdapters. tzhiyuanyxTableAdapter ();
// After the adapt is created, the system automatically directs the connection according to the student ID.
Zyapt. Connection. ConnectionString = zhiyuan. changeconbyksh (ksh );
Zhiyuans. tzhiyuanyxDataTable tablezhiyuan = new zhiyuans. tzhiyuanyxDataTable ();
Zyapt. getkszhiyuan (tablezhiyuan, ksh, pcdm, zyh );

OK.

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.