如何在运行时在C#中更改连接字符串 [英] how to change connection string at run time in c#

查看:127
本文介绍了如何在运行时在C#中更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Visual Studio和后端作为mysql

i am using microsoft visual studio and backend as mysql

推荐答案

这是我的工作,与Tim的建议类似.此技术根据计算机选择连接字符串.准备部署时,将连接字符串添加到目标计算机的App.Config.

首先是App.Config

Here''s what I do, which is similar to Tim''s suggestion. This technique picks the connection string based on the machine. When you''re ready to deploy, add a connection string to the App.Config for the target machine.

First the App.Config

<connectionstrings>
      
  <add name="marois_work_1_spares" connectionstring="Data Source=MAROIS_WORK_1\SQLSERVER2008;Initial Catalog=Spares2;Integrated Security=True">
		providerName="System.Data.SqlClient" />

  <add name="ui-pc_spares" connectionstring="Data Source=(local);Initial Catalog=Spares2;Integrated Security=True">
		providerName="System.Data.SqlClient" />

</connectionstrings>



然后,在我的数据访问类中:



Then, in my data access class:

private SparesDataContext getDataContext()
{

    string machineName = Environment.MachineName.Trim().ToLower();
    machineName = machineName + "_spares";
    string connString = string.Empty;

    try
    {
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings[machineName];
        connString = conSettings.ConnectionString;
    }
    catch (Exception ex)
    {
        string errorMessage = "Error reading the connection string from App.Config. " + Environment.NewLine + 
            "The machine name is ''" + machineName.Trim() + "''";
        throw new Exception(errorMessage, ex);
    }


    SparesDataContext dc = new SparesDataContext(connString);

    try
    {
        dc.Connection.Open();
        dc.Connection.Close();
    }
    catch (Exception ex)
    {
        string errorMessage = "Unable to acccess the database." + Environment.NewLine + 
            "The connection string is ''" + connString.Trim() + 
            "''. The machine name name is ''" + machineName.Trim() + "''";
        throw new Exception(errorMessage, ex);
    }

    return dc;
}


这是一个相当广泛的问题.有很多方法可以做到这一点.一种可能是直接向用户询问连接字符串(不是一个好主意).另一种方法是将多个连接字符串保存在配置文件中,然后让您的应用程序根据给定的条件选择使用哪个连接字符串(我们将其用于实时或测试环境).

有多种方法可以根据您的要求(谁进行更改,何时进行更改以及根据什么条件进行更改)来执行此操作.快速的Google搜索将为您提供许多不同的选择.
That is a fairly broad question. There are a number of ways to do this. One would be to ask the user directly for the connection string (not a great idea). Another would be to save multiple connection strings in the config file and then have your application choose which one to use based upon given criteria (we use this for live versus test environment).

There are many ways to do this based upon your requirements (who makes the change, when, and based upon what criteria). A quick Google search will give you a number of different options.


这是一个相当广泛的主题.我建议你看看:

对于Linq To SQL [对于ADO [
This is a pretty broad topic. I suggest you look at:

For Linq To SQL[^]

For ADO[^]


这篇关于如何在运行时在C#中更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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