myBlog

Contoh akses database dengan ADO.net menggunakan C# dan vb.net

on July 12, 2011

C#

//sertakan class MySql.Data.MySqlClient
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      string connectionString = @"server=localhost;user id=master_db;
                 password=admin;database=master_db;";
      //note : "@" untuk mengakomodasi penulisan multiple lines of string

      //deklarasi objek connection
      MySqlConnection myConnection = null;
      //deklarasi objek command
      MySqlCommand myCommand = null;
      //deklarasi objek datareader
      MySqlDataReader myReader = null;

      try
        {
	  //inisiasi objek connenction
           myConnection = new MySqlConnection(connectionString);
           //open connection
           myConnection.Open();

           string queryString = "select * from user";
		
           //inisiasi objek command
           myCommand = new MySqlCommand(queryString);
           //set command connection
           myCommand.Connection = myConnection;

           //eksekusi command query menggunakan objek datareader
           myReader = myCommand.ExecuteReader();

           listBox1.Items.Clear();
           while (myReader.Read())
           {
             listBox1.Items.Add(myReader["nama"].ToString());
           }
           myReader.Close();
         }
         catch (Exception ex)
         {
           MessageBox.Show(ex.Message);
         }

         myCommand.Dispose();
         myConnection.Dispose();
    }
  }
}

 

Vb.net

'sertakan class MySql.Data.MySqlClient
Imports MySql.Data.MySqlClient

Public Class Form1
    Private Sub button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles button1.Click

        Dim connectionString As String = "server=localhost;" & _
            "user id =master_db;password=admin;database=master_db;"	
	
        'deklarasi objek connection
        Dim myConnection As MySqlConnection = Nothing
        'deklarasi objek command
        Dim myCommand As MySqlCommand = Nothing
        'deklarasi objek datareader
        Dim myReader As MySqlDataReader = Nothing

        Try
            'inisiasi objek connenction
            myConnection = New MySqlConnection(connectionString)
            'open connection
            myConnection.Open()

            Dim queryString As String = "select * from user"

            'inisiasi objek connenction
            myCommand = New MySqlCommand(queryString)
            'set command connection
            myCommand.Connection = myConnection

            'eksekusi command query menggunakan objek datareader
            myReader = myCommand.ExecuteReader()

            listBox1.Items.Clear()
            While myReader.Read()
                listBox1.Items.Add(myReader("nama").ToString())
            End While
            myReader.Close()

        Catch ex As System.Exception
            MessageBox.Show(ex.Message)
        End Try

        myCommand.Dispose()
        myConnection.Dispose()
    End Sub
End Class

 

Advertisements

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

%d bloggers like this: