【发布时间】:2012-03-22 01:47:44
【问题描述】:
我想知道如何在 SQL Server 表中创建图像列并使用 VB.NET 在 ASP.NET 中检索它
create table Tbl(ID int primary key,Name varchar(20),Image ....)
【问题讨论】:
标签: asp.net sql sql-server vb.net sql-server-2008
我想知道如何在 SQL Server 表中创建图像列并使用 VB.NET 在 ASP.NET 中检索它
create table Tbl(ID int primary key,Name varchar(20),Image ....)
【问题讨论】:
标签: asp.net sql sql-server vb.net sql-server-2008
您想使用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();
【讨论】:
【讨论】:
在 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();
}
【讨论】: