在Windows应用程序中使用两个连接字符串 [英] use two connection string in windows application

查看:59
本文介绍了在Windows应用程序中使用两个连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All。



我正在使用Windows应用程序。

它将在两个地方使用。

sql server ll b在一个位置。

所以我在app-config文件中给出了两个不同名称的连接字符串。 (本地和网络)



现在如何检查哪个连接可用?





用于连接我在类中编写以下代码。



Hello All.

I m working on a windows application.
it ll be used in two locations.
sql server ll b at one location.
so i hv given two connection strings in app-config file with two different names. (Local and Web)

now how to check which connection is available?


for connection i hv written following code in a class.

public sealed class Connection : MyDBManager
   {
       private static string _dbUser, _dbPwd, _dataSource, _dataProvider;
       public string DBUser { get { return _dbUser; } set { _dbUser = value; } }
       public string DBPwd { get { return _dbPwd; } set { _dbPwd = value; } }
       public string DBSource { get { return _dataSource; } set { _dataSource = value; } }
       public string Provider { get { return _dataProvider; } set { _dataProvider = value; } }


       public Connection()
       {
           ConnectToDB();
       }

       public Connection(string ServerName)
       {
           ConnectToDB(ServerName);
       }

       private bool ConnectToDB()
       {
           getDBValues();
           string connStr = ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString;
           base.ConnectionString = connStr;
           try
           {
               base.Open();
               return true;
           }
           catch
           {
               return false;
           }
       }

       private bool ConnectToDB(string ServerName)
       {
           if (ServerName == "DelhiServer")
           {
               getDBValues();
               string connStr = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
               base.ConnectionString = connStr;
               try
               {
                   base.Open();
                   return true;
               }
               catch
               {
                   return false;
               }
           }
           else
           {
               return false;
           }
       }

       public bool DisposeConnection()
       {
           base.RemoveParameters();
           base.Close();
           base.Dispose();
           return true;
       }

       private void getDBValues()
       {
           //Cryptograph crypt= new Cryptograph();
           //string _dataProvider;
           //_dbUser = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbUser"],PW_KEY);
           //_dbPwd = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbPwd"], PW_KEY);
           //_dataSource = crypt.Decrypt(System.Configuration.ConfigurationSettings.AppSettings["dbSource"], PW_KEY);
           //_dataProvider=System.Configuration.ConfigurationSettings.AppSettings["dataProvider"];

           _dataProvider = "SQLSERVER";
           if (_dataProvider.ToUpper() == "OLEDB")
           {
               base.ProviderType = DataProvider.OleDb;
           }
           else if (_dataProvider.ToUpper() == "ORACLE")
               base.ProviderType = DataProvider.Oracle ;
           else if (_dataProvider.ToUpper() == "SQLSERVER")
               base.ProviderType = DataProvider.SqlServer;

           else if (_dataProvider.ToUpper() == "ODBC")
               base.ProviderType = DataProvider.Odbc;
           else
               base.ProviderType = DataProvider.OleDb;

       }
   }







请帮助我这样做。



提前感谢




please kindly help me to do this.

thanks in advance

推荐答案

这可能不是最优雅或最有效的方式做它,但它确实有效,此代码首先尝试连接Windows身份验证,如果失败,它将尝试SQL身份验证。从本质上讲,它与您的问题在同一主体上工作。

您需要做的就是在try catch中的不同阶段使用两个不同的连接字符串。



This might not be the most elegant or efficient way to do it, but it does work, This code first tries connecting with windows authentication and if that fails it will try SQL authentication. In essence it works on the same principal as your question.
All you will need to do is use the two different connection strings at different stages within the try catches.

public void Connect()
       {
           try
           {
               string connectionString = "Data Source='" + ServerAdrress.Text + "';Initial Catalog='" + DatabaseSelection.SelectedItem.ToString() + "';Integrated Security=SSPI;";
               string SQLconnectionString = "Data Source='" + ServerAdrress.Text + "';Initial Catalog='" + DatabaseSelection.SelectedItem.ToString() + "';Persist Security Info=True;User ID='" + ConnectionUser.SelectedItem.ToString() + "';Password='" + lblPassword.Text + "'";
               try
               {
                   conn = new SqlConnection(connectionString);
                   conn.Open();
               }
               catch (Exception ex)
               {
                   try
                   {
                       conn = new SqlConnection(SQLconnectionString);
                       conn.Open();
                   }
                   catch (Exception)
                   {
                       MessageBox.Show(ex.Message);
                       conn.Close();
                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }





希望这会有所帮助。



Hope this helps.


您只需要一个连接字符串,只需将不同的配置文件分发到每个位置。
You only need one connection string, simply distribute different config files to each location.


这篇关于在Windows应用程序中使用两个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆