วันจันทร์ที่ 2 มิถุนายน พ.ศ. 2551

วิธีการติดต่อฐานข้อมูลโดยใช้ SharpDevelop

1. ก่อนอื่นต้อง Download MySql Connector มาก่อน
2. หลังจากติดตั้งเสร็จแล้ว ให้เรา Add Reference ลงใน Project ของเราโดยเลือก
Project->AddReference->GAC->MySQL->Select

3. เพิ่ม Code ลง "using Mysql.Data.MySqlClient;" ลงในส่วน using

ตัวอย่าง Code


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace TestDatabase
{
///
/// Description of MainForm.
///

public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=test;" +
"UID=root;" +
"PASSWORD=;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from test";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string thisrow = "";
for(int i= 0;ithisrow+=Reader.GetValue(i).ToString() + ",";
listBox1.Items.Add(thisrow);
}
connection.Close();
}
}
}