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

AS

begin try

begin

BEGIN tran

insert into blog_pic values(@picture);

commit

select @msg='Insertion Successful'

return 0

end

 

End try

 

BEGIN catch

    

    SET NOCOUNT ON;

    select @msg='Some Problem Ocured In Processing'

    rollback

    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()

{

InitializeComponent();

}

 

private void button1_Click(object sender, EventArgs e)

{

openFileDialog1.ShowDialog();

 

fulllocimage = openFileDialog1.FileName;

filename = openFileDialog1.SafeFileName;

pictureBox1.ImageLocation = fulllocimage;

textBox1.Text = filename;

}

 

private void button2_Click(object sender, EventArgs e)

{

try

{

 

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

 

con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

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

cmd.Parameters.Add(param);

//***********************************************************************

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

param1.Size = 50;

param1.Direction = ParameterDirection.Output;

//add to parameter to collection

cmd.Parameters.Add(param1);

//*************************************************************************

cmd.ExecuteNonQuery();

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");

}

else

{

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.