We often need to save and access images during our development. And most people do in the following way: save the image in the disk and then save image name in the database. Yet, sometimes we could also try to directly save the image in the database. Here I’d like to share how to store the images into sqlserver in winform with the help of Daolite
as well as how to access them from sqlserver and display in picturebox. The whole process is very simple. Take Northwind as an example:
Firstly have a look at the Categories Table structure of Northwind Database.
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[CategoryName] [nvarchar](15) NOT NULL,
[Description] [ntext] NULL,
We can see the data type of Picture field is image where we will save the image.
Simply use the following codes:
private void button2_Click(object sender, EventArgs e)
//declare data access objects
CategoriesAccess CateAcc = new CategoriesAccess();
Categories cate = new Categories();
//set value to newly added caregory
cate.CategoryName = "a new category";
cate.Description = "hello world";
// set value to Picture field
cate.Picture = CateAcc.UIDataConverter.ReadFile(@"F:\myproject\im ages\iphone-logo.jpg");
//insert newly added category into database
//Completed. Images would be display in pictureBox.
this.pictureBox1.Image = CateAcc.UIDataConverter.ConvertToImage(cate.Pictur e);