1.      private void Form1_Load(object sender, EventArgs e)
  2. {
  3. #region 保存数据库
  4. string url = @"C:\Users\Administrator\Desktop\tiger.jpg";
  5. byte[] dd = GetPictureData(url);
  6.  
  7. kkkkk(dd);//保存到数据库中
  8. #endregion
  9.  
  10. Read();// 从数据库中读取保存到 C盘
  11.  
  12. }
  13. /// <summary>
  14. /// 保存到数据库
  15. /// </summary>
  16. /// <param name="imgBytesIn"></param>
  17. private void kkkkk(byte[] imgBytesIn)
  18. {
  19. try
  20. {
  21. SqlConnection con = new SqlConnection("server=192.168.1.118,1433;uid=sa;pwd=123456;database=aa");
  22. con.Open();
  23. SqlCommand cmd = new SqlCommand("insert into aaaa (img) values( @Image ) ;", con);
  24. cmd.Parameters.Add("@Image", SqlDbType.Image);
  25. cmd.Parameters["@Image"].Value = imgBytesIn;
  26. cmd.ExecuteNonQuery();
  27.  
  28. con.Close();
  29. MessageBox.Show("图片上传成功");
  30.  
  31. }
  32. catch
  33. {
  34. MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  35.  
  36. }
  37.  
  38. }
  39. /// <summary>
  40. /// 从数据库中 图片 并且保存到C盘
  41. /// </summary>
  42. public void Read()
  43. {
  44. byte[] MyData = new byte[];
  45. using (SqlConnection conn = new SqlConnection("server=192.168.1.118,1433;uid=sa;pwd=123456;database=aa"))
  46. {
  47. conn.Open();
  48. SqlCommand cmd = new SqlCommand();
  49. cmd.Connection = conn;
  50. cmd.CommandText = "select * from aaaa";
  51. SqlDataReader sdr = cmd.ExecuteReader();
  52. sdr.Read();
  53. object o = sdr["img"];
  54. MyData = (byte[])sdr["img"];//读取第一个图片的位流
  55. int ArraySize= MyData.GetUpperBound();//获得数据库中存储的位流数组的维度上限,用作读取流的上限
  56.  
  57. FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
  58. fs.Write(MyData, , ArraySize);
  59. fs.Close(); //-- 写入到c:\00.jpg。
  60. conn.Close();
  61. Console.WriteLine("读取成功");//查看硬盘上的文件
  62. }
  63. }
  64.  
  65. /// <summary>
  66. /// 根据路径将图片转换成 byte[]
  67. /// </summary>
  68. /// <param name="imagepath"></param>
  69. /// <returns></returns>
  70. public byte[] GetPictureData(string imagepath)
  71. {
  72. /**/
  73. ////根据图片文件的路径使用文件流打开,并保存为byte[]
  74. FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
  75. byte[] byData = new byte[fs.Length];
  76. fs.Read(byData, , byData.Length);
  77. fs.Close();
  78. return byData;
  79. }

C#中 将图片保存到Sql server 中的更多相关文章

  1. C#图片转换成二进制流并且保存到sql server数据库

    注意:我要存储文件二进制流的列的类型是text,不是image类型. 我已经实现了从数据库中读取text类型的二进制流,,现在就是不知道怎么存进去. 我的部分关键代码: StreamReader sr ...

  2. 019. Asp.net将SqlServer中的数据保存到xls/txt中

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  3. asp.net c#整理所有本地的图片一次性保存到SQL表中

    string sql1 = "select distinct tx from tiku where tx is not null"; //检索tx表中所有的不重复的tx值 stri ...

  4. C#中执行存储过程并在SQL server中调试

    1.编写存储过程 ) drop PROCEDURE [dbo].[sp_calcPci_of_baseRcd_GTmpTbl] CREATE PROCEDURE [dbo].[sp_calcPci_o ...

  5. c# 将datatable中的数据保存到excel文件中

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  6. Excel文件数据保存到SQL中

    1.获取DataTable /// <summary> /// 查询Excel文件中的数据 /// </summary> /// <param name="st ...

  7. Session如何保存在sql数据库中

    aspnet中,session默认以inproc模式存储,也就是保存在iis进程中,这样有个优点就是效率高,但不利于为本负载均衡扩展.可以把session信息保存在SQL Server中,据说,该种方 ...

  8. [转]细说SQL Server中的加密

    简介 加密是指通过使用密钥或密码对数据进行模糊处理的过程.在SQL Server中,加密并不能替代其他的安全设置,比如防止未被授权的人访问数据库或是数据库实例所在的Windows系统,甚至是数据库所在 ...

  9. SQL Server中关于跟踪(Trace)那点事

    前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中“跟踪”也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充足的证据来破案. 简 ...

随机推荐

  1. Rsync + sersync 实时同步备份

    一      Rsync + Sersync  实时同步介绍 1.Rsync 服务搭建介绍 云机上搭建Rsync server,在本地搭建Rsync Clinet. 2. Sersync 服务搭建介绍 ...

  2. 小A的柱状图

    链接 [https://ac.nowcoder.com/acm/contest/549/H] 题意 [] 分析 很显然你必须找到该高度下往左右找到第一个高度比该位置小的.这个区间的宽*该高度.就当前能 ...

  3. c++ 套路多

    1. 浅拷贝带来的多次析构问题 参见:https://www.cnblogs.com/33debug/p/6657730.html 解决方案,深拷贝.强烈建议自定义拷贝构造函数为深拷贝,否则可能会给自 ...

  4. vue.js 列表追加项写法

    <ul id="app"> <template v-for="site in sites"> <li>{{ site.nam ...

  5. codeforces510D

    Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...

  6. codeforces342B

    Xenia and Spies CodeForces - 342B Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined u ...

  7. 内置函数(sorted、map、enumerate、filter、reduce)

    1.sorted() 语法: sorted(iterable, cmp=None, key=None, reverse=False) 把iterable中的items进行排序之后,返回一个新的列表,原 ...

  8. 【集训队作业2018】矩阵玩小凹 NTT

    题目大意 有一个 \(n\times m\) 的矩阵 \(A\),每个元素都是 \([0,1]\) 内的等概率随机实数,记 \(s_i=\sum_{j=1}^mA_{i,j}\),求 \(\lfloo ...

  9. jatoolsprinter html实现每隔几秒获取数据直接后台打印不弹窗

    1.流程说明 jatoolspringter 必须要能在html代码里面看到  id =page1 page2 page3..... 才能打印,所以无法动态打印,必须先把要打印的内容放到页面某个地方隐 ...

  10. js获取当前url

    1.window.location.href(设置或获取整个 URL 为字符串) 2.window.location.protocol(设置或获取 URL 的协议部分) 3.window.locati ...