🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / sql e varbinary olarak resim kayıt etme

1-) C# RMOS - sql e varbinary olarak resim kayıt etme

 

1-) C# RMOS - procedure

 

USE [RM_OCR]

GO

/****** Object:  StoredProcedure [dbo].[ocrEkle]    Script Date: 31.12.2018 09:45:44 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER proc [dbo].[ocrEkle](

@ad nvarchar(50) NULL,

@soyad nvarchar(50) NULL,

@ulke nvarchar(50) NULL,

@cinsiyet nvarchar(10) NULL,

@dogumtarihi nvarchar(15) NULL,

@belgeno nvarchar(50) NULL,

@gecerliliktarihi nvarchar(15) NULL,

@uyruk nvarchar(50) NULL,

@belgeturu nvarchar(15) NULL,

@tc nvarchar(50) NULL,

@anaadi nvarchar(50) NULL,

@babaadi nvarchar(50) NULL,

@resimon varbinary(MAX) NULL,

@resimarka varbinary(MAX) NULL,

@odano nvarchar(50) NULL

) as begin

-- exec ocrEkle 'ad','soyad','ulke','cinsiyet','dogumtar','belgeno','gecerlilktar','uyruk','belgeturu','tc','anadi','babaad',0XFF,0XFF

-- exec ocrEkle 'ad','soyad','ulke','cinsiyet','dogumtar','belgeno','gecerlilktar','uyruk','belgeturu','tc','anadi','babaad',NULL,NULL

-- select top 2  * from ocr order by 1 desc

insert into ocr values(

@ad,@soyad,@ulke,@cinsiyet,@dogumtarihi,@belgeno,@gecerliliktarihi,@uyruk,@belgeturu,@tc,@anaadi,@babaadi,@resimon,@resimarka,@odano)

 

end

 

1-) C# RMOS - TÜM KODLAR ARASINDAN BULURSUN

 

 using System;

using System.Windows.Forms;

using RMOS_OCR.Library;

using System.IO;

using System.Drawing;

using System.Collections;

using System.Text;

using System.Data.SqlClient;

using System.Collections.Generic;

 

namespace RMOS_OCR

{

    public partial class MyOcr : DevExpress.XtraEditors.XtraForm

    {

        public MyOcr()

        {

            InitializeComponent();

        }

 

 

        public static MyOcr Form1;

 

 

        public static string MyClass = "MyOcr";

 

        public static void MyNewInstance(Form f1)

        {

            try

            {

                if (Form1 != null)

                {

                    Form1.BringToFront();

                    Form1.Focus();

                    return;

                }

                Form1 = new MyOcr();

                Form1.MdiParent = f1;

                Form1.Show();

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "MyNewInstance", "", ex);

            }

 

        }

 

        private void MyOcr_Load(object sender, EventArgs e)

        {

            MyLoad();

        }

 

        public void MyLoad()

        {

            try

            {

                gridControl1.DataSource = RHVeritabani.MyGetDataTable("select * from ocr order by 1 desc");

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "MyLoad", "", ex);

            }

        }

 

        public void MyClearText()

        {

            try

            {

                txtAd.Text = "";

                txtAnaAdi.Text = "";

                txtBabaAdi.Text = "";

                txtBelgeNo.Text = "";

                txtBelgeTur.Text = "";

                txtCinsiyet.Text = "";

                txtDogumTar.Text = "";

                txtGecerlilikTar.Text = "";

                txtSoyad.Text = "";

                txtTc.Text = "";

                txtUlke.Text = "";

                txtUyruk.Text = "";

                txtResimOn.Text = "";

                txtOdaNo.Text = "";

                pictureBoxOn.Image = null;

                pictureBoxArka.Image = null;

                txtAd.Select();

                txtAd.Focus();

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "MyClearText", "", ex);

            }

        }

 

        private void MyOcr_FormClosed(object sender, FormClosedEventArgs e)

        {

            Form1 = null;

        }

        byte[] Deger;

        private void MyOcr_KeyDown(object sender, KeyEventArgs e)

        {

            try

            {

                if (e.KeyCode == Keys.F1)

                {

                    MyClearText();

                }

                if (e.Alt && e.KeyCode == Keys.R)

                {

                    txtResimOn.Select();

                    txtResimOn.Focus();

                }

                if (e.Alt && e.KeyCode == Keys.Y)

                {

                    txtResimOn.Select();

                    txtResimOn.Focus();

 

                    Deger = RHVeritabani.MyGetItemBinary("resim", @"select

                    case when resimarka IS  NULL  then resimon else resimarka end as resim

                    from ocr where id=" + txtResimOn.Text);

                    var ms = new MemoryStream(Deger);

                    if (pictureBoxOn.Image == null)

                    {

                        pictureBoxOn.Image = byteArrayToImage(Deger);

                    }

                    else

                    {

                        pictureBoxArka.Image = byteArrayToImage(Deger);

                    }

                }

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "MyOcr_KeyDown", "", ex);

            }

        }

 

        private void btnTemizle_Click(object sender, EventArgs e)

        {

            MyClearText();

        }

 

        private void txtBabaAdi_Leave(object sender, EventArgs e)

        {

            txtAd.Select();

        }

        public byte[] imageToByteArray(System.Drawing.Image imageIn)

        {

            MemoryStream ms = new MemoryStream();

            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            return ms.ToArray();

        }

 

        public Image byteArrayToImage(byte[] byteArrayIn)

        {

            MemoryStream ms = new MemoryStream(byteArrayIn);

            Image returnImage = Image.FromStream(ms);

            return returnImage;

        }

        List<SqlParameter> sp;

        private void btnKaydet_Click(object sender, EventArgs e) // PROCEDURE ÇEVİRDİM

        {

            try

            {

                if (pictureBoxOn.Image == null && pictureBoxArka.Image == null)

                {

                    sp = new List<SqlParameter>(){

    new SqlParameter() {ParameterName = "@ad", Value= txtAd.Text},

    new SqlParameter() {ParameterName = "@soyad", Value= txtSoyad.Text},

    new SqlParameter() {ParameterName = "@ulke", Value= txtUlke.Text},

    new SqlParameter() {ParameterName = "@cinsiyet", Value= txtCinsiyet.Text},

    new SqlParameter() {ParameterName = "@dogumtarihi", Value= txtDogumTar.Text},

    new SqlParameter() {ParameterName = "@belgeno", Value= txtBelgeNo.Text},

    new SqlParameter() {ParameterName = "@gecerliliktarihi", Value= txtGecerlilikTar.Text},

    new SqlParameter() {ParameterName = "@uyruk", Value= txtUyruk.Text},

    new SqlParameter() {ParameterName = "@belgeturu", Value= txtBelgeTur.Text},

    new SqlParameter() {ParameterName = "@tc", Value= txtTc.Text},

    new SqlParameter() {ParameterName = "@anaadi", Value= txtAnaAdi.Text},

    new SqlParameter() {ParameterName = "@babaadi", Value= txtBabaAdi.Text},

    new SqlParameter() {ParameterName = "@resimon", Value=System.Data.SqlTypes.SqlBinary.Null},

    new SqlParameter() {ParameterName = "@resimarka", Value= System.Data.SqlTypes.SqlBinary.Null},

    new SqlParameter() {ParameterName = "@odano", Value= txtOdaNo.Text}

 

                };

                }

                else if (pictureBoxOn.Image != null && pictureBoxArka.Image == null)

                {

                    sp = new List<SqlParameter>(){

    new SqlParameter() {ParameterName = "@ad", Value= txtAd.Text},

    new SqlParameter() {ParameterName = "@soyad", Value= txtSoyad.Text},

    new SqlParameter() {ParameterName = "@ulke", Value= txtUlke.Text},

    new SqlParameter() {ParameterName = "@cinsiyet", Value= txtCinsiyet.Text},

    new SqlParameter() {ParameterName = "@dogumtarihi", Value= txtDogumTar.Text},

    new SqlParameter() {ParameterName = "@belgeno", Value= txtBelgeNo.Text},

    new SqlParameter() {ParameterName = "@gecerliliktarihi", Value= txtGecerlilikTar.Text},

    new SqlParameter() {ParameterName = "@uyruk", Value= txtUyruk.Text},

    new SqlParameter() {ParameterName = "@belgeturu", Value= txtBelgeTur.Text},

    new SqlParameter() {ParameterName = "@tc", Value= txtTc.Text},

    new SqlParameter() {ParameterName = "@anaadi", Value= txtAnaAdi.Text},

    new SqlParameter() {ParameterName = "@babaadi", Value= txtBabaAdi.Text},

    new SqlParameter() {ParameterName = "@resimon", Value=imageToByteArray(pictureBoxOn.Image) } ,

    new SqlParameter() {ParameterName = "@resimarka", Value=System.Data.SqlTypes.SqlBinary.Null },

    new SqlParameter() {ParameterName = "@odano", Value= txtOdaNo.Text} };

                }

                else if (pictureBoxOn.Image != null && pictureBoxArka.Image != null)

                {

                    sp = new List<SqlParameter>(){

    new SqlParameter() {ParameterName = "@ad", Value= txtAd.Text},

    new SqlParameter() {ParameterName = "@soyad", Value= txtSoyad.Text},

    new SqlParameter() {ParameterName = "@ulke", Value= txtUlke.Text},

    new SqlParameter() {ParameterName = "@cinsiyet", Value= txtCinsiyet.Text},

    new SqlParameter() {ParameterName = "@dogumtarihi", Value= txtDogumTar.Text},

    new SqlParameter() {ParameterName = "@belgeno", Value= txtBelgeNo.Text},

    new SqlParameter() {ParameterName = "@gecerliliktarihi", Value= txtGecerlilikTar.Text},

    new SqlParameter() {ParameterName = "@uyruk", Value= txtUyruk.Text},

    new SqlParameter() {ParameterName = "@belgeturu", Value= txtBelgeTur.Text},

    new SqlParameter() {ParameterName = "@tc", Value= txtTc.Text},

    new SqlParameter() {ParameterName = "@anaadi", Value= txtAnaAdi.Text},

    new SqlParameter() {ParameterName = "@babaadi", Value= txtBabaAdi.Text},

    new SqlParameter() {ParameterName = "@resimon", Value=imageToByteArray(pictureBoxOn.Image)},

    new SqlParameter() {ParameterName = "@resimarka", Value= imageToByteArray(pictureBoxArka.Image)},

    new SqlParameter() {ParameterName = "@odano", Value= txtOdaNo.Text}};

                }

                RHVeritabani.MySetQuery_P("ocrEkle", sp);

 

                MyLoad();

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "btnKaydet_Click", "", ex);

            }

        }

 

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)

        {

            try

            {

                int seciliSatir = gridView1.FocusedRowHandle;

                if (seciliSatir < 0)

                {

                    RHMesaj.MyMessageInformation("Lütfen Satır Seçiniz!");

                    return;

                }

                txtAd.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["ad"]).ToString();

                txtSoyad.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["soyad"]).ToString();

                txtUlke.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["ulke"]).ToString();

                txtCinsiyet.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["cinsiyet"]).ToString();

                txtDogumTar.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["dogumtarihi"]).ToString();

                txtBelgeNo.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["belgeno"]).ToString();

                txtGecerlilikTar.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["gecerliliktarihi"]).ToString();

                txtUyruk.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["uyruk"]).ToString();

                txtBelgeTur.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["belgeturu"]).ToString();

                txtTc.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["tc"]).ToString();

                txtAnaAdi.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["anaadi"]).ToString();

                txtBabaAdi.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["babaadi"]).ToString();

                txtOdaNo.Text = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["odano"]).ToString();

 

                var degerOn = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["resimon"]);

                var degerArka = gridView1.GetRowCellValue(seciliSatir, gridView1.Columns["resimarka"]);

                byte[] resimon, resimarka; pictureBoxOn.Image = null; pictureBoxArka.Image = null;

                if (!degerOn.ToString().Equals(""))

                {

                    resimon = (byte[])degerOn;

                    pictureBoxOn.Image = byteArrayToImage(resimon);

                }

                if (!degerArka.ToString().Equals(""))

                {

                    resimarka = (byte[])degerArka;

                    pictureBoxArka.Image = byteArrayToImage(resimarka);

                }

                //byte[] resimon = (byte[])gridView1.GetRowCellValue(0, "resimon");

                //MemoryStream ms = new MemoryStream(resimon);

                //pictureBoxOn.Image = Image.FromStream(ms);

            }

            catch (Exception ex)

            {

                RHMesaj.MyMessageError(MyClass, "gridView1_FocusedRowChanged", "", ex);

            }

        }

    }

}

 2021 Ocak 18 Pazartesi
 476