Step 1:
Open Visual Studio->New project->Window form Application
Create a table in database for storing images ,Set increment property for IdCreate Store procedure for insert values in to database and name it as "testinsert"
Create Store procedure for Retrieving values from database and name it as "testshow"
Step 2:
For
3-tier Architecture first open visual studio add new project->class library for DataAccess
layer name as Dal in that take one class as Dalcls similarly add new
project to the solution for Bussiness layer name as Bal in that take one class as
Balcls.
Step 3:
In this article am taking one sqlhelper class for database connection .Refer sqlhelper class by Click here
Design the form as below:
The Form1[Designer].cs looks like this
And
write the following code in Dal class:
Dalcls.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
namespace DAL
{
public class dalcls
{
SqlHelper sh = new
SqlHelper();
public int
insertimage(string name ,byte[] photo)
{
Hashtable ht = new
Hashtable();
ht.Add("@name", name);
ht.Add("@image", photo);
int result = sh.ExecuteQuery("testinsert",
ht);
return result;
}
public DataSet
showimage(string name)
{
Hashtable ht = new
Hashtable();
ht.Add("@name", name);
DataSet ds = sh.ExecuteProcudere("testshow", ht);
return ds;
}
}
}
Balcls.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;
namespace BAL
{
public class balcls
{
dalcls dal = new dalcls();
public int
insertimage(string name, byte[] photo)
{
return dal.insertimage(name,photo);
}
public DataSet
showimage(string name)
{
return dal.showimage(name);
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using BAL;
namespace Insertimage3tier
{
public partial class Form1 : Form
{
balcls bal = new balcls();
public
Form1()
{
InitializeComponent();
}
private void
btnSave_Click(object sender, EventArgs e)
{
MemoryStream ms1 = new
MemoryStream();
pictureBox1.Image.Save(ms1, pictureBox1.Image.RawFormat);
byte[] data = ms1.GetBuffer();
Hashtable ht = new
Hashtable();
ArrayList arry = new
ArrayList();
arry.Add(pictureBox1 );
arry.Add(txtname.Text);
int result = bal.insertimage(Convert.ToString
(txtname.Text),data);
if (result > 0)
{
MessageBox.Show("image inserted");
}
}
private void btnupload_Click(object sender, EventArgs
e)
{
openFileDialog1.Filter = "Image
Files|*.gif;*.jpg;*.png;*.bmp";
openFileDialog1.ShowDialog();
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
private void btnRetrieve_Click(object sender, EventArgs e)
{
DataSet ds = bal.showimage(txtname.Text);
int c=ds.Tables [0].Rows .Count;
if (c > 0)
{
Byte[] b = new
Byte[0];
b = (Byte[])(ds.Tables[0].Rows[c -
1]["Image"]);
MemoryStream ms1 = new MemoryStream(b,
true);
pictureBox1.Image = Image.FromStream(ms1);
}
}
}
}
Where Is sqlhelper class
ReplyDeletego to C# Examples u will find it there.
Delete