Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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
Comments
ZurdoDev 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.

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;
}
 
Share this answer
 
Comments
promod madushan 2-Jul-13 8:26am    
thank you bro!!!!
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.
 
Share this answer
 
Comments
srinvias kumar lumeris 2-Jul-13 8:20am    
i will also suggest this mechanism, otherwise degrade in performance of the application
promod madushan 2-Jul-13 10:04am    
thanks for your thoughts.......
Boipelo 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900