如何加载图片在sql server中

lrjt1980 2009-10-09 06:41:55
我不知道如何在sql server中存储图片,那位给我仔细的讲一下步骤。
...全文
75 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
waterzouzou 2009-10-11
  • 打赏
  • 举报
回复
学习一下
wuyq11 2009-10-09
  • 打赏
  • 举报
回复
保存路径到数据库
或保存二进制到到Image字段
using(SqlConnection conn=new SqlConnection(ConnectString))
{
conn.Open();
string strsql = "select photo from testimage where id=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strsql;
SqlDataReader rd = cmd.ExecuteReader();
byte[] data = null;
if (rd.Read())
{
Response.Clear();
data = (byte[])rd["photo"];
Response.BinaryWrite(data);
Response.End();
}
}
Stream ImageStream;
string Path = FileUpload1.PostedFile.FileName;
int Size = FileUpload1.PostedFile.ContentLength;
string Type = FileUpload1.PostedFile.ContentType;
ImageStream = FileUpload1.PostedFile.InputStream;
byte[] Content = new byte[Size];
int Status = ImageStream.Read(Content, 0, Size);

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand comm = new SqlCommand("insert into testimage(name,photo,Path,Type)values(@Name,@photo,@Path,@Type)", conn);

comm.CommandType = CommandType.Text;
comm.Parameters.Add("@Name", SqlDbType.VarChar, 255).Value = txtUserName.Text;
comm.Parameters.Add("@photo", SqlDbType.Image).Value = Content;
comm.Parameters.Add("@Path", SqlDbType.VarChar, 255).Value = Path;
comm.Parameters.Add("@Type", SqlDbType.VarChar, 255).Value = Type;

conn.Open();
comm.ExecuteNonQuery();
conn.Close();

SK_Aqi 2009-10-09
  • 打赏
  • 举报
回复
不要存bit型在数据库中,存路径就可以了
这样项目建一个文件夹下,没有图片都有路径了存起来

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧