连接字符串C# [英] Connection String C#

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

问题描述

public string ConnectionString = string.Empty;

在上面的行中,如果将连接字符串指定为string.empty,那么如何连接字符串得到它的值?我不明白这到底是什么意思。

In the line above, if the connection string is assigned as string.empty, then how will the connection string get its value? I dont understand what this means exactly.

正在读取的代码在上述语句之后包含以下内容:

The code am reading through contains the following after the above statement:

  public DataSet GetData(SqlCommand cmd)
{
    SqlConnection conn = new SqlConnection(this.ConnectionString);
    DataSet ds = new DataSet();
    try
    {
        cmd.Connection = conn;
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        conn.Open();
        ad.Fill(ds);
        cmd.Parameters.Clear();

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        cmd.Parameters.Clear();
        conn.Close();
    }
    return ds;
}

因此,连接字符串在哪里从中获取值

So, here where does the connection string gets it value from

推荐答案

问题的答案是,如果您的类将ConnectionString作为属性,则此处的SqlConnection对象无法连接到任何东西,因为它具有值String.Empty传递给它。

The answer to your question is that if your class has that ConnectionString as a property then the SqlConnection object here cannot connect to anything as its had the value String.Empty passed to it.

它不能神奇地计算出它永远不会连接任何东西的值,因为您没有有效的连接字符串。此代码是否实际连接?

It doesn't magically work out the value its just never going to connect to anything, as you do not have a valid connection string. Does this code actually connect? if it does it can't be using String.Empty like you are describing.

为澄清起见,我希望连接字符串来自某种设置对象,

To clarify I would expect the connection string to come from a settings object of some sort, either from a .config file or other such mechanism.

为了帮助我,我猜测您正在使用ASP.net,如下所述,您谈论的是post变量。这些应用程序类型倾向于从如下所示的web.config文件中读取。

In an attempt to help I am guessing you are using ASP.net as below you talk about a post variable. These application types tend to read from a web.config file like below.

然后,连接字符串变量将由一段看起来像这样的代码初始化,如果

Then your connection string variable would be initialized by a piece of code that looked something like this, if there are multiple keys in the connectionString elements (noted by the add element) then you access via the indexer as shown.

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["ApplicationServices"];
string connectionString = connectionStringSettings.ConnectionString;

配置代码如下:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="false" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

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

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