Connect to MySQL database from ASP


Berikut adalah cara bagaimana kita membuat connection script agar terhubung ke database MySQL menggunakan ASP. Setelah kita terhubung ke database MySQL dari ASP, kita dapat belajar bagaimana untuk memilih data dari tabel database MySQL dan menampilkan hasilnya di browser. Untuk itu yang diperlukan agar koneksi ke database MySQL dari ASP, kita perlu menginstal MySQL ODBC Driver-MyODBC 3.51.

Cara I – menggunakan DSN

<%
‘declare the variables
Dim Connection
Dim Recordset
Dim SQL

‘declare the SQL statement that will query the database
SQL = “SELECT * FROM TABLE_NAME”

‘create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject(“ADODB.Connection”)
Set Recordset = Server.CreateObject(“ADODB.Recordset”)

‘open the connection to the database
Connection.Open “DSN=dsn_name”

‘Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

‘first of all determine whether there are any records
If Recordset.EOF Then
Response.Write(“No records returned.”)
Else
‘if there are records then loop through the fields
Do While NOT Recordset.Eof
Response.write Recordset(“FIRST_FIELD_NAME”)
Response.write Recordset(“SECOND_FIELD_NAME”)
Response.write Recordset(“THIRD_FIELD_NAME”)
Response.write “<br>”
Recordset.MoveNext
Loop
End If

‘close the connection and recordset objects freeing up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>

Cara II – Tanpa DNS

<%
‘declare the variables
Dim Connection
Dim ConnectionString
Dim Recordset
Dim SQL

‘declare the SQL statement that will query the database
SQL = “SELECT * FROM TABLE_NAME”

‘define the connection string, specify database driver
ConnString = “DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=Your_Mysql_DB; ” &_
“UID=mysql_username;PASSWORD=mysql_password; OPTION=3”

‘create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject(“ADODB.Connection”)
Set Recordset = Server.CreateObject(“ADODB.Recordset”)

‘Open the connection to the database
Connection.Open ConnString

‘Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

‘first of all determine whether there are any records
If Recordset.EOF Then
Response.Write(“No records returned.”)
Else
‘if there are records then loop through the fields
Do While NOT Recordset.Eof
Response.write Recordset(“FIRST_FIELD_NAME”)
Response.write Recordset(“SECOND_FIELD_NAME”)
Response.write Recordset(“THIRD_FIELD_NAME”)
Response.write “<br>”
Recordset.MoveNext
Loop
End If

‘close the connection and recordset objects freeing up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>

Semoga bermanfaat dan selamat mencoba…

Iklan

2 thoughts on “Connect to MySQL database from ASP

  1. Dear alvinburhani.wordpress.com

    Siang,

    Kami dari Humas Merdeka.com bermaksud menawarkan content yang sesuai dengan tipikal blog Anda yang bisa digunakan secara gratis. Content ini sangat

    mudah aplikasinya dan sangat membantu Anda dalam Reblogging.
    Jika Anda berminat, silahkan kunjungi http://content.merdeka.com/ dan dapatkan contentnya.

    Terima kasih atensinya, kami menunggu kabar baik dari Anda.

    Salam,

    Humas Merdeka.com
    Selvie Chummairoch

    Suka

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s