简体   繁体   English

如何使用VB.Net/C#将图像从SQL数据库导出到特定文件夹?

[英]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.

相关问题 如何在VB.NET中使用SQL从特定数据库中检索Ms访问表名称 - How to retrieve Ms access tables names from specific database using SQL in VB.NET 如何从SQL Server数据库中检索图像字段civilimage1,civilimage2,以在带有vb.net或c#的ASP.NET中使用图像控件进行显示? - How to retrieve image fields civilimage1,civilimage2 from SQL Server database to show using image control in ASP.NET with vb.net or c#? 使用vb.net或C#更改文件夹的跟踪 - change tracking for folder using vb.net or C# 在asp.net(vb.net/c#)中从ms sql数据库填充数据的更好方法 - Better way for populating data from ms sql database in asp.net (vb.net/c#) 如何使用C#/ Vb.Net在Windows Server 2003及更高版本上“删除-保护”文件或文件夹? - How to **delete-protect** a file or folder on Windows Server 2003 and onwards using C#/Vb.Net? 如何在C#项目的App_Code文件夹中一起使用VB.NET类 - How to Using VB.NET classes together in the App_Code folder for C# project C#/VB.Net 如何折叠列表中的特定值 - C#/VB.Net How to collapse specific values in a list 使用C#/ VB.NET的SQL Server Profiler实现 - SQL Server Profiler implementation using C#/VB.NET Windows 中特定文件夹的预加载文件夹图标 C# 或 VB.NET 中的图标缓存 - Preload folder icon for a specific folder in Windows Icon cache, in C# or VB.NET 如何将特定的代码行从 C# 转换为 VB.net - How to translate a specific line of code from C# to VB.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM