如何在用户使用ASP.NET中的C#将图像文件上载到SQL Server 2005时动态插入图像?这是为了让用户在我的网络应用中上传他们的个人资料照片。与使用C#的Windows应用程序完成它有什么不同?将用户配置文件映像保存到数据库中
1
A
回答
2
在灯架上有一个一吨的在网络上的例子:
http://aspalliance.com/138
http://www.4guysfromrolla.com/articles/120606-1.aspx
http://www.aspfree.com/c/a/ASP.NET/Uploading-Images-to-a-Database--C---Part-I/
你应该能够遵循任何这些实现的目标你要。
1
与在WinForms中一样。获取byte[]
和image
列。但我强烈建议使用文件系统来存储图片。 DB用于关系数据,文件系统用于原始字节。
+1
是的,我同意安德烈,如果你正在做的是试图存储用户个人资料图片,上传他们的目录并存储他们的来源。想象一下从数据库查询大量的用户个人资料图片......你会有巨大的表现点击! – 2010-09-16 13:37:56
0
下面是在C#中将图像插入数据库的代码示例。你会粗糙的需要支持表的图片应该是一个字节字段,并保持图片类型,以便您可以稍后检索图像来显示它。除此之外,您需要将文件输入框与提交按钮一起放在页面上。
public void AddImage(object sender, EventArgs e)
{
int intImageSize;
String strImageType;
Stream ImageStream;
FileStream fs = File.OpenRead(Request.PhysicalApplicationPath + "/Images/default_image.png");
Byte[] ImageContent;
if (PersonImage.PostedFile.ContentLength > 0)
{
intImageSize = PersonImage.PostedFile.ContentLength;
strImageType = PersonImage.PostedFile.ContentType;
ImageStream = PersonImage.PostedFile.InputStream;
ImageContent = new Byte[intImageSize];
int intStatus;
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
}
else
{
strImageType = "image/x-png";
ImageContent = new Byte[fs.Length];
fs.Read(ImageContent, 0, ImageContent.Length);
}
SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
SqlCommand objCmd;
string strCmd;
strCmd = "INSERT INTO ImageTest (Picture, PictureType) VALUES (@Picture, @PictureType)";
objCmd = new SqlCommand(strCmd, objConn);
SqlParameter prmPersonImage = new SqlParameter("@Picture", SqlDbType.Image);
prmPersonImage.Value = ImageContent;
objCmd.Parameters.Add(prmPersonImage);
objCmd.Parameters.AddWithValue("@PictureType", strImageType);
lblMessage.Visible = true;
try
{
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();
lblMessage.Text = "ImageAdded!";
}
catch
{
lblMessage.Text = "Error occured the image has not been added to the database!";
}
}
相关问题
- 1. 将文件保存到数据库中
- 2. 保存用户配置文件图片到数据库使用PHP myql分贝
- 3. 将图像保存到数据库中
- 4. MySQL将文件保存到数据库
- 5. LINQ:将文件保存到数据库
- 6. 将tmp文件保存到数据库
- 7. 将SQLite数据库保存到文件?
- 8. 将图像文件保存到数据库中
- 9. 将图像文件保存到数据库中
- 10. 将数据保存到配置文件模型不起作用
- 11. 数据库:用户配置文件
- 12. 如何将图像保存到文件中并使用MVC3将图像名称保存到数据库?
- 13. 将数据存储到配置文件
- 14. 保存在数据库映像
- 15. 将图像保存到文件目录中vs将图像保存到数据库中
- 16. Android:将图像保存到数据库
- 17. 将图像保存到数据库MVC3
- 18. 将'modules/addons'的配置保存到数据库中
- 19. 将用户配置文件保存到模型
- 20. 将配置保存到数据库 - 键值存储
- 21. C#将KeyCode保存到配置文件
- 22. 将memcache数据保存到文件或数据库中
- 23. 将用户配置文件信息保存在与用户登录信息相同的数据库中
- 24. SharedPreferences保存用户配置文件
- 25. Django:保存用户配置文件
- 26. 保存到数据库中而不是保存到文件中?
- 27. 保护nodeJS数据库配置文件
- 28. 我应该将图像二进制数据保存到数据库还是将图像保存为文件?
- 29. 将配置文件映像存储为文件url或Base64?
- 30. 保存图像文件SQL数据库
你确定要插入数据库而不是将其上传到目录吗? – 2010-09-16 13:08:04