跨页面的C#Sql连接字符串

跨页面的C#Sql连接字符串,c#,sql,connection-string,C#,Sql,Connection String,如果我有几个页面,那么创建连接字符串变量并在所有页面之间共享它的正确过程是什么。我不希望为每个页面键入100次连接字符串,而希望调用它。我可以在我的命名空间中创建它吗?或者最好的方法是什么?它通常在配置文件(web.config)中。它通常在配置文件(web.config)中。将连接字符串放在web.config文件中。请参阅MSDN上的以下内容: 配置中的连接字符串示例: <connectionStrings> <add name="Movies2"

如果我有几个页面,那么创建连接字符串变量并在所有页面之间共享它的正确过程是什么。我不希望为每个页面键入100次连接字符串,而希望调用它。我可以在我的命名空间中创建它吗?或者最好的方法是什么?

它通常在配置文件(web.config)中。

它通常在配置文件(web.config)中。

将连接字符串放在web.config文件中。请参阅MSDN上的以下内容:

配置中的连接字符串示例:

<connectionStrings>
    <add name="Movies2"
         connectionString="Data Source=(local);Initial Catalog=Movies;User ID=wt3movies;Password=lalalalala;Integrated Security=SSPI"
         providerName="System.Data.SqlClient" />
</connectionStrings>

将连接字符串放入web.config文件中。请参阅MSDN上的以下内容:

配置中的连接字符串示例:

<connectionStrings>
    <add name="Movies2"
         connectionString="Data Source=(local);Initial Catalog=Movies;User ID=wt3movies;Password=lalalalala;Integrated Security=SSPI"
         providerName="System.Data.SqlClient" />
</connectionStrings>
您不能使用
配置吗?

您不能使用
配置吗?

好吧,我会把设置文件放在那里。这可能不是最好的选择,但对我来说很有用。

好吧,我会把设置文件放在那里。这可能不是最好的选择,但对我来说很有用。

有很多选择。这取决于您使用的数据访问方法。我建议创建一个类来处理从web.config文件加载连接字符串并将其作为公共属性公开。

有很多选项。这取决于您使用的数据访问方法。我建议创建一个类来处理从web.config文件加载连接字符串并将其公开为公共属性的问题。

有多种方法可以实现这一点,可以深入研究architecture/SOC/IoC/Repository/etc,但要以最简单的方式回答您的问题,您可以创建一个数据库类,该类有一个从配置中获取连接字符串的方法

internal class DataAccess
{
   static string GetDatabaseConnection()
    {
      return ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
      // where AppDb is defined in your web.config/app.config.
    }
}
您的页面可以只使用:

string connection = DataAccess.GetDatabaseConnection();

有多种方法可以实现这一点,这些方法将深入研究体系结构/SOC/IoC/Repository/etc,但要以最简单的方式回答您的问题,您可以创建一个数据库类,该类有一个从配置中获取连接字符串的方法

internal class DataAccess
{
   static string GetDatabaseConnection()
    {
      return ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
      // where AppDb is defined in your web.config/app.config.
    }
}
您的页面可以只使用:

string connection = DataAccess.GetDatabaseConnection();

连接字符串通常存储在配置文件(如web配置)中。下面是一个简单的例子 在配置中添加类似的内容

<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

全文在这里

连接字符串通常存储在配置文件(如web配置)中。下面是一个简单的例子 在配置中添加类似的内容

<connectionStrings>
  <add 
    name="NorthwindConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>

全文在这里

您应该将其保存在配置文件中。对于winforms,它将是app.config,对于webforms,它是web.config。这是您需要的部分(对于winforms)


然后您可以像这样访问连接字符串(取决于您的.NET版本-这适用于2.0)


string connectionString=((string)(configurationAppSettings.GetValue(ConnectionString1“”,typeof(string)))

您应该将其保存在配置文件中。对于winforms,它将是app.config,对于webforms,它是web.config。这是您需要的部分(对于winforms)


然后您可以像这样访问连接字符串(取决于您的.NET版本-这适用于2.0)


string connectionString=((string)(configurationAppSettings.GetValue(ConnectionString1“”,typeof(string)))

(App/Web)文件中有连接字符串(CS)。配置
文件,并从静态方法
GetConncectionString()
返回CS。这意味着,此特定的静态方法将在需要CS的所有页面中使用。

(App/Web)中有连接字符串(CS).Config文件,并从静态方法返回CS。这意味着,此特定静态方法将在需要CS的所有页面中使用。

您还可以创建一个名为connectionStrings.Config的文件或您选择的名称,其中包含以下内容:

    <connectionStrings>
  <add name="MyConnection" connectionString="server=MyServer; database=MyDataBase; user id=myUser; pwd=MyPwd;"/>
</connectionStrings>

然后,在Web.Config中 在节点下插入此标记

<connectionStrings configSource="connectionStrings.config"/>

您还可以创建一个名为connectionStrings.config的文件或您选择的名称,其中包含以下内容:

    <connectionStrings>
  <add name="MyConnection" connectionString="server=MyServer; database=MyDataBase; user id=myUser; pwd=MyPwd;"/>
</connectionStrings>

然后,在Web.Config中 在节点下插入此标记

<connectionStrings configSource="connectionStrings.config"/>