欢迎光临
我的个人博客网站

C#与sql进行图片存取

 1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Linq;  7 using System.Text;  8 using System.Threading.Tasks;  9 using System.Windows.Forms; 10  11 using System.IO; 12 using System.Data.SqlClient; 13  14  15  16 namespace 图片存取 17 { 18     public partial class 实例 : Form 19     { 20         public 实例() 21         { 22             InitializeComponent(); 23         } 24         public static string str = "server=192.168.0.102;database=mysql;uid=sa;pwd=a123456"; 25         private void 实例_Load(object sender, EventArgs e) 26         { 27              28         } 29     30         /// <summary> 31         ///  32         /// </summary> 33         /// <param name="path"></param> 34         public static void InsertImg(string path) 35         { 36  37             byte[] bytes = File.ReadAllBytes(path); 38             SqlConnection con = new SqlConnection(str); 39             con.Open(); 40             SqlCommand cmd = new SqlCommand("insert into table_2 values(@image)",con); 41             cmd.Parameters.Add("@image", SqlDbType.Image).Value = bytes; 42             cmd.ExecuteNonQuery(); 43             con.Close(); 44  45             //cmd.Dispose(); 46  47         } 48  49         public static void ReadImg(string path) 50         { 51             SqlConnection con = new SqlConnection(str); 52             con.Open(); 53             SqlCommand cmd = new SqlCommand("select image from table_2 ",con); 54             object scalar = cmd.ExecuteScalar(); 55             byte[] bytes = (byte[])scalar; 56  57             File.WriteAllBytes(path, bytes); 58             con.Close(); 59             //cmd.Dispose(); 60  61         } 62  63         private void button1_Click(object sender, EventArgs e) 64         { 65             InsertImg(@"C: UsersAdministratorDesktop8113-2.jpg"); 66             ReadImg(@"C: UsersAdministratorDesktop8113-2-1.jpg"); 67         } 68     } 69 }

C#与sql进行图片存取

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » C#与sql进行图片存取
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏