[英]How to export Images from SQL database to a specific folder using VB.Net/C#?
I have this SQL database "Student" and I want to export images(all or selected) from it's table "StudentPic" to a specific folder with ".jpg" extension. 我有这个SQL数据库“ Student”,我想从表“ StudentPic”中将图像(全部或选定的)导出到扩展名为“ .jpg”的特定文件夹中。
This is my work and it didn't work. 这是我的工作,没有成功。 Any comments about this.? 关于这个有什么意见吗?
Dim sql As String = " SELECT TOP 10 StudID,StudentPic FROM Student"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
conn.Open
Dim dr As SqlDataReader = cmd.ExecuteReader
While dr.Read
Dim bytes() As Byte = CType(dr("StudentPic"),Byte())
Dim memStream As MemoryStream = New MemoryStream(bytes)
Try
Dim MyImage As Bitmap = New Bitmap(memStream)
MyImage = New Bitmap(MyImage, 200, 250)
MyImage.Save((dr("StudID") + ".jpg"), ImageFormat.Jpeg)
Catch ex As Exception
Throw ex
End Try
End While
You can select the images from the table with a query, then save the images to disk using .net image functions. 您可以通过查询从表中选择图像,然后使用.net图像功能将图像保存到磁盘。 It's not too difficult, but as has been mentioned, it's not a good idea to expect code to be written here. 不太困难,但是如上所述,期望在此处编写代码不是一个好主意。 I would dig into it in .net and if you get stuck, then post your questions here (with details). 我将在.net中进行深入研究,如果您遇到问题,请在此处(详细信息)发表您的问题。 Good luck! 祝好运!
You can retrive image to a byte array then write to file using FileStream. 您可以将图像检索到字节数组,然后使用FileStream写入文件。 Here is Example. 这是示例。
Retrieving images from the database is the exact reverse process of saving images to the database. 从数据库检索图像是将图像保存到数据库的完全相反的过程。
SqlCommand cmd = new SqlCommand("select Pic from StudentPic where StudentID=@ID",
new SqlConnection("conn stirng"));cmd.Parameters.AddWithValue("@ID",'id of a student');cmd.Connection.Open();
execute “ExecuteScalar” because we want only “IMAGE” column data back. 执行“ ExecuteScalar”,因为我们只希望返回“ IMAGE”列数据。
byte[] byteImg=(byte[])cmd.ExecuteScalar();cmd.Connection.Close();
Save this data to a Folder. 将此数据保存到文件夹。
string strPath=Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\StudentPics\\"+"picName.jpg";
FileStream fs=new FileStream(strFileTime,FileMode.CreateNew,FileAccess.Write);
fs.Write(byteImg,0,byteImg.Length);fs.Flush();fs.Close();
Hopefully helps it. 希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.