How to save or get Images through Database.

原创 2006年05月24日 18:15:00

Recently, I see lots of friends in CSDN ask how to exchange non-text data with Database,

I looked up some materials and summary a sample . I will show you how to save or get images from SQL Server.

I hope it can give you some help.

Firstly, Create a database and a table in your SQL Server ,the table name is MyImages.

I create 3 fields in this table as follows:

1.Identity field that is named as “ID” type of int

2.Name field that is named as “Name”type of varchar

3.Image field that is named as “ImgField”type of Image

Secondly , create an C# Windows Application ,and add two buttons on the main form.

One’s name is “Save” the other’s  “Get”.

Thirdly, add the following namespace which are necessary:

Using System.Data;

Using System.Data.SqlClient;

Using System.IO;

Fourthly, add Save and Get button’s Click event

Save_Click Event:

// Database Connection String

     SqlConnection con = new SqlConnection("Server=YourServer;uid=yourName;pwd=yourpwd;database=yourdb");

SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);

SqlCommandBuilder MyCB = new SqlCommandBuilder(da);

DataSet ds = new DataSet("MyImages");

da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

// Read Bitmap from file

FileStream fs = new FileStream(@"C:/winnt/Gone Your.BMP", FileMode.OpenOrCreate, FileAccess.Read);

// Put the data into byte array

byte[] MyData= new byte[fs.Length];

fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

                       

fs.Close();            

da.Fill(ds,"MyImages");                

DataRow myRow;

// Add a new Row to DataSet

myRow=ds.Tables["MyImages"].NewRow();

myRow["Description"] = "This would be description text";

// Set Img field value

myRow["imgField"] = MyData;

// Insert a record into database

ds.Tables["MyImages"].Rows.Add(myRow);

da.Update(ds, "MyImages");

con.Close();

 

Get_Click Event:

SqlConnection con = new SqlConnection("Server=YourServer;uid=yourName;pwd=yourpwd;database=yourdb");

SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);

SqlCommandBuilder MyCB = new SqlCommandBuilder(da);

DataSet ds = new DataSet("MyImages");

byte[] MyData= new byte[0];

da.Fill(ds, "MyImages");

DataRow myRow;

myRow=ds.Tables["MyImages"].Rows[0];        

MyData =  (byte[])myRow["imgField"];

int ArraySize = new int();

ArraySize = MyData.GetUpperBound(0);

 

FileStream fs = new FileStream(@"C:/winnt/Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(MyData, 0,ArraySize);

fs.Close();

【TED】How to get better at the things you care about?

0:11 Most of us go through life trying to do our best at whatever we do, whether it’s our job, fami...
  • Coding_ZXY
  • Coding_ZXY
  • 2017年02月02日 15:14
  • 4212

[Paper note] Learning from Simulated and Unsupervised Images through Adversarial Training

paper note for SimGAN
  • chn13
  • chn13
  • 2016年12月30日 09:50
  • 504

Android Room

官方文档:https://developer.android.com/topic/libraries/architecture/room.html 一、Room数据库框架结构 1、数据实体对象:作为业...
  • tscyds
  • tscyds
  • 2017年06月26日 16:02
  • 836

使用MySQL和Hibernate时,出现java.lang.UnsupportedOperationException: Update queries only supported through HQL异常的解决方法

在使用mysql和hibernate进行开发时,发现使用native sql进行数据库插入操作时,抛出java.lang.UnsupportedOperationException: Update q...
  • wang12
  • wang12
  • 2009年12月25日 13:08
  • 6218

[ACM] POJ 1442 Black Box (堆,优先队列)

解题思路: 给出一串数字序列,依次添加,问
  • sr19930829
  • sr19930829
  • 2014年07月18日 13:37
  • 2451

对抗学习之Learning from Simulated and Unsupervised Images through Adversarial Training

今天讲一篇cvpr2017的best paper,不是大家熟知的densely connect,是苹果的Learning from Simulated and Unsupervised Images ...
  • dreamer_on_air
  • dreamer_on_air
  • 2017年11月09日 10:11
  • 210

STL--H - Black Box(两个优先队列,求第k小的值)

H - Black Box Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S...
  • u013015642
  • u013015642
  • 2014年08月04日 21:22
  • 1032

Resize images and save thumbnails

public UIImage ResizeImage(UIImage sourceImage, float width, float height) { UIGraphics.BeginIma...
  • lz37025
  • lz37025
  • 2016年03月07日 18:29
  • 113

Docker image批量save和load

docker image批量save和load
  • Tomstrong_369
  • Tomstrong_369
  • 2016年06月22日 17:55
  • 2786

Learn How TO Use SSH To Get Through the GFW

This article show how to use the ssh to ge through the GFW step by step. and it work for me, althou...
  • ImGoTop
  • ImGoTop
  • 2010年03月04日 00:49
  • 990
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to save or get Images through Database.
举报原因:
原因补充:

(最多只允许输入30个字)