Reputation: 29
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 ....)
Upvotes: 2
Views: 22364
Reputation: 3667
in the sql server create the column with type: image
.
and in the asp, use this example code: (it's in c#)
string con = "your connection string"
var Image = (from A in YourTblImage
select A).First();//to get one record of the table use the `first`
FileStream fs = new FileStream(@"yourPath to save the image", FileMode.Create);
try
{
System.Data.Linq.Binary img = image.imageColumnName;
byte[] imageK = img.ToArray();
fs.Write(imageK, 0, imageK.Length);
}
catch (Exception ex)
{
string str = ex.Message;
}
finally
{
fs.Close();
}
Upvotes: 0
Reputation: 39055
Save and Retrieve Files from SQL Server Database using ASP.Net
Storing and Retrieving Images/Files In Sql Server - VB.NET
Upvotes: 1
Reputation: 67115
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();
Upvotes: 4