Cara Panggil String Connection di ASP.NET

Biar ga ngetik connection berulang2 di setiap file aspnya, bikin connection string cukup di web.config ajah, dah gitu panggil connection stringnya.

1. Beranjak dulu ke web.confignya cari :

<connectionStrings>
<add name=”SqlServerConnectionString” connectionString=”Data Source=nama_server;Initial Catalog=BengkelDB;Integrated Security=True”
providerName=”System.Data.SqlClient” />
</connectionStrings>

2. Tambahin imports di semua file .vb di paling atas :

Imports System.Data

Imports System.Data.SqlClient

3. Copy syntax dibawah ini di semua file .vb :

Public Class Connection

Public Shared ReadOnly Property ConnectionString() As String

Get

Return

ConfigurationManager.ConnectionStrings(“BengkelDBConnectionString

“).ConnectionString

End Get

End Property

End Class

Syntax di atas gunanya buat manggil string conection yang tadi uda di bikin di web.config.

4. Nah..sekarang kalo mau dipake di event onClick dsb atau Page_Load tinggal ketik :

Dim Conn As New SqlConnection

Conn.ConnectionString = Connection.ConnectionString

<connectionStrings>
<add name=”BengkelDBConnectionString” connectionString=”Data Source=amul;Initial Catalog=BengkelDB;Integrated Security=True”
providerName=”System.Data.SqlClient” />
</connectionStrings>