Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL
First of all i created a db called travel_Direction and there is a table called myMapDB
 
so i created a column called image_Upload which holds binary (varbinary(max))
 

so i basically want to upload an image to that particular column
 

so someone help me to do the code.. i'm new to this field.
 

here's c# my code
 
protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read
               (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
 
            // Create SQL Connection 
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";
 

            SqlCommand DbPath = new SqlCommand();
            DbPath.CommandText = "INSERT INTO MyMapDb(image_Upload)" + " VALUES (@image_Upload)";
            DbPath.CommandType = CommandType.Text;
            DbPath.Connection = con;
 

            SqlParameter ImageUpload = new SqlParameter("@image_Upload", SqlDbType.Image, imageSize.Length);
            ImageUpload.Value = imageSize;
 
            ImageUpload.Value = imageSize;
            DbPath.Parameters.Add(ImageUpload);
            con.Open();
            DbPath.ExecuteNonQuery();
            con.Close();
 
           
                Image1.DataBind(); // image will load to this image controller
            }
 

 
        }
 

please be kind to help me..... big help
Posted 2-Jul-13 2:28am
Comments
ryanb31 at 2-Jul-13 7:54am
   
What doesn't work? There are lots of examples online and even on this site of how to save an image to binary into sql.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
you can try this out:
 
public void saveImageInDataBase(int imageID)
{
byte[] imageData = ReadImageFile(textBox1.Text); //This nethod returns image in byte array format.
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=aaaa;User ID=sa;Password=abcd;Initial Catalog=Workbook"; //provide connection string of your server.
con.Open(); //open connection to server.
string query = "insert into ImageTable values(@imageId,@imageData)"; //create a query variable.
SqlCommand cmd = new SqlCommand(query, con); //create a sqlcommand object.
cmd.Parameters.Add(new SqlParameter("@imageId", imageID)); //add first parameters.
cmd.Parameters.Add(new SqlParameter("@imageData", imageData)); //add second parameters.
int rows = cmd.ExecuteNonQuery(); //execute query.
if (rows > 0)
MessageBox.Show("Image saved.");
else
MessageBox.Show("Unable to save image.");
con.Close();
}
 

public byte[] ReadImageFile(string imageLocation)
{
byte[] imageData = null;
FileInfo fileInfo = new FileInfo(imageLocation);
long imageFileLength = fileInfo.Length;
FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imageData = br.ReadBytes((int)imageFileLength);
return imageData;
}
  Permalink  
Comments
promod madushan at 2-Jul-13 8:26am
   
thank you bro!!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I would not personally recommend to use the BLOB to save image in the database. You can upload the file and save it in a folder on your server and save the path in the DB.
 
Then you can call the path in the image source property of the control or tag
 
Using BLOB to save images increases the Database size and thus the overhead.
  Permalink  
Comments
srinvias kumar lumeris at 2-Jul-13 8:20am
   
i will also suggest this mechanism, otherwise degrade in performance of the application
promod madushan at 2-Jul-13 10:04am
   
thanks for your thoughts.......
Boipelo at 2-Jul-13 14:25pm
   
...but when some idiot delete the folder, you are dead. You might have backups but new images will be gone. I will rather have another DB for images, if there will be too many images uploads.
promod madushan at 2-Jul-13 22:27pm
   
taht's correct!!!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 2 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100