C#中 将图片保存到Sql server 中
- private void Form1_Load(object sender, EventArgs e)
- {
- #region 保存数据库
- string url = @"C:\Users\Administrator\Desktop\tiger.jpg";
- byte[] dd = GetPictureData(url);
- kkkkk(dd);//保存到数据库中
- #endregion
- Read();// 从数据库中读取保存到 C盘
- }
- /// <summary>
- /// 保存到数据库
- /// </summary>
- /// <param name="imgBytesIn"></param>
- private void kkkkk(byte[] imgBytesIn)
- {
- try
- {
- SqlConnection con = new SqlConnection("server=192.168.1.118,1433;uid=sa;pwd=123456;database=aa");
- con.Open();
- SqlCommand cmd = new SqlCommand("insert into aaaa (img) values( @Image ) ;", con);
- cmd.Parameters.Add("@Image", SqlDbType.Image);
- cmd.Parameters["@Image"].Value = imgBytesIn;
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("图片上传成功");
- }
- catch
- {
- MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- }
- /// <summary>
- /// 从数据库中 图片 并且保存到C盘
- /// </summary>
- public void Read()
- {
- byte[] MyData = new byte[];
- using (SqlConnection conn = new SqlConnection("server=192.168.1.118,1433;uid=sa;pwd=123456;database=aa"))
- {
- conn.Open();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.CommandText = "select * from aaaa";
- SqlDataReader sdr = cmd.ExecuteReader();
- sdr.Read();
- object o = sdr["img"];
- MyData = (byte[])sdr["img"];//读取第一个图片的位流
- int ArraySize= MyData.GetUpperBound();//获得数据库中存储的位流数组的维度上限,用作读取流的上限
- FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
- fs.Write(MyData, , ArraySize);
- fs.Close(); //-- 写入到c:\00.jpg。
- conn.Close();
- Console.WriteLine("读取成功");//查看硬盘上的文件
- }
- }
- /// <summary>
- /// 根据路径将图片转换成 byte[]
- /// </summary>
- /// <param name="imagepath"></param>
- /// <returns></returns>
- public byte[] GetPictureData(string imagepath)
- {
- /**/
- ////根据图片文件的路径使用文件流打开,并保存为byte[]
- FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
- byte[] byData = new byte[fs.Length];
- fs.Read(byData, , byData.Length);
- fs.Close();
- return byData;
- }
C#中 将图片保存到Sql server 中的更多相关文章
- C#图片转换成二进制流并且保存到sql server数据库
注意:我要存储文件二进制流的列的类型是text,不是image类型. 我已经实现了从数据库中读取text类型的二进制流,,现在就是不知道怎么存进去. 我的部分关键代码: StreamReader sr ...
- 019. Asp.net将SqlServer中的数据保存到xls/txt中
using System; using System.Collections; using System.Configuration; using System.Data; using System. ...
- asp.net c#整理所有本地的图片一次性保存到SQL表中
string sql1 = "select distinct tx from tiku where tx is not null"; //检索tx表中所有的不重复的tx值 stri ...
- C#中执行存储过程并在SQL server中调试
1.编写存储过程 ) drop PROCEDURE [dbo].[sp_calcPci_of_baseRcd_GTmpTbl] CREATE PROCEDURE [dbo].[sp_calcPci_o ...
- c# 将datatable中的数据保存到excel文件中
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- Excel文件数据保存到SQL中
1.获取DataTable /// <summary> /// 查询Excel文件中的数据 /// </summary> /// <param name="st ...
- Session如何保存在sql数据库中
aspnet中,session默认以inproc模式存储,也就是保存在iis进程中,这样有个优点就是效率高,但不利于为本负载均衡扩展.可以把session信息保存在SQL Server中,据说,该种方 ...
- [转]细说SQL Server中的加密
简介 加密是指通过使用密钥或密码对数据进行模糊处理的过程.在SQL Server中,加密并不能替代其他的安全设置,比如防止未被授权的人访问数据库或是数据库实例所在的Windows系统,甚至是数据库所在 ...
- SQL Server中关于跟踪(Trace)那点事
前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中“跟踪”也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充足的证据来破案. 简 ...
随机推荐
- Rsync + sersync 实时同步备份
一 Rsync + Sersync 实时同步介绍 1.Rsync 服务搭建介绍 云机上搭建Rsync server,在本地搭建Rsync Clinet. 2. Sersync 服务搭建介绍 ...
- 小A的柱状图
链接 [https://ac.nowcoder.com/acm/contest/549/H] 题意 [] 分析 很显然你必须找到该高度下往左右找到第一个高度比该位置小的.这个区间的宽*该高度.就当前能 ...
- c++ 套路多
1. 浅拷贝带来的多次析构问题 参见:https://www.cnblogs.com/33debug/p/6657730.html 解决方案,深拷贝.强烈建议自定义拷贝构造函数为深拷贝,否则可能会给自 ...
- vue.js 列表追加项写法
<ul id="app"> <template v-for="site in sites"> <li>{{ site.nam ...
- codeforces510D
Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...
- codeforces342B
Xenia and Spies CodeForces - 342B Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined u ...
- 内置函数(sorted、map、enumerate、filter、reduce)
1.sorted() 语法: sorted(iterable, cmp=None, key=None, reverse=False) 把iterable中的items进行排序之后,返回一个新的列表,原 ...
- 【集训队作业2018】矩阵玩小凹 NTT
题目大意 有一个 \(n\times m\) 的矩阵 \(A\),每个元素都是 \([0,1]\) 内的等概率随机实数,记 \(s_i=\sum_{j=1}^mA_{i,j}\),求 \(\lfloo ...
- jatoolsprinter html实现每隔几秒获取数据直接后台打印不弹窗
1.流程说明 jatoolspringter 必须要能在html代码里面看到 id =page1 page2 page3..... 才能打印,所以无法动态打印,必须先把要打印的内容放到页面某个地方隐 ...
- js获取当前url
1.window.location.href(设置或获取整个 URL 为字符串) 2.window.location.protocol(设置或获取 URL 的协议部分) 3.window.locati ...