X
  • On1 Mayıs 2015
Categories: C#ProjelerYazılım

C# Mesajlaşma Programı

Projemiz C# da basit bir mesajlaşma uygulaması.Veri tabanı olarak MySql kullandım.Projemizi ADO.NET ile MySql’le bagladık.Veritabanına baglanmak için Referans bölümünden MySql eklemezseniz Veritabanına baglanamazsınız.Onun içinde Connector yüklemeniz gerekiyor.
Projemizin Giriş Ekranı:Form1

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 MySql.Data.MySqlClient;
namespace WindowsFormsApplication25
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string login;
        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection cnn = new MySqlConnection("Server=localhost;Database=odev;Uid=root;Pwd='';");
            int id=-1;
            try
            {
                cnn.Open();

                string sql = "SELECT * FROM kullanici WHERE kul_adi=@kad and sifre=@s";

                MySqlCommand cmd = new MySqlCommand(sql, cnn);
                cmd.Parameters.Add(new MySqlParameter("@kad", textBox1.Text));
                cmd.Parameters.Add(new MySqlParameter("@s", textBox2.Text));

                DataTable dt = new DataTable();
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(dt);
              
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    id = Convert.ToInt32(dr["kul_id"].ToString());
                }


                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("Hoşgeldin ");
                }
                else
                {
                    MessageBox.Show("Veritabanında böyle bir kullanıcı bulunamadı"); return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            login = "@kad";
            this.Visible = false;
            Form2 form2 = new Form2(id);
            form2.Show();
        }
    }
}

Giriş yaptıktak sonra görüceginiz ekran:Form2

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 MySql.Data.MySqlClient;
namespace WindowsFormsApplication25
{
    public partial class Form2 : Form
    {
        int kID;
        public Form2(int gelenid)
        {
            InitializeComponent();
            kID = gelenid;
        }

        int[] idler = new int[100];
        int[] secilenID = new int[100];

        private void Form2_Load(object sender, EventArgs e)
        {
            MySqlConnection cnn = new MySqlConnection("Server=localhost;Database=odev;Uid=root;Pwd='';");
            cnn.Open();
            MySqlCommand cmd = new MySqlCommand("select * from kullanici", cnn);
            MySqlDataReader dr = cmd.ExecuteReader();
            int say=0;
            while (dr.Read())
            {
                comboBox1.Items.Add(dr["kul_adi"]);
                idler[say] = Convert.ToInt32(dr["kul_id"].ToString());
                say++;
            }

            cnn.Close(); 
        }

        int eklenen = 0;
       
        private void button2_Click(object sender, EventArgs e)
        {

            if (eklenen > 0)
            {
                textBox2.Text += " , ";
                
            }
            textBox2.Text += comboBox1.SelectedItem.ToString();
            secilenID[eklenen] = idler[comboBox1.SelectedIndex];
            eklenen++;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sonuc = textBox2.Text;
           
            string[] dizi = sonuc.Split(',');
            MySqlConnection cnn = new MySqlConnection("Server=localhost;Database=odev;Uid=root;Pwd='';");
            cnn.Open();
            MySqlCommand komut = new MySqlCommand();
            komut.Connection = cnn;
            
            for (int i = 0; i < dizi.Length; i++)
            {

                komut.CommandText = "Insert Into mesaj (tarih,mesaj_icerik,kul_id,gon_kul_id,okundu) VALUES (@tarih,@sonuc,@kul_id,@dizi,false)";
                
                komut.Parameters.AddWithValue("@sonuc", textBox1.Text.ToString());
                komut.Parameters.AddWithValue("@kul_id", kID);
                komut.Parameters.AddWithValue("@tarih", DateTime.Now);
                komut.Parameters.AddWithValue("@dizi", secilenID[i]);
                komut.ExecuteNonQuery();
                komut.Parameters.Clear();
            }
            cnn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
            groupBox2.Visible = false;
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            groupBox2.Visible = true;
            MySqlConnection baglanti = new MySqlConnection("Server=localhost;Database=odev;Uid=root;Pwd='';Convert Zero Datetime=True;Allow Zero Datetime=True");
            baglanti.Open();
            MySqlDataAdapter da = new MySqlDataAdapter("select *from mesaj", baglanti);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            baglanti.Close();
        }
    }
}

Projenin tamamını BURDAN indirebilirsiniz.

Hilal Saim: Namık Kemal Üniversitesi/Bilgisayar Mühendisliği