How to create an image column in a SQL Server table and retrieve it in ASP.NET using VB

20,008

Solution 1

You want to use the VARBINARY(MAX) to store this data.

As to the code side, here is a good, brief answer.

From the answer:

FileStream st = new FileStream(@"C:\filename.jpg", FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();

SqlConnection conn = new SqlConnection("...");
SqlCommand cmd = new SqlCommand(
    "UPDATE SomeTable SET image=@image WHERE ID = 1", conn);
cmd.Parameters.AddWithValue("@image", buffer);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();

Solution 2

Save and Retrieve Files from SQL Server Database using ASP.Net

Storing and Retrieving Images/Files In Sql Server - VB.NET

Share:
20,008
Yassine Kira
Author by

Yassine Kira

Updated on December 27, 2020

Comments

  • Yassine Kira
    Yassine Kira over 3 years

    I want to know how to create an image column in a SQL Server table and retrieve it in ASP.NET using VB.NET

    create table Tbl(ID int primary key,Name varchar(20),Image ....)
    
  • Yassine Kira
    Yassine Kira about 12 years
    pllz Justin how can i directly add an image in sql column without usin any VB method ?
  • Yassine Kira
    Yassine Kira about 12 years
    pllz JotaBa how can i directly add an image in sql without usin any VB method ?
  • Justin Pihony
    Justin Pihony about 12 years
  • Yassine Kira
    Yassine Kira about 12 years
    i want to insert the image in a column of a table alredy have a 5 columns i try this but it does not work insert into dbo.Produit values('Pc portable','HP EliteBook série p','Un ordinateur professionnel robuste de 35,5 et 39,6 cm (14.0" et 15.6") à fonctions multiples, hautes performances et longue autonomie',SELECT * FROM OPENROWSET(BULK N'C:\Users\Yassine-Kira\Desktop\Templates\ProductImg\elite-b‌​ook_tcm_133_1096796.‌​png', SINGLE_BLOB) ,20,4999,0);
  • Yassine Kira
    Yassine Kira about 12 years
    tnkks my bro ^^ i want to insert the image in a column of a table alredy have a 5 columns i try this but it does not work insert into dbo.Produit values('Pc portable','HP EliteBook série p','Un ordinateur professionnel robuste de 35,5 et 39,6 cm (14.0" et 15.6") à fonctions multiples, hautes performances et longue autonomie',SELECT * FROM OPENROWSET(BULK N'C:\Users\Yassine-Kira\Desktop\Templates\ProductImg\elite-b‌​ook_tcm_133_1096796.‌​png', SINGLE_BLOB) ,20,4999,0);
  • Shrieks
    Shrieks about 12 years
    Is this what you are looking for: stackoverflow.com/questions/416881/…