You must Sign In to post a response.
  • Category: ASP.NET

    How to add imges in database table?

    I want to add a field in my database table in sql where i want to store images, is it possible if yes then please explain to me?
    Thanks in advance.
    Ramesh Deora.
  • #652220
    if you are using oracle then we gave blob, clob and image datatype. you can store the image in all these datatype and use them in your application.

    if you are using SQL Server 2005 then in that database you can get similler type of datatype.

    let me know if you are not able to resolve your problem with the help of former explanation. i will send you the code for storing and retriving the values from database

  • #652228
    Please check the following code:

    Database
    --------


    CREATE TABLE Images(
    ImageID INT IDENTITY(1,1) PRIMARY KEY,
    ImageData IMAGE)


    C# Code Behind
    --------------


    protected void Button1_Click(object sender, EventArgs e)
    {
    if (FileUpload1.HasFile)
    {
    byte[] ImageData = ReadImage(FileUpload1.PostedFile.FileName);
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
    string query = "INSERT INTO Images VALUES(@ImageData)";
    SqlCommand com = new SqlCommand(query, con);
    com.Parameters.AddWithValue("@ImageData", (object)ImageData);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    }
    }

    byte[] ReadImage(string ImagePath)
    {
    byte[] ImageData = null;
    FileInfo fi = new FileInfo(ImagePath);
    long NumberOfBytes = fi.Length;
    FileStream fs = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    ImageData = br.ReadBytes((int)NumberOfBytes);
    return ImageData;
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #652229
    You can take the data type as Var binary to store the image and other binary kind of data. the sample code to insert the image in the data base is


    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
    con.Open();
    byte[] ImgBytes = new byte[flurl.PostedFile.InputStream.Length];
    flurl.PostedFile.InputStream.Read(ImgBytes, 0, ImgBytes.Length);
    string qry = "Insert into Table1(ImageData) values(@ImageData)";
    SqlCommand cmd = new SqlCommand(qry, con);
    cmd.Parameters.AddWithValue("@ImageData", ImgBytes);
    cmd.ExecuteNonQuery();
    con.Close();

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #652313
    Ramesh deora try like this to add in table


    CREATE TABLE [dbo].[imgupload](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ImgName] [varchar](50) NULL,
    [Img] [image] NULL,
    [ImgType] [varchar](50) NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    GO



    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    public partial class ImageUpload : System.Web.UI.Page
    {
    SqlConnection sqlcon=new SqlConnection(@"Server=RAVI\SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;");
    SqlCommand sqlcmd=new SqlCommand();
    String ImageName,ImageType;
    protected void Button1_Click(object sender, EventArgs e)
    {
    if (FileUpload1.HasFile)
    {
    Stream fs = default(Stream);
    fs = FileUpload1.PostedFile.InputStream;
    BinaryReader br1 = new BinaryReader(fs);
    byte[] picture = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);

    ImageName = FileUpload1.FileName;
    ImageType = FileUpload1.PostedFile.ContentType;
    sqlcon.Open();
    SqlCommand sqlcmd = new SqlCommand("insert into imgupload(ImgName,Img,ImgType) values (@Im, @Img, @ImgType)", sqlcon);
    sqlcmd.Parameters.Add("@Im", ImageName);
    sqlcmd.Parameters.Add("@Img", picture);
    sqlcmd.Parameters.Add("@ImgType", ImageType);
    sqlcmd.ExecuteNonQuery();
    sqlcon.Close();
    }
    }
    }


    Let me know if you need further help on this

    Regards
    N.Ravindran
    Your Hard work never fails

  • #652349
    [Response removed by Admin. Read forum policies.]
    Regards,
    Naveen


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.