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();

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HOW TO GET ORACLE DATABASE IDENTIFIER (DBID)

DBID stands for database identifier. Which is a unique identifier for each oracle database running.w...
  • t0nsha
  • t0nsha
  • 2012-06-06 14:33
  • 1146

How To Mark Or Get The Highlighted String Inside UIWebView

Before we go over with this tutorial, I would like to thank everyone for reading my posts, for askin...

How to get the visible region of a window or CDC(转自http://www.programmersheaven.com)

If one is dealing with an application that performs a lot of painting or drawing, the OnDraw functio...

Export GridView with Images from database to Word Excel and PDF Formats

转自: http://www.aspsnippets.com/Articles/Export-GridView-with-Images-from-database-to-Word-Excel-and-...
  • lJean
  • lJean
  • 2011-10-18 17:55
  • 775

神奇的图像处理算法(Amazing algorithms to enhance or transform images)

Scriptol列出了几种神奇的图像处理算法,让我们一起来看一下。 一、像素图生成向量图的算法 数字时代早期的图片,分辨率很低。尤其是一些电子游戏的图片,放大后就是一个个像素方块。...

\tutorial_code\core\how_to_scan_images

我们将探索以下问题的答案: 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 没错啦! L...

How to capture video frames from the camera as images using AV Foundation

Q:  How do I capture video frames from the camera as images using AV Foundation? A: How do I capt...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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