English 中文(简体)
页: 1
原标题:.net 3.5: To read connectionstring from app.config?

如何阅读用电网复印件从信箱中显示胎儿的链接?

平台为3.5。

     <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
            <connectionStrings>
                 <add connectionString="" providerName="" name=""/>
            </connectionStrings>
        </configuration> 
最佳回答

请见ReadingLink Strings in Web.Config and App.Config and Enterprises Library DAAB Cons(关于作为原删除的回击机的反馈)。

ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["MyConnectionString"]
string connectionString = connection.ConnectionString

您可能需要在<条码>系统上添加一个组字。

问题回答

In the config:

<add name="ConnectionName" connectionString="Data Source=xxxxyyyy;Initial Catalog=MyDB;User ID=userName;Password=pwd" />

C#代码:

    using System.Configuration;

...

    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();

更理想的是,在各地的法典中界定职能并加以使用:

public string getConnectionStringMyDB()
        {
            return ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
        }

如果<代码> 姓名/代码>是显示连接名称的显示价值:

var connectionString = 
    ConfigurationManager.ConnectionStrings[name].ConnectionString;

阁下的榜样是,你没有为<条码>提供价值,因此,你不得不在工作之前这样做。

这就是我所做的。

作为启动工作的一部分,我需要一个自动启动和连接服务器数据库的服务。 这意味着需要将非行连接的名称储存在登记册中,在登记册中储存的护卫必须符合界定的联系方式。 答案是一份小型的Winforms资料袋,管理登记册储存服务启动参数,其中一部分储存参数是银连接线的 姓名/em>。

我在Linq创建的数据库背景类别中增加了两个固定功能。 一种方法列出了在“发展银行”项目背景下界定的亚洲开发银行连接名称。 第二种方法从非行连接名称中转回到了我。 登记册管理手册称为“统计员方法”,以填写名单箱和称为“<代码”的窗口服务。 GetDBContextFromConnectionName() 方法,将从登记册中检索的亚洲开发银行连接名称改为非行。 然后将非银的背景用于进入非行。

这两套方法被编入项目的第一类文件,与林克创建的数据内容类别相同。

结果是:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;

namespace RepositoryProject
{
    public partial class RepositoryDataContext
    {
        /// <summary>
        /// Return a MS SQL-LINQ DB Context given the name of the DB Connection name defined in 
        /// Properties.Settings.Default area of the SQL-Linq project.
        /// </summary>
        /// <param name="dbConnectionName">The name of the prediefined DB Connection string</param>
        /// <returns>A SQL-Linq database context </returns>
        public static RepositoryDataContext GetDBContextFromConnectionName(string dbConnectionName)
        {
            string fullConnectionString = null;

            dbConnectionName = dbConnectionName.Trim();
            if (!String.IsNullOrEmpty(dbConnectionName))
            {
                SettingsPropertyCollection allConnectionStrings = global::Cognex.TA.Framework.Properties.Settings.Default.Properties;
                SettingsProperty connectionProperty = allConnectionStrings[dbConnectionName];
                if (null != connectionProperty)
                {
                    fullConnectionString = (string) connectionProperty.DefaultValue;
                    if (String.IsNullOrEmpty(dbConnectionName))
                    {
                        string msg = "";
                        msg += String.Format( "The connection string name, {0}, exists within the settings of the RepositoryDataContext class but creates an empty DB connection string.", dbConnectionName);
                        throw new ArgumentException(msg);
                    }
                }
                else
                {
                    string msg = "";
                    msg += String.Format( "The connection string name, {0}, does not exist within the settings of the RepositoryDataContext class.", dbConnectionName);
                    throw new ArgumentException(msg);
                }
            }
            else
            {
                string msg = "";
                msg += "The connection string name to the test repository cannot be null or empty.";
                throw new ArgumentException(msg);
            }

            return new RepositoryDataContext(fullConnectionString);

        }

        /// <summary>
        /// Return a list of all the DB Connection names defined in 
        /// Properties.Settings.Default area of the SQL linq project.
        /// </summary>
        /// <returns>A list of DB Connection name</returns>
        public static List<string> GetAllDBConnectionNames()
        {
            List<string> listONames = new List<string>();

            /*
             * within the the Linq-generated code (TestRepository.designer.cs) there is an empty constructor for
             * the data context which looks similar to this:
             *
             * public TestRepositoryDataContext() :
             * base(global::Framework.Properties.Settings.Default.DefaultConnectionString, mappingSource)
             * {
                  OnCreated();
             * }
             *
             * Duplicate that assembly name here
             */
            SettingsPropertyCollection allConnectionStrings = global::Framework.Properties.Settings.Default.Properties;
            foreach(SettingsProperty entry in allConnectionStrings)
            {
                if (entry.PropertyType.ToString().Equals("System.String"))
                {
                    listONames.Add(entry.Name);
                }
            }

            return listONames;
        }
    }
}

我希望这一帮助。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...