ARTICLE

How to insert an image in database SQL Server with VB.NET.

Posted by Rohatash Kumar Articles | Visual Basic 2010 October 18, 2010
In this article we will learn how to insert an image in database SQL Server with VB.NET.
 
Reader Level:

In this article you will learn how to insert an image from database SQL Server

Inserting an image in database

Creating a table in SQL Server.

Table name student and database name master.

use master

go

create table student

(

name varchar(20),

rollno int,

photo image

);

go

select * from student;

go

Table has created in SQL Server.

Now come to the front end window forms application in VB.NET.

Taking two level, two textbox and three button and one picture box and opendialog control on the form.

database1.gif

Figure 1.

Now double click on the button named browse and add the following code in c#.

private void button1_Click(object sender, EventArgs e)

        {

            openFileDialog1.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp";

            openFileDialog1.ShowDialog();

            pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

        }

 

code in visual Basic.

 

private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files|*.gif;*.jpg;*.png;*.bmp";
openFileDialog1.ShowDialog();
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}

 

Now double click on the button save and add the following c# code.

private void button2_Click(object sender, EventArgs e)

        {

            string str = "Data Source=.;uid=sa;pwd=wintellect;database=master";

            SqlConnection con = new SqlConnection(str);

            con.Open();

            string sql = "INSERT INTO student VALUES(@rollno,@name,@photo)";

            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.Parameters.AddWithValue("@rollno", textBox1.Text);

            cmd.Parameters.AddWithValue("@name", textBox2.Text);

 

            MemoryStream ms = new MemoryStream();

            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);

            byte[] data = ms.GetBuffer();

            SqlParameter p = new SqlParameter("@photo", SqlDbType.Image);

            p.Value = data;

            cmd.Parameters.Add(p);

            cmd.ExecuteNonQuery();

            MessageBox.Show("record has been saved","Save", MessageBoxButtons .OK);

            con.Close();

 

        }

code in visual Basic.

 

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)

Dim str As String = "Data Source=.;uid=sa;pwd=wintellect;database=master"

        Dim con As New SqlConnection(str)

        con.Open()

        Dim sql As String = "INSERT INTO student VALUES(@rollno,@name,@photo)"

        Dim cmd As New SqlCommand(sql, con)

        cmd.Parameters.AddWithValue("@rollno", textBox1.Text)

        cmd.Parameters.AddWithValue("@name", textBox2.Text)

        Dim ms As New MemoryStream()

        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat)

        Dim data As Byte() = ms.GetBuffer()

        Dim p As New SqlParameter("@photo", SqlDbType.Image)

        p.Value = data

        cmd.Parameters.Add(p)

        cmd.ExecuteNonQuery()

        MessageBox.Show("record has been saved", "Save", MessageBoxButtons.OK)

        con.Close()

    End Sub

 

Now execute the application press ctrl+F5.


database2.gif

Figure 2.

Now click on the save button message will be display.

database3.gif

Now open the sql server record has been saved with the image

Login to add your contents and source code to this article
share this article :
post comment
 

that's great but i have quastion how can i load this pic from database in my picturebox at form

Posted by jamal ahmed Jan 10, 2012

please more details or add samples

Posted by piragar janarthanan Jan 25, 2011

Like I mentioned earlier, this works on a local machine but does not work on a network. Any solution please?

Posted by Samuel Okoye Dec 15, 2010

frd simply create a table in database and on the form browse image. it will be appear on the image box.

Posted by Rohatash Kumar Dec 14, 2010

Which data type have you used.

Posted by Rohatash Kumar Dec 14, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor