关闭

How to save or get Images through Database.

标签: datasetdatabasebytesql serverexchangetable
595人阅读 评论(0) 收藏 举报

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

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:329661次
    • 积分:5816
    • 等级:
    • 排名:第4372名
    • 原创:248篇
    • 转载:19篇
    • 译文:0篇
    • 评论:38条
    最新评论