99
Tayanch so`zlar: MySqlConnection , MySql.Data , C#, C++.
C # ni MySQLga bog‟lash. MySQL ma'lumotlar bazasini C #
dasturiga ulash uchun MySQL MySQL ulagichi / tarmog'ida
bir qator
kutubxonalarni qo‟shishimiz zarur. C#
ilova va MySQL serveri
o'rtasidagi barcha aloqa MySqlConnection ob'ekti orqali yuboriladi.
Shunday qilib, dastur server bilan bog'lanishdan
oldin,
u MySqlConnection ob'ekti, sozlashi va ochishi kerak.
Mysql dasturini
quyidagi
veb-saytidan MySQL
Connector
/
Netni bepul yuklab olishingiz mumkin.
Havolani bosing: https://dev.mysql.com/downloads/connector/net
Ulanish uchun oldin MySQL kutubxonasini qo'shishingiz kerak,
mysql
Reference bo‟limidan qo‟shiladi.
Buni amalga oshirish uchun
proekt nidan kelib chiqib o'ng tugmasini bosing va "qo'shish"ni tanlang,
so'ng ro'yxatdan " MySql.Data " ni tanlang .
14.1 rasm MySQL.Data qo‟shish
Keyinchalik, C # loyihangizga MySql Libraryni qo'shishingiz kerak .
C# MySQL ulanish quyidagi ko‟rinishda bo‟ladi:
string myConnectionString = "server=localhost; database=testDB;
uid=root; pwd=abc123;";
Quyidagi C# dasturi MySqlConnection ob'ektini yaratish, ulanish satrini
tayinlash va ulanishni ochish uchun ishlatiladi.
using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
100
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
MySqlConnection cnn ;
connetionString
=
"server=localhost;database=testDB;uid=root;pwd=abc123;";
cnn = new MySqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
Serverlar
Replikatsiya qilingan server konfiguratsiyasida
serverga ulanish
uchun quyidagi server ulanishi kerak bo'ladi.
myConnectionString = Server=server1, server2; database=testDB;
uid=root; pwd=abc123;";
TCP portini ko'rsatish
myConnectionString="Server=myServerAddress;Port=1234;Datab
ase=testDB;Uid=root;Pwd=abc123;
101
MySQL Connector / Net,
Microsoft mahsulotlarining,
shu jumladan
Microsoft Visual Web Developer-ning Express versiyalarini qo'llab-
quvvatlamaydi.