我想知道如何在SQL Server表中创建一个图像列,并使用VB.NET在ASP.NET中检索它
create table Tbl(ID int primary key,Name varchar(20),Image ....)
答案 0 :(得分:4)
您想使用VARBINARY(MAX)存储此数据。
关于代码方,here is a good, brief 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();
答案 1 :(得分:1)
答案 2 :(得分:0)
在sql server中创建类型为image
的列。
在asp中,使用这个示例代码:(它在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();
}