Anda di halaman 1dari 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
SqlConnection cn = new SqlConnection("server=bikashlama;uid=sa;pwd=nccs;
database=contactdb");
SqlCommand sqc;
SqlDataAdapter sda;
FileStream fs;
byte[] rawdata;
DataSet ds = new DataSet();
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cn.Open();
sda = new SqlDataAdapter("select photo from images", cn);
sda.Fill(ds);
cn.Close();
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds.Tab
les[0].Rows[0].ItemArray[0]));
}
private void btnInsert_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
fs = new FileStream(ofd.FileName, FileMode.Open,FileAccess.Read);
rawdata = new byte[Convert.ToInt32(fs.Length)];
fs.Read(rawdata, 0, Convert.ToInt32(fs.Length));
cn.Open();
sqc = new SqlCommand("insert into images(photo) values (@image)", cn
);
sqc.Parameters.AddWithValue("@image", rawdata);
sqc.ExecuteNonQuery();
cn.Close();
pictureBox1.Image=Image.FromStream(new MemoryStream((byte[])rawdata)
);
}
private void btnNext_Click(object sender, EventArgs e)
{
cn.Open();
sda = new SqlDataAdapter("select photo from images", cn);
sda.Fill(ds);
cn.Close();
int k = ds.Tables[0].Rows.Count;
i++;
if (i < k)
{
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds
.Tables[0].Rows[i].ItemArray[0]));
}
else
{
i = 0;
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds
.Tables[0].Rows[i].ItemArray[0]));
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
cn.Open();
sda = new SqlDataAdapter("select photo from images", cn);
sda.Fill(ds);
cn.Close();
int k = ds.Tables[0].Rows.Count;
i--;
if (i >=0)
{
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds
.Tables[0].Rows[i].ItemArray[0]));
}
else
{
i = k;
pictureBox1.Image = Image.FromStream(new MemoryStream((byte[])ds
.Tables[0].Rows[i].ItemArray[0]));
}
}
}
}

Anda mungkin juga menyukai