C# ve SQL Server ile Resim Yükleme ve Login İşlemi Form1.cs (AnaForm) Durhan GABRALI Nahit Menteşe Mes. ve Tek. And. Lisesi Bilişim Teknolojileri Alanı - 2016 1
using System.Data.SqlClient; using System.IO; public partial class Form1 : Form static public string userid; static public string user; public Form1() private void kullanıcıekletoolstripmenuitem_click(object sender, EventArgs e) KullaniciEkle frm = new KullaniciEkle(); frm.showdialog(); private void Form1_Load(object sender, EventArgs e) FormGiris frm = new FormGiris(); frm.showdialog(); toolstriplabel1.text = "Kulalnıcı: " + user; //Sql Veritabanı ve Resim Okuma işlemleri SqlConnection bag = new SqlConnection("Data \\verideposu.mdf;integrated Security=True"); SqlCommand kmt = new SqlCommand("SELECT * FROM [Table] WHERE id=@id", bag); kmt.parameters.add("@id", userid); SqlDataReader dr; dr = kmt.executereader(); Image KullResim = null; while (dr.read()) byte[] resim = (byte[])dr[3]; //Okuyucu ile üzerine tıkladığımız üyenin resmini byte dizisi tanımlayıp içine atıyoruz. if (resim!= null) MemoryStream ms = new MemoryStream(resim, 0, resim.length); // System.IO isim uzayı altındaki MemoryStream sınıfıyla oluşturduğumuz byte dizisi için bir akım oluşturuyoruz. ms.write(resim, 0, resim.length); KullResim = Image.FromStream(ms, true); // Oluşturduğumuz akım üzerinden aldığımızı image imize atıyoruz. picturebox1.image = KullResim; label1.text = "ID : " + dr[0].tostring(); label2.text = "Kullanıcı : " + dr[1].tostring(); catch (Exception ex) MessageBox.Show(ex.Message.ToString()); finally bag.close(); private void programdançıkıştoolstripmenuitem_click(object sender, EventArgs e) Durhan GABRALI Nahit Menteşe Mes. ve Tek. And. Lisesi Bilişim Teknolojileri Alanı - 2016 2
FormGiris.cs Application.Exit(); using System.Data.SqlClient; public partial class FormGiris : Form SqlConnection bag = new SqlConnection(); SqlCommand komut = new SqlCommand(); SqlDataReader dr; public FormGiris() private void button2_click(object sender, EventArgs e) Application.Exit(); private void FormGiris_Load(object sender, EventArgs e) bag.connectionstring = "Data \\verideposu.mdf;integrated Security=True"; if (bag.state == ConnectionState.Open) label3.text = "Bağlantı tamam."; label3.text = "Bağlantı yok."; catch label3.text = "Bağlantı yok."; private void button1_click(object sender, EventArgs e) komut.commandtext = "SELECT * FROM [Table] WHERE Kulladi=@p1 AND sifre=@p2"; komut.connection = bag; komut.parameters.add("@p1", textbox1.text); komut.parameters.add("@p2", textbox2.text); dr = komut.executereader(); if (dr.hasrows) dr.read(); // login tamam Form1.user = textbox1.text; Form1.userid = dr[0].tostring(); Durhan GABRALI Nahit Menteşe Mes. ve Tek. And. Lisesi Bilişim Teknolojileri Alanı - 2016 3
this.close(); MessageBox.Show("Kullanıcı yada Şifre Hatalı!"); catch MessageBox.Show("Veritabanı ile ilgili sorun var! Daha sonra tekrar deneyiniz."); KullaniciEkle.cs using System.Data.SqlClient; using System.IO; public partial class KullaniciEkle : Form string resimpath; public KullaniciEkle() private void KullaniciEkle_Load(object sender, EventArgs e) label4.text = Application.StartupPath + "\\resimyok.jpg"; private void button1_click(object sender, EventArgs e) openfiledialog1.title = "Resim Aç"; openfiledialog1.filter = "Jpeg Dosyası (*.jpg) *.jpg Gif Dosyası (*.gif) *.gif Png Dosyası (*.png) *.png Tif Dosyası (*.tif) *.tif"; if (openfiledialog1.showdialog() == DialogResult.OK) picturebox1.image = Image.FromFile(openFileDialog1.FileName); resimpath = openfiledialog1.filename.tostring(); label4.text = resimpath; resimpath = Application.StartupPath + "\\resimyok.jpg"; label4.text = resimpath; private void button2_click(object sender, EventArgs e) byte[] resim; if (openfiledialog1.filename == "openfiledialog1") resimpath = Application.StartupPath + "\\resimyok.jpg"; //Resimimizi FileStream metoduyla okuma modunda açıyoruz. Durhan GABRALI Nahit Menteşe Mes. ve Tek. And. Lisesi Bilişim Teknolojileri Alanı - 2016 4
FileStream fs = new FileStream(resimPath, FileMode.Open, FileAccess.Read); //BinaryReader ile byte dizisi ile FileStream arasında veri akışı sağlanıyor. BinaryReader br = new BinaryReader(fs); /*ReadBytes ile FileStreamde belirtilen resim dosyasındaki byte lar byte dizisine aktarılıyor. */ resim = br.readbytes((int)fs.length); br.close(); fs.close(); //Sql Veritabanı ve Kayıt işlemleri string bagstr = "Data \\verideposu.mdf;integrated Security=True"; SqlConnection bag = new SqlConnection(bagstr); SqlCommand kmt = new SqlCommand("INSERT INTO [Table](Kulladi,Sifre,Resim) Values (@p1,@p2,@p3) ", bag); kmt.parameters.add("@p3", SqlDbType.Image, resim.length).value = resim; kmt.parameters.add("@p1", textbox1.text); kmt.parameters.add("@p2", textbox2.text); int kontrol; kontrol = kmt.executenonquery(); if (kontrol == 1) MessageBox.Show(" Veritabanına kayıt yapıldı."); textbox1.text = ""; textbox2.text = ""; picturebox1.image = Image.FromFile(Application.StartupPath + "\\resimyok.jpg"); label4.text = Application.StartupPath + "\\resimyok.jpg"; catch (Exception ex) MessageBox.Show(ex.Message.ToString()); finally bag.close(); Durhan GABRALI Nahit Menteşe Mes. ve Tek. And. Lisesi Bilişim Teknolojileri Alanı - 2016 5