C# ile MySQL Bağlantısı

Doğukan Taydaş

Moderatör
Moderatör
Premium Üye
Geliştirici
Yardımsever Üye
Mesaj
740
Çözümler
55
Beğeni
1.661
Puan
1.179
Ticaret Puanı
0
:mmt-hakkinda:
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:
Genişlet Daralt Kopyala
using MySql.Data.MySqlClient; // MySQL bağlantı
MySQL.cs eklenti:
Genişlet Daralt Kopyala
private void Form1_Load(object sender, EventArgs e)
        {
            this.AcceptButton = ok; // Enter'e basıldığında Tamam düğmesi aktif olsun
        }
MySQL.cs eklenti:
Genişlet Daralt Kopyala
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:
Genişlet Daralt Kopyala
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:
Genişlet Daralt Kopyala
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;
    }
}

:mmt-resimler:

1.PNG

Boş bırakmayınız.

Mysql bağlantı hatası.

Mysql veri tabanına bağlandı.
 

Dosya Eklentileri

Yönetim paneli yapılabilir aslında , p2p ile bağlantı sağlayabiliyor muyuz peki c# üzerinden.
 
Yönetim paneli yapılabilir aslında , p2p ile bağlantı sağlayabiliyor muyuz peki c# üzerinden.

Daha önce şöyle bir şey denemiştim arşivimde buldum işe yarar mı bilmiyorum fakat deneyebilirsin ya da bunun üzerinden bir ihtimal ilerleyebilirsin,

C#:
Genişlet Daralt Kopyala
using System.Net.Sockets;

public static (int total, int empire1, int empire2, int empire3, int local) P2PSendServer(string yazi, string tip="NOTICE", int port=13000)
{

    string serverIp = "192.168.1.45";
    string password = "admin_page pass p2p";


    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    try
    {
        s.Connect(serverIp, port);
    }
    catch (SocketException e)
    {
        Console.WriteLine($"\n Bağlantı hatası. Hata : {e.Message}\n");
        return (0, 0, 0, 0, 0);
    }

    if (tip == "USER_COUNT")
    {
        byte[] query = Encoding.ASCII.GetBytes($"@{tip}\n");
        s.Send(query);
    }
    else
    {
        byte[] query2 = Encoding.ASCII.GetBytes($"@{password}\n");
        s.Send(query2);
        s.Receive(new byte[256]);
        byte[] query = Encoding.ASCII.GetBytes($"@{tip} {yazi}\n");
        s.Send(query);
    }

    byte[] result1 = new byte[256];
    s.Receive(result1);

    if (tip == "USER_COUNT")
    {
        string result = Encoding.ASCII.GetString(result1);
        string[] parts = result.Split();
        int total = int.Parse(parts[0]);
        int empire1 = int.Parse(parts[1]);
        int empire2 = int.Parse(parts[2]);
        int empire3 = int.Parse(parts[3]);
        int local = int.Parse(parts[4]);
        return (total, empire1, empire2, empire3, local);
    }
    else
    {
        return Encoding.ASCII.GetString(result1);
    }
}

Sorgu:
Genişlet Daralt Kopyala
var result = P2PSendServer("USER_COUNT");
Console.WriteLine(result);
 
Güzel paylaşım , önü geliştirmeye açık bir oyun olsa (yüksek kalitede iş çıkarılmaya değer bir oyun) aslında API yazılarak mobile , desktop ve web app yazılabilir. Ama maalesef değmez :)
 
Uyarı: Bu konu açıldığından bu yana baya zaman geçmiş.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.
Geri
Üst