Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am working on .Net Project in which my requirement is to connect with two same database(on different server) . If one fail then automatically switch to second. Please tell me how can I solve this.
Thanks in Advance.

C#
public MySqlConnection DBConnect()
      {
          try
          {
              Boolean checkConnection = false;
              string connString1 = string.Format(
                      "Server={0};Database={1};Uid=" + Properties.Settings.Default.DBUser +
                      ";Pwd=" + Properties.Settings.Default.DBPass + ";CharSet=utf8;", Properties.Settings.Default.DBServer, Properties.Settings.Default.DBName);
              string connString2 = string.Format(
                     "Server={0};Database={1};Uid=" + Properties.Settings.Default.DBUser +
                     ";Pwd=" + Properties.Settings.Default.DBPass + ";CharSet=utf8;", Properties.Settings.Default.DBServer1, Properties.Settings.Default.DBName);
              var Connection = new MySql.Data.MySqlClient.MySqlConnection();

                  Connection.ConnectionString = connString1;

                  checkConnection = true;
                  Connection.Open();


             if (!checkConnection)
              {
                  Connection.ConnectionString = connString2;
                  Connection.Open();

              }




              statusStripMain.BeginInvoke((MethodInvoker)delegate
                                                              {
                                                                  toolStripStatusLabelDBConnectionStatus.Text =
                                                                      "DB Connected!";
                                                                  toolStripStatusLabelDBConnectionStatus.ForeColor =
                                                                      Color.Green;
                                                              });

              return Connection;
          }
          catch (Exception exp)
          {
              LogOperation(exp.Message);

              statusStripMain.BeginInvoke((MethodInvoker)delegate
                                                              {
                                                                  toolStripStatusLabelDBConnectionStatus.Text =
                                                                      "DB Disconnected!";
                                                                  toolStripStatusLabelDBConnectionStatus.ForeColor =
                                                                      Color.Red;
                                                              });

              return null;
          }
      }
Posted

1 solution

This is supported by MySQL in the connection string

Server=serverAddress1, serverAddress2;

See this link for more info.
http://www.connectionstrings.com/mysql/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900