Copying image file or any file from one directory to another and to save the filename to database

In this article we will be learning how to copy image file or any file form one folder to another and to save the filename to database.

Now there are situation when we need to copy a filename to different directory or a specified directory from your user application e.g. Suppose there is an application which copies you photos and picture file to a specific directory and access those files from that directory. In situations like this your application need to save the files to the specific intended directory.

So, let's see how we can achieve that.

I am doing two things here:

  1. Copying the file to specific directory.
  2. Saving the filename to a table in database.

Below is my form:

We need a table to be created in order to save the file into that table.

Code is as follows:

CREATE TABLE [dbo].[blog_pic](

    [picture] [varchar](max) NOT NULL


Stored procedure for insertion into table:

CREATE PROCEDURE [dbo].[blog_insert_picture]


@picture varchar(50),

@msg varchar(100) output


    -- Add the parameters for the stored procedure here


begin try


BEGIN tran

insert into blog_pic values(@picture);


select @msg='Insertion Successful'

return 0



End try


BEGIN catch



    select @msg='Some Problem Ocured In Processing'


    return 1

END catch

C # code:

We are using "dot net test folder for in out operations" directory to transfer our file from different directories and its location in my system is "F:\dotnet test folder for in out operations\". You can have your own location and your own directory.We are using Filecopy method to copy our file to the desired directory which in my case is "dotnet test folder for in out operations".

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 System.IO;

using System.Data.SqlClient;

namespace blog


public partial class Form4_pictures : Form


SqlConnection con;

string constr = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=blog;Data Source=Your_Database_Name";

string ntfic;

SqlCommand cmd;

string fulllocimage;

string filename ;

string fileextflg;


public Form4_pictures()





private void button1_Click(object sender, EventArgs e)




fulllocimage = openFileDialog1.FileName;

filename = openFileDialog1.SafeFileName;

pictureBox1.ImageLocation = fulllocimage;

textBox1.Text = filename;



private void button2_Click(object sender, EventArgs e)





File.Copy(fulllocimage, @"F:\dotnet test folder for in out operations\" + textBox1.Text);


con = new SqlConnection();

con.ConnectionString = constr;


cmd = new SqlCommand("blog_insert_picture", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@picture", SqlDbType.NVarChar).Value = textBox1.Text;


SqlParameter param = new SqlParameter("@returnval", SqlDbType.Int);

param.Size = 50;

param.Direction = ParameterDirection.ReturnValue;

//add to parameter to collection



SqlParameter param1 = new SqlParameter("@msg", SqlDbType.VarChar);

param1.Size = 50;

param1.Direction = ParameterDirection.Output;

//add to parameter to collection




label1.Text = cmd.Parameters["@msg"].Value.ToString();

ntfic = cmd.Parameters["@msg"].Value.ToString() + "--" + cmd.Parameters["@returnval"].Value.ToString();


catch (Exception ex)


if (textBox1.Text == "")


MessageBox.Show("Error!" + "--" + "You must browse the picture first");




MessageBox.Show(ntfic+"--"+ex.ToString() );








So far I have explained how to copy the file from one directory to another and to save the filename to database, but in my next article I will continue to explain how to access the picture from the directory through the filename which is stored in database.