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 ....)
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 ....)
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();
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();
}