29 กรกฎาคม 2554

การใช้งาน app.config


เพิ่ม File ชื่อ app.config

     ตัวอย่างคือเราจะเก็บการเชื่อมต่อฐานข้อมูลโดยใช้ app.config และเก็บค่า 123456 ใน app.config โดยมี Key คือ ConnectionString และ Value1

1. เพิ่ม code ด้านล่างใน app.config ใต้ <configuration>

<appSettings>
<add key="ConnectionString"  value="Data Source=(local)\SQLEXPRESS;Initial Catalog=DatabaseName;User ID=sa;PWD=pwd"  />
<add key="Value1"  value="123456"  />
</appSettings>

2.การเรียกใช้งานภายใน code ก่อนอื่นอย่าลืม
Imports System.Configuration.ConfigurationSettings
Imports System.Configuration.ConfigurationSettings

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim strConnection As String = ""
            Dim strValue1 As String = ""
            strConnection = AppSettings("ConnectionString")
            strValue1 = AppSettings("Value1")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
End Class

0 ความคิดเห็น:

แสดงความคิดเห็น