□投稿者/ たかし (23回)-(2019/12/08(Sun) 11:07:01)
|
分類:[.NET 全般]
いつもお世話になっております、たかしです。
App.configの「接続文字列」を変数に格納してVB.NETからSQLサーバーに接続しようと思い、
次のようなコードを書きました。
ちなみにForm1の接続文字列TextBoxには
"Data Source=(local)\SQLEXPRESS;Initial Catalog=**********SQL;Integrated Security=True"
と入力してあります。
Private Sub 実行Button_Click(sender As Object, e As EventArgs) Handles 実行Button.Click
Dim cn As New SqlClient.SqlConnection
Dim cd As New SqlClient.SqlCommand
Dim 接続文字列 As String
接続文字列 = Form1.接続文字列TextBox.Text
cn.ConnectionString = 接続文字列
cn.Open()
cd.CommandType = CommandType.StoredProcedure
cd.CommandText = "ストアドプロシージャ名"
cd.Connection = cn
cd.ExecuteNonQuery()
cd.Dispose()
cn.Close()
cn.Dispose()
End Sub
Visual StudioのApp.configタブは次のとおりです。
前略
<connectionStrings>
<add name="**********.My.MySettings.**********_SQLConnectionString"
connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=**********SQL;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
後略
なお、変数を用いずにダイレクトに次のように記載すると正常に動きます。
cn.ConnectionString = "Data Source=(local)\SQLEXPRESS;Initial Catalog=**********SQL;Integrated Security=True"
どこがいけないのでしょうか?
どなたかお願いします。
|
|