السلام عليكم و رحمة الله
كيفية ربط MYSQL 5.1 مع 2010 # Microsoft Visual C
نبدأ الشرح :
1 الأدوات المستخدمة:
Microsoft Visual C# Expresse 2010
WampServer
MysqlConnector ODBC
بعد تنصيب Mysqlconnector
من Control Panel --> Administation Tolls --> Data Sources ODBC)
نطبق ما في الصورة

و في C#

أضف هذا الكود
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string con;
OdbcConnection odbCcon = new OdbcConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnListTables.Enabled = false;
}
public void btnConnect_Click_1(object sender, EventArgs e)
{
con = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=" + txtIP.Text + ";PORT=" + txtPort.Text + ";DATABASE=" + txtDatabase.Text + ";UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";OPTION=3";
OdbcConnection odbCcon = new OdbcConnection(con);
try
{
label6.Text = odbCcon.ConnectionString;
txtLog.AppendText("Openning connection...\r\n");
if (odbCcon.State == ConnectionState.Closed)
{
odbCcon.Open();
}
if (odbCcon.State == ConnectionState.Open)
{
label6.ForeColor= System.Drawing.Color.Green;
label6.Text = "Online";
btnListTables.Enabled = true;
}
}
catch (System.Data.Odbc.OdbcException Ex)
{
txtLog.AppendText(Ex.Message + "\r\n");
MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try    again.\r\n\r\nMore    details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnListTables_Click(object sender, EventArgs e)
{
con = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=" + txtIP.Text + ";PORT=" + txtPort.Text + ";DATABASE=" + txtDatabase.Text + ";UID=" + txtUsername.Text + ";PWD=" + txtPassword.Text + ";OPTION=3";
OdbcConnection odbCcon = new OdbcConnection(con);
odbCcon.Open();
if (odbCcon.State == ConnectionState.Open)
{
OdbcCommand OdbcCom = new OdbcCommand("SHOW TABLES", odbCcon);
OdbcDataReader OdbcDR;
OdbcDR = OdbcCom.ExecuteReader();
txtLog.AppendText("DataBase Selected " + txtDatabase.Text + ":\r\n");
while(OdbcDR.Read())
{
comdatabase.Items.Add(OdbcDR[0]);
}
}
}
private void comdatabase_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btndisconnect_Click(object sender, EventArgs e)
{
odbCcon.Close();
if (odbCcon.State == ConnectionState.Closed)
{
label6.ForeColor = System.Drawing.Color.Red;
label6.Text = "Offline";
btnListTables.Enabled = false;
comdatabase.Items.Clear();
txtLog.Clear();
txtLog.AppendText("Disconnected\r\n");
}
}
}
}الملف كامل في المرفقات
