简体   繁体   English

连接字符串C#

[英]Connection String C#

public string ConnectionString = string.Empty;

In the line above, if the connection string is assigned as string.empty, then how will the connection string get its value? 在上面的行中,如果将连接字符串分配为string.empty,那么连接字符串将如何获得其值? 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 所以,在这里连接字符串从哪里得到它的值

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

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. 如果确实如此,则不能使用您所描述的String.Empty。

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. 为了明确起见,我希望连接字符串来自某种设置对象,或者来自.config文件或其他类似机制。

In an attempt to help I am guessing you are using ASP.net as below you talk about a post variable. 为了帮助我,我猜测您正在使用ASP.net,如下所述,您谈论的是post变量。 These application types tend to read from a web.config file like below. 这些应用程序类型倾向于从如下所示的web.config文件中读取。

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. 然后,您的连接字符串变量将由类似于以下内容的一段代码初始化,如果connectionString元素中有多个键(由add元素表示),则可以通过索引器进行访问,如图所示。

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

config code follows: 配置代码如下:

<?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>

Right click on this.ConnectionString and find all refferences. 右键单击this.ConnectionString并找到所有引用。 I'm sure it is being set somewhere in your code befor it gets to public DataSet GetData(SqlCommand cmd) 我敢肯定它是在您的代码中的某个地方设置的,因为它可以访问公共DataSet GetData(SqlCommand cmd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM