- Mesaj
- 740
- Çözümler
- 55
- Beğeni
- 1.661
- Puan
- 1.179
- Ticaret Puanı
- 0
Değerli forum üyeleri ve değerli forum ziyaretcileri Merhabalar,
bugün sizlere C# ile MySQL Bağlantısını göstereceğim. Mysql bağlantısı kontrol etmek içinde kullanılabilinir.
MySQL.cs eklenti:
using MySql.Data.MySqlClient; // MySQL bağlantı
MySQL.cs eklenti:
private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = ok; // Enter'e basıldığında Tamam düğmesi aktif olsun
}
MySQL.cs eklenti:
private void ok_Click(object sender, EventArgs e)
{
string _server = Hostadresi.Text;
string _dbname = DBName.Text;
string _user = UserName.Text;
string _pass = Parola.Text;
string MySQLBaglanti = $"SERVER={_server};DATABASE={_dbname};UID={_user};PWD={_pass}";
if (string.IsNullOrEmpty(Hostadresi.Text) == true || string.IsNullOrEmpty(DBName.Text) == true || string.IsNullOrEmpty(UserName.Text))
{
Mesaj.Text = "Boş bırakmayınız."; // Parola kontrol edilmemektedir.
Mesaj.ForeColor = Color.Red;
MessageBox.Show("Boş bırakmayınız.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
using (var baglan = new MySqlConnection(MySQLBaglanti))
{
try
{
baglan.Open();
MessageBox.Show("Giriş başarıyla yapıldı.", "Bilgilendirme", MessageBoxButtons.OK, MessageBoxIcon.Information);
Mesaj.Text = "Veri tabanına bağlandı.";
Mesaj.ForeColor = Color.Green;
}
catch (Exception Hata)
{
Mesaj.Text = "Veri tabanına bağlanamadı.";
Mesaj.ForeColor = Color.Red;
MessageBox.Show("Veri tabanına bağlanamadı." + Hata.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
MySQL.cs:
using MySql.Data.MySqlClient; // MySQL bağlantı
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MySQL
{
public partial class MySQL : Form
{
public MySQL()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = ok; // Enter'e basıldığında Tamam düğmesi aktif olsun
}
private void ok_Click(object sender, EventArgs e)
{
string _server = Hostadresi.Text;
string _dbname = DBName.Text;
string _user = UserName.Text;
string _pass = Parola.Text;
string MySQLBaglanti = $"SERVER={_server};DATABASE={_dbname};UID={_user};PWD={_pass}";
if (string.IsNullOrEmpty(Hostadresi.Text) == true || string.IsNullOrEmpty(DBName.Text) == true || string.IsNullOrEmpty(UserName.Text))
{
Mesaj.Text = "Boş bırakmayınız."; // Parola kontrol edilmemektedir.
Mesaj.ForeColor = Color.Red;
MessageBox.Show("Boş bırakmayınız.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
using (var baglan = new MySqlConnection(MySQLBaglanti))
{
try
{
baglan.Open();
MessageBox.Show("Giriş başarıyla yapıldı.", "Bilgilendirme", MessageBoxButtons.OK, MessageBoxIcon.Information);
Mesaj.Text = "Veri tabanına bağlandı.";
Mesaj.ForeColor = Color.Green;
}
catch (Exception Hata)
{
Mesaj.Text = "Veri tabanına bağlanamadı.";
Mesaj.ForeColor = Color.Red;
MessageBox.Show("Veri tabanına bağlanamadı." + Hata.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void form1_Shown(object sender, EventArgs e)
{
Hostadresi.Focus();
}
}
}
MySQL.Designer.cs:
namespace MySQL
{
partial class MySQL
{
/// <summary>
///Gerekli tasarımcı değişkeni.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
///Kullanılan tüm kaynakları temizleyin.
/// </summary>
///<param name="disposing">yönetilen kaynaklar dispose edilmeliyse doğru; aksi halde yanlış.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer üretilen kod
/// <summary>
/// Tasarımcı desteği için gerekli metot - bu metodun
///içeriğini kod düzenleyici ile değiştirmeyin.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MySQL));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.Hostadresi = new System.Windows.Forms.TextBox();
this.DBName = new System.Windows.Forms.TextBox();
this.UserName = new System.Windows.Forms.TextBox();
this.Parola = new System.Windows.Forms.TextBox();
this.ok = new System.Windows.Forms.Button();
this.Mesaj = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 18);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(50, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Sunucu :";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 110);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(67, 13);
this.label2.TabIndex = 0;
this.label2.Text = "Veri Tabanı :";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(10, 51);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(52, 13);
this.label3.TabIndex = 0;
this.label3.Text = "Kullanıcı :";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 81);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(34, 13);
this.label4.TabIndex = 0;
this.label4.Text = "Şifre :";
//
// Hostadresi
//
this.Hostadresi.Location = new System.Drawing.Point(88, 18);
this.Hostadresi.Name = "Hostadresi";
this.Hostadresi.Size = new System.Drawing.Size(138, 20);
this.Hostadresi.TabIndex = 0;
//
// DBName
//
this.DBName.Location = new System.Drawing.Point(88, 110);
this.DBName.Name = "DBName";
this.DBName.Size = new System.Drawing.Size(138, 20);
this.DBName.TabIndex = 1;
//
// UserName
//
this.UserName.Location = new System.Drawing.Point(88, 51);
this.UserName.Name = "UserName";
this.UserName.Size = new System.Drawing.Size(138, 20);
this.UserName.TabIndex = 2;
//
// Parola
//
this.Parola.Location = new System.Drawing.Point(88, 81);
this.Parola.Name = "Parola";
this.Parola.PasswordChar = '*';
this.Parola.Size = new System.Drawing.Size(138, 20);
this.Parola.TabIndex = 3;
//
// ok
//
this.ok.Location = new System.Drawing.Point(79, 150);
this.ok.Name = "ok";
this.ok.Size = new System.Drawing.Size(75, 23);
this.ok.TabIndex = 4;
this.ok.Text = "Bağlan";
this.ok.UseVisualStyleBackColor = true;
this.ok.Click += new System.EventHandler(this.ok_Click);
//
// Mesaj
//
this.Mesaj.AutoSize = true;
this.Mesaj.Location = new System.Drawing.Point(65, 177);
this.Mesaj.Name = "Mesaj";
this.Mesaj.Size = new System.Drawing.Size(0, 13);
this.Mesaj.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(241, 196);
this.Controls.Add(this.Mesaj);
this.Controls.Add(this.ok);
this.Controls.Add(this.Parola);
this.Controls.Add(this.UserName);
this.Controls.Add(this.DBName);
this.Controls.Add(this.Hostadresi);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "MySQL Bağlantı";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox Hostadresi;
private System.Windows.Forms.TextBox DBName;
private System.Windows.Forms.TextBox UserName;
private System.Windows.Forms.TextBox Parola;
private System.Windows.Forms.Button ok;
private System.Windows.Forms.Label Mesaj;
}
}