Koneksi VB. NET SQL Server 2008


Pada kesempatan kali ini saya akan berbagi tentang koneksi VB.NET ke SQL Server. Software yang digunakan Visual Studio 2010 dan SQL Server 2008.

1. Membuat database


create database DB_INVENTORI;

use DB_INVENTORI;

--- membuat table barang

create table t_barang(

id_barang varchar(5) primary key,

nama_barang varchar(25)

)

 

---- mengisi table barang

insert into t_barang(id_barang,nama_barang) values ('00001','Buku');

insert into t_barang(id_barang,nama_barang) values ('00002','Pensil');

insert into t_barang(id_barang,nama_barang) values ('00003','Penghapus');

insert into t_barang(id_barang,nama_barang) values ('00004','Penggaris');

--- menampilkan data barang

select * from t_barang

2. Membuat project di Visual Studio 2010

Menambahkan connectionstring di web.config


<connectionStrings>

<add name="DB_INVENTORIConnectionString" connectionString="Data Source=HERDIS-PC;Initial Catalog=DB_INVENTORI;User ID=sa;Password=Test1234" providerName="System.Data.SqlClient"/>

</connectionStrings>

Catatan : untuk User ID dan Password disesuaikan dengan login ke SQL Server

Tambahkan form baru koneksi.aspx untuk membuat button pengecekan koneksi


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="koneksi.aspx.vb" Inherits="koneksi" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="btnKoneksi" runat="server" Text="Cek Koneksi ke database" />

</div>

</form>

</body>

</html>

Buka koneksi.aspx.vb atau Klik kanan di koneksi.aspx pilih view code


Imports System.Data

Imports System.Data.SqlClient

Partial Class koneksi

Inherits System.Web.UI.Page

Dim strCon As String = ConfigurationManager.ConnectionStrings("DB_INVENTORIConnectionString").ToString

Dim koneksi As New SqlConnection(strCon)

Dim strMsg As String

'method koneksi

Sub cekKoneksi()

Try

koneksi.Close()

koneksi.Open()

strMsg = "<script language=javascript>alert('koneksi berhasil..')</script>"

Response.Write(strMsg)

Catch ex As Exception

strMsg = "<script language=javascript>alert('koneksi gagal..')</script>"

Response.Write(strMsg)

End Try

End Sub

'method panggil koneksi

Protected Sub btnKoneksi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnKoneksi.Click

cekKoneksi()

End Sub

End Class

3. Run program atau tekan F5 maka akan muncul program di browser seperti gambar berikut

koneksi_aspx

Tekan button Cek Koneksi ke database. Jika berhasil akan muncul alert koneksi berhasil.. dan jika gagal akan muncul alert koneksi gagal..

3 thoughts on “Koneksi VB. NET SQL Server 2008

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s