【问题标题】:Can we declare variables in the 'app.config' file?我们可以在“app.config”文件中声明变量吗?
【发布时间】:2011-05-14 11:55:34
【问题描述】:

我有一个需要连接到 SQL Server 的表单,我有一个用于选择数据库列表和执行主键检查等操作的下拉菜单。

但目前我的连接字符串如下所示:

SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");

但是除了给定的数据库之外,我还需要将它作为变量,以便我可以将它连接到我从下拉列表中选择的数据库。

我该怎么做?

【问题讨论】:

    标签: c# sql-server visual-studio ado.net


    【解决方案1】:

    您可以在 app.config 配置中使用 connectionStrings 标记。您可以根据需要添加任意数量(给它们每个单独的键),然后检索它们

    示例 app.config xml(将 providerName 设置为有效的提供者,例如 System.Data.SqlClient,以及相应的连接字符串):

    <?xml version='1.0' encoding='utf-8'?>
      <configuration>
        <connectionStrings>
          <clear />
          <add name="firstDb" 
           providerName="System.Data.ProviderName" 
           connectionString="Valid Connection String;" />
          <add name="secondDb" 
           providerName="System.Data.ProviderName" 
           connectionString="Valid Connection String;" />
        </connectionStrings>
      </configuration>
    

    获取它们并列出它们的示例(在您的情况下,您将在下拉列表中创建适当的项目并设置值):

    ConnectionStringSettingsCollection settings =
                ConfigurationManager.ConnectionStrings;
    
            if (settings != null)
            {
                foreach(ConnectionStringSettings cs in settings)
                {
                    Console.WriteLine(cs.Name);
                    Console.WriteLine(cs.ProviderName);
                    Console.WriteLine(cs.ConnectionString);
                }
            }
    

    【讨论】:

      【解决方案2】:

      嗯,你可以像这样声明你的变量

      <appSettings>
          <add key="SmtpServerHost" value="********" />
          <add key="SmtpServerPort" value="25" />
          <add key="SmtpServerUserName" value="******" />
          <add key="SmtpServerPassword" value="*****" />
      </appSettings>
      

      读起来像

      string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
      int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);
      

      【讨论】:

      • 这里的值包含什么?
      • value 包含你的值 :) 如果你写 serverhost 值“asd.abc.com”,那么你的字符串 smtpHost 值将是“asd.abl.com”
      • @SerkanHekimoglu 您可能还应该为 ConfigurationManager 指明命名空间 - System.Configuration。并且适当的参考也应该添加到解决方案中。
      • 不努力反对,但不是每个来这里的人都是开发人员,他们中的许多人都是开发人员。最好是在回答时考虑新手,这正是我的想法。
      • @TalhaImam 别担心。编译器会警告您该做什么,以及添加什么作为参考;)
      【解决方案3】:

      您可以使用 AppSettings 部分。阅读here 获取示例。

      【讨论】:

        【解决方案4】:

        我认为他想要一个“半常数”:

        Web.Config

        <?xml version='1.0' encoding='utf-8'?>
        <configuration>
            <connectionStrings>
                <add name="YourName" providerName="System.Data.ProviderName" connectionString="Data Source={0}; Initial Catalog=myDataBase; User Id=myUsername; Password=myPassword;" />
            </connectionStrings>
        </configuration>
        

        CS 文件

        String Servername = "Test";
        String ConnectionString = String.Format(ConfigurationManager.ConnectionStrings["YourName"].ConnectionString, ServerName);
        

        【讨论】:

          猜你喜欢
          • 2022-01-09
          • 2020-07-27
          • 2011-05-05
          • 2017-05-07
          • 2016-02-29
          • 1970-01-01
          • 1970-01-01
          • 2020-09-11
          • 1970-01-01
          相关资源
          最近更新 更多