「web.configで設定した接続文字列を取得」の編集履歴(バックアップ)一覧に戻る

web.configで設定した接続文字列を取得 - (2008/01/21 (月) 15:24:13) のソース

*web.configで設定した接続文字列を取得

***概要
web.config中にある「connectionString」を取得する方法

-[[.NET Framework クラス ライブラリ ConfigurationElement.Item プロパティ (String)>>http://msdn2.microsoft.com/ja-jp/library/c8693ks1.aspx]]

//見出し
***前提条件
-[[新しいwebサイトの作成]]
-[[データベースへの接続の追加]]

***手順
web.configに以下を追加。
#highlight(xml){{
<connectionStrings>
  <add 
       name="masterConnectionString1" 
       connectionString= _
        "Data Source=YOKOSAN\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"
       providerName="System.Data.SqlClient" />
</connectionStrings>
}}
default.aspx.vbで以下のように取得。
#highlight(vb.net){{

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = System.Configuration.ConfigurationManager. 
                      ConnectionStrings.Item("masterConnectionString1").ToString
     End Sub

End Class
}}

Label1をデザインで配置。

#img(113.jpg)


以下のように表示される。

#img(112.jpg)
記事メニュー
目安箱バナー