C#从SQL server数据库中读取l图片和存入图片

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

一、从图片中获得二进制值的基本方法:Image.Save 方法 (String, ImageFormat)
这会将保存 Image 写入指定的文件中指定的格式。

命名空间: System.Drawing
程序集: System.Drawing(位于 System.Drawing.dll)

语法:
public void Save( string filename, ImageFormat format)
参数
filename
Type: System.String
一个字符串,包含要保存此文件的名称 Image。

format
Type: System.Drawing.Imaging.ImageFormat
用于此 ImageFormat 的 Image。(包括.JGP、png等格式)

异常

ArgumentNullException: filename 或 format 是 null.

ExternalException: 使用错误的图像格式保存图像。- 或 - 图像已保存到同一文件从创建它。

二、Bitmap.Save 方法
命名空間: System.Drawing
組件: System.Drawing (於 System.Drawing.dll)

多載清單:
1、Save(Stream, ImageCodecInfo, EncoderParameters) :使用指定的編碼器和影像編碼器參數,將此影像儲存至指定的資料流。(繼承自 Image。)
2、Save(Stream, ImageFormat) 將這個影像以指定的格式儲存至指定的資料流。(繼承自 Image。)
3、Save(String) 這會將儲存 Image 到指定的檔案或資料流。(繼承自 Image。)
4、Save(String, ImageCodecInfo, EncoderParameters) 這會將儲存 Image 至指定的檔案,以指定的編碼器和影像編碼器參數。(繼承自 Image。)
5、Save(String, ImageFormat) 這會將儲存 Image 至指定的檔案中指定的格式。(繼承自 Image。)

三、将图片保存在数据库和从数据库中还原程序的示例:

//存图片
private void button1_Click(object sender, EventArgs e)
{
#region //从文件中读取图片
FileStream fs = new FileStream(@"D:\i\2017年资料\SPC\SPC\30.jpg", FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
SqlConnection mycn = new SqlConnection("server=192.168.1.14;database=fengyp;uid=sa;pwd=");
mycn.Open();
SqlCommand com = new SqlCommand("insert into dbo.试验图片存取 values(10,@ImageList)", mycn);

        com.Parameters.Add("ImageList", SqlDbType.Image);

        com.Parameters["ImageList"].Value = imagebytes;

        com.ExecuteNonQuery();

        mycn.Close();
                     #endregion

                    #region   //从picturebox中读取图片 
                     //将Image转换成流数据,并保存为byte[] 
        MemoryStream mstream = new MemoryStream();
        pictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
        byte[] bytes = new Byte[mstream.Length];
        mstream.Position = 0;
        mstream.Read(bytes, 0, bytes.Length);
        mstream.Close();
                    SqlConnection mycn = new   SqlConnection("server=192.168.1.14;database=fengyp;uid=sa;pwd=");
        mycn.Open();

SqlCommand com = new SqlCommand("insert into dbo.试验图片存取 values(11,@ImageList)", mycn);

"ImageList""ImageList"#endregion//从数据库读取图片并还原private void button2_Click(object sender, EventArgs e)

    bytenull//打开数据库new"server=192.168.1.14;database=fengyp;uid=sa;pwd="new"select 图片 from dbo.试验图片存取 where 序号=10"whilebyte0newnew,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
2月前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
|
4月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
|
4月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(下)
本文接续前文,深入讲解了在Windows环境下使用C#和ADO.NET操作南大通用GBase 8s数据库的方法。通过Visual Studio 2022创建项目,添加GBase 8s的DLL引用,并提供了详细的C#代码示例,涵盖数据库连接、表的创建与修改、数据的增删查改等操作,旨在帮助开发者提高数据库管理效率。
|
5月前
|
SQL 缓存 大数据
C#高效处理大数据的批次处理,以及最好的数据库设计
C#高效处理大数据的批次处理,以及最好的数据库设计
224 0
|
6月前
|
SQL 存储 监控
如何在SQL中插入图片
如何在SQL中插入图片
|
6月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
6月前
|
SQL 网络协议 数据库连接
已解决:连接SqlServer出现 provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程【C#连接SqlServer踩坑记录】
本文介绍了解决连接SqlServer时出现“provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程”错误的步骤,包括更改服务器验证模式、修改sa用户设置、启用TCP/IP协议,以及检查数据库连接语句中的实例名是否正确。此外,还解释了实例名mssqlserver和sqlserver之间的区别,包括它们在默认设置、功能和用途上的差异。
|
6月前
|
SQL 存储 关系型数据库
C#一分钟浅谈:使用 ADO.NET 进行数据库访问
【9月更文挑战第3天】在.NET开发中,与数据库交互至关重要。ADO.NET是Microsoft提供的用于访问关系型数据库的类库,包含连接数据库、执行SQL命令等功能。本文从基础入手,介绍如何使用ADO.NET进行数据库访问,并提供示例代码,同时讨论常见问题及其解决方案,如连接字符串错误、SQL注入风险和资源泄露等,帮助开发者更好地利用ADO.NET提升应用的安全性和稳定性。
472 6
|
7月前
|
SQL 安全 Java
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
620 0

热门文章

最新文章