C# 如何操作SQL连接字符串

C# 如何操作SQL连接字符串,c#,connection,connection-string,C#,Connection,Connection String,我正在尝试操作SQL连接字符串,因此它不是运行数据库的原始副本,而是从C#project中的copy one文件夹运行。最简单的方法可能是,不按原样存储连接字符串,而是为要替换为不同值的位添加占位符。如果您使用{0},{1}等,那么您就可以在运行时使用string.Format插入正确的值。最简单的方法可能是不按原样存储连接字符串,而是为要替换为不同值的位放入占位符。如果您使用{0},{1}等,那么您就可以在运行时使用string.Format插入正确的值。我不能100%确定问题的背景是什么,但

我正在尝试操作SQL连接字符串,因此它不是运行数据库的原始副本,而是从C#project中的copy one文件夹运行。

最简单的方法可能是,不按原样存储连接字符串,而是为要替换为不同值的位添加占位符。如果您使用
{0}
{1}
等,那么您就可以在运行时使用
string.Format
插入正确的值。

最简单的方法可能是不按原样存储连接字符串,而是为要替换为不同值的位放入占位符。如果您使用
{0}
{1}
等,那么您就可以在运行时使用
string.Format
插入正确的值。

我不能100%确定问题的背景是什么,但以下内容可能适合您。 一种方法是让App.Config完成工作:

<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
<dataConfiguration defaultDatabase="DEV" />
<connectionStrings>
    <add name="DEV" connectionString="Database=xxx;Server=xxx;Trusted_Connection=True" providerName="System.Data.SqlClient" />
    <add name="LIVE" connectionString="Database=xxx;Server=xxx;Trusted_Connection=True" providerName="System.Data.SqlClient" />
</connectionStrings>...

我不是100%确定你的问题的背景是什么,但以下可能对你有用。 一种方法是让App.Config完成工作:

<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
<dataConfiguration defaultDatabase="DEV" />
<connectionStrings>
    <add name="DEV" connectionString="Database=xxx;Server=xxx;Trusted_Connection=True" providerName="System.Data.SqlClient" />
    <add name="LIVE" connectionString="Database=xxx;Server=xxx;Trusted_Connection=True" providerName="System.Data.SqlClient" />
</connectionStrings>...
看看这个:

如果您使用SQL Server,这比尝试操作字符串要好得多…

请尝试查看以下内容:


如果您使用SQL Server,这比尝试操作字符串要好得多。

如果您使用XSD/适配器,则需要在应用程序启动时手动设置适配器的connectionString。如果您的适配器位于一个单独的库中,则需要使用一个签名重载适配器的构造函数,您可以向该签名传递connectionString(如果是这种情况,请阅读下面的内容)。用于创建xsd的第一次连接字符串被缓存

如果是这种情况,请执行以下操作:

namespace Adapters.UserTableAdapters
{
    public partial class UsersTableAdapter : global::System.ComponentModel.Component
    {
        public UsersTableAdapter(string connectionString)
        {
            this._clearBeforeFill = true;
            this._connection = new System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = connectionString;
        }
    }
}
假设您有User.xsd。右键单击,然后查看代码。如果展开(+)xsd,您会注意到User.cs已创建

您可能已生成以下代码:

namespace Adapters {


    public partial class User
    {
    }
}
您应该添加以下内容:

namespace Adapters.UserTableAdapters
{
    public partial class UsersTableAdapter : global::System.ComponentModel.Component
    {
        public UsersTableAdapter(string connectionString)
        {
            this._clearBeforeFill = true;
            this._connection = new System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = connectionString;
        }
    }
}
现在,在初始化UsersTableAdapter时,将connectionString传递给它

因为我在整个网站上都使用connectionString,而且一直阅读web.config很烦人,所以我创建了一个简单的类:

namespace Data
{
    public class DataModule
    {
        private static string _connectionString = String.Empty;


        public static string ConnectionString
        {
            get
            {
                if (_connectionString == String.Empty)
                    throw new Exception("DataModule not initialized");
                return _connectionString;
            }
        }

        public static void initialize(string connectionString)
        {
            _connectionString = connectionString;
        }
    }
}
在应用程序启动时,我调用

DataModule.Initialize(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
请注意,此类仅支持1个connectionString。您可以轻松地对其进行修改,以支持许多


如果您更好地描述您的环境,我们可以为您提供更多帮助=)

如果您使用XSD/适配器,则需要在应用程序启动时手动设置适配器的连接字符串。如果您的适配器位于一个单独的库中,则需要使用一个签名重载适配器的构造函数,您可以向该签名传递connectionString(如果是这种情况,请阅读下面的内容)。用于创建xsd的第一次连接字符串被缓存

如果是这种情况,请执行以下操作:

namespace Adapters.UserTableAdapters
{
    public partial class UsersTableAdapter : global::System.ComponentModel.Component
    {
        public UsersTableAdapter(string connectionString)
        {
            this._clearBeforeFill = true;
            this._connection = new System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = connectionString;
        }
    }
}
假设您有User.xsd。右键单击,然后查看代码。如果展开(+)xsd,您会注意到User.cs已创建

您可能已生成以下代码:

namespace Adapters {


    public partial class User
    {
    }
}
您应该添加以下内容:

namespace Adapters.UserTableAdapters
{
    public partial class UsersTableAdapter : global::System.ComponentModel.Component
    {
        public UsersTableAdapter(string connectionString)
        {
            this._clearBeforeFill = true;
            this._connection = new System.Data.SqlClient.SqlConnection();
            this._connection.ConnectionString = connectionString;
        }
    }
}
现在,在初始化UsersTableAdapter时,将connectionString传递给它

因为我在整个网站上都使用connectionString,而且一直阅读web.config很烦人,所以我创建了一个简单的类:

namespace Data
{
    public class DataModule
    {
        private static string _connectionString = String.Empty;


        public static string ConnectionString
        {
            get
            {
                if (_connectionString == String.Empty)
                    throw new Exception("DataModule not initialized");
                return _connectionString;
            }
        }

        public static void initialize(string connectionString)
        {
            _connectionString = connectionString;
        }
    }
}
在应用程序启动时,我调用

DataModule.Initialize(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
请注意,此类仅支持1个connectionString。您可以轻松地对其进行修改,以支持许多

如果您更好地描述您的环境,我们可以为您提供更多帮助=)

该类是处理连接字符串的各种键/值对的好方法。您应该使用相关的特定于提供程序的connectionstringbuilder类。

该类是处理连接字符串的各种键/值对的好方法。您应该使用相关的特定于提供程序的connectionstringbuilder类