Save Images in SQL Server using C# Archives - Tech Insights https://reactconf.org/category/save-images-in-sql-server-using-c/ Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Sat, 30 Aug 2014 16:22:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 Save Images in SQL Server using C# Archives - Tech Insights https://reactconf.org/category/save-images-in-sql-server-using-c/ 32 32 230003556 Save Images in SQL Server using C# https://reactconf.org/save-images-in-sql-server/ https://reactconf.org/save-images-in-sql-server/#respond Sat, 30 Aug 2014 16:22:00 +0000 http://www.sqlneed.com/2014/08/30/save-images-in-sql-server-using-c/ This C# program explain how you can store images into SQL Server Database. First you need to Create New Project and give the Name of the project is SaveImage Add …

The post Save Images in SQL Server using C# appeared first on Tech Insights.

]]>

This C# program explain how you can store images into SQL Server Database. First you need to Create New Project and give the Name of the project is SaveImage Add the label, TextBox, command button and picture box control and copy this code and paste. So please change the data source name, database name and Table name.
Please Give the Comments.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.IO;
namespaceSaveImage
{
    public partial class Form1 : Form
    {
        publicForm1()
        {
            InitializeComponent();
        }
        privatevoid CmdBrowse_Click(objectsender, EventArgs e)
        {
            OpenFileDialogdlg = new OpenFileDialog();
            DialogResultdlgresult = dlg.ShowDialog();
            if(dlgresult != DialogResult.Cancel)
            {
                label1.Text = dlg.FileName;
                pictureBox1.ImageLocation = dlg.FileName;
            }
        }
        privatevoid CmdSave_Click(objectsender, EventArgs e)
        {
            byte[] @ImageData = ReadFile(label1.Text);
            SqlConnectioncnn = new SqlConnection(“Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True”);
            stringSql = “INSERT INTO idcard(CustId,PHOTO)VALUES(“ + textBox1.Text  + “,@ImageData) “;
            SqlCommandSqlcmd = new SqlCommand(Sql, cnn);
            Sqlcmd.Parameters.Add(new SqlParameter(“@ImageData”,(object)ImageData));
            cnn.Open();
            Sqlcmd.ExecuteNonQuery();
            cnn.Close();
            MessageBox.Show(“Data Saved Successfully”);
        }
        //Read an image file into a byte array
        byte[] ReadFile(string Path)
        {
            byte[] data = null;
            FileInfoinfo = new FileInfo(Path);
            longNBytes = info.Length;
            FileStreamfstream = new FileStream(Path, FileMode.Open, FileAccess.Read);
            BinaryReaderBread = new BinaryReader(fstream);
            data = Bread.ReadBytes((int)NBytes);
            returndata;
        }
    }
}

The post Save Images in SQL Server using C# appeared first on Tech Insights.

]]>
https://reactconf.org/save-images-in-sql-server/feed/ 0 2239