Reputation: 41
Hy master I have some problem when I convert Image varbinary(MAX)
I have a procedure like this
Create Procedure SelectBarang
as
Begin
Select * from Barang;
End
and i have class model like this
public class ShopDB
{
string cs = ConfigurationManager.ConnectionStrings["ShopEntities"].ConnectionString;
public List<Barang> ListAll()
{
List<Barang> lst = new List<Barang>();
SqlConnection con = new SqlConnection(cs);
//using(SqlConnection con=new SqlConnection(cs))
{
con.Open();
SqlCommand com = new SqlCommand("SelectBarang", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = com.ExecuteReader();
while (rdr.Read())
{
lst.Add(new Barang
{
IdBarang = Convert.ToInt32(rdr["IdBarang"]),
NamaBarang = rdr["NamaBarang"].ToString(),
Harga = Convert.ToInt32(rdr["Harga"]),
CategoriId = Convert.ToInt32(rdr["CategoriId"]),
GambarBarang = Convert ??
});
}
return lst;
}
}
}
How I can change varbinary
to image for Add my data?
Upvotes: 1
Views: 1853
Reputation: 2379
Hi you can do something like
you can read data using:
byte[] myImage = (byte[])reader["MyImageColumn"];
Then use this in mvc view
@{
var base64 = Convert.ToBase64String(Model.ByteArray);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}
Upvotes: 1