2012-03-19 20 views
0
public string ConnectionString = string.Empty; 

上記の行で、接続文字列がstring.emptyとして割り当てられている場合、接続文字列はどのようにその値を取得しますか?私はこれが正確に何を意味するのか分かりません。接続文字列は接続文字列C#

+1

ConnectionStringは単なる変数です。表示されたコードから、空に設定されます。あなたの質問についてより多くの文脈を提供してください。 –

+0

あなたの他の質問を調べた後で、あなたが受け入れていない有効な答えを持つ少なくとも1つがあります。あなたは正解で答えを持っていても答えを受け入れないなら、人々はいつも助けてくれるわけではありません。 –

答えて

4

からそれに値を取得しない場所あなたの質問への答えはあなたのクラスであればということで、ここで、だから、

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; 
} 

:コードはを通じて読んでい

は、上記のステートメントの後に次のものが含まそのConnectionStringをプロパティとして持つ場合、ここのSqlConnectionオブジェクトはString.Emptyに渡された値を持つため何も接続できません。

有効な接続文字列がないため、何も接続しないという価値は魔法のように働きません。このコードは実際に接続していますか?それはあなたが記述しているようにString.Emptyを使用することはできません。

明確にするために、私は接続文字列が.configファイルか他のそのようなメカニズムのいずれかの設定オブジェクトから来ると予想します。

あなたが推測しているように、以下のようにASP.netを使用しています。投稿変数について説明します。これらのアプリケーションタイプは、以下のようにweb.configファイルから読み取る傾向があります。

次に、connectionString要素に複数のキーがある場合(add要素で示される)、次に示すようにインデクサーを介してアクセスする場合、接続文字列変数がこのようなコードで初期化されます。

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> 
+0

ご返信ありがとうございます。設定ファイルは以下のもので構成されています:public static string DBConnection(文字列ProjDB){戻り値 "データソース= SampleDB;初期カタログ=" + ProjDB + ";ユーザーID = sa;パスワード=ユーザー"; }この場合、接続する2つのデータベースがあります。その場合、変数ProjDBはどのようにして正しいデータベースを取得しますか?アプリケーションの実行中にPOST変数として渡されますか? – rosebrit3

+0

私はそれを言うのは嫌ですが、ProjDBはあなたのコードの別のビットによってその関数に渡されますあなたの質問から私はあなたがPOSTについて話すときにASP.netを使用していると単純に推測します。物事をより明確にする –

2

を右this.ConnectionStringをクリックし、すべてのrefferencesを見つけます。私はそれがあなたのコードのどこかに設定されていると確信しています。それはpublic DataSet GetData(SqlCommand cmd)に到達します。