using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace VeritabaniBaglantisi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connStr = "server=localhost;user=root;database=kutuphane;port=3306;password='';"; MySqlConnection conn = new MySqlConnection(connStr); try { MessageBox.Show("Veritabanına bağlanıyor."); conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { MessageBox.Show("Veritabanına bağlandı."); string sql = "SELECT COUNT(*) FROM kullanicilar WHERE kullaniciAdi=@uname AND parola=@pass"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd.Parameters.AddWithValue("@uname", kullaniciAdi.Text); cmd.Parameters.AddWithValue("@pass", parola.Text); int result = Convert.ToInt32(cmd.ExecuteScalar()); // Eğer veritabanında bu kullanıcıadı ve şifresine sahip kullanıcıların count değeri 0'dan büyükse: if (result > 0) { // Form2 form2 = new Form2(kullaniciAdi.Text); Form2 form2 = new Form2(); form2.Show(); this.Hide(); } else { MessageBox.Show("Kullanıcı adı veya parola hatalı"); } } else { MessageBox.Show("Veritabanı bağlantımız açık değil."); } } catch (Exception Ex) { MessageBox.Show("Bir hata var: " + Ex.Message); } finally { conn.Close(); } } } }