....................Database................
create database demo
CREATE TABLE [dbo].[info] (
[Id] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (50) NOT NULL,
[Address] [varchar] (150) NOT NULL,
[Phone] [varchar] (15) NOT NULL,
[OriginalPath] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Photo] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
select * from info
........................................................................................................
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace Demo_Save_Update_Delete_Search_Photo_Store
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data Source=RANJEETKUMAR-PC\\SQLEXPRESS; Initial Catalog=demo; User Id=sa; Password=ranjeet");
private void Form1_Load(object sender, EventArgs e)
{
show_detail();
}
public void clear()
{
textId.Text = "";
textName.Text = "";
textAddress.Text = "";
textPhone.Text = "";
textPhoto_Path.Text = "";
}
public void show_detail()
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from info", con);
DataSet ds = new DataSet();
da.Fill(ds, "info");
dataGridView1.DataSource = ds.Tables["info"];
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
clear();
}
private void SAVE_Click(object sender, EventArgs e)
{
byte[] Photo = ReadFile(textPhoto_Path.Text);
try
{
string str = "insert into info (Name,Address,Phone,OriginalPath,Photo) values(@Name,@Address,@Phone,@OriginalPath,@Photo)";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add(new SqlParameter("@Name", (object)textName.Text));
cmd.Parameters.Add(new SqlParameter("@Address", (object)textAddress.Text));
cmd.Parameters.Add(new SqlParameter("@Phone", (object)textPhone.Text));
cmd.Parameters.Add(new SqlParameter("@OriginalPath", (object)textPhoto_Path.Text));
cmd.Parameters.Add(new SqlParameter("@Photo", (object)Photo));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Saved Successfully");
show_detail();
}
catch
{
}
}
private void UPDATE_Click(object sender, EventArgs e)
{
byte[] Photo = ReadFile(textPhoto_Path.Text);
try
{
string str = "update info set Name=@Name,Address=@Address,Phone=@Phone,OriginalPath=@OriginalPath,Photo=@Photo where Id='" + textId.Text + "'";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.Add(new SqlParameter("@Name", (object)textName.Text));
cmd.Parameters.Add(new SqlParameter("@Address", (object)textAddress.Text));
cmd.Parameters.Add(new SqlParameter("@Phone", (object)textPhone.Text));
cmd.Parameters.Add(new SqlParameter("@OriginalPath", (object)textPhoto_Path.Text));
cmd.Parameters.Add(new SqlParameter("@Photo", (object)Photo));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated Successfully");
show_detail();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog fl = new OpenFileDialog();
DialogResult dl = fl.ShowDialog();
if (dl != DialogResult.Cancel)
{
pictureBox1.ImageLocation = fl.FileName;
textPhoto_Path.Text = fl.FileName;
}
}
byte[] ReadFile(String spath)
{
byte[] data = null;
FileInfo finfo = new FileInfo(spath);
long numbyts = finfo.Length;
FileStream fs = new FileStream(spath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
data = br.ReadBytes((int)numbyts);
return data;
}
private void DELETE_Click(object sender, EventArgs e)
{
try
{
if (textDelete.Text == "")
{
MessageBox.Show("Please enter a valid ID.");
}
else
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from info where Id='" + textDelete.Text + "'", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Deleted successfully");
show_detail();
textDelete.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
string row;
string row1;
string row2;
string row3;
string row4;
row = dataGridView1.CurrentRow.Cells["Name"].Value.ToString();
textName.Text = row;
row1 = dataGridView1.CurrentRow.Cells["Id"].Value.ToString();
textId.Text = row1;
row2 = dataGridView1.CurrentRow.Cells["Address"].Value.ToString();
textAddress.Text = row2;
row3 = dataGridView1.CurrentRow.Cells["Phone"].Value.ToString();
textPhone.Text = row3;
row4 = dataGridView1.CurrentRow.Cells["OriginalPath"].Value.ToString();
textPhoto_Path.Text = row4;
try
{
byte[] Photo = (byte[])dataGridView1.Rows[e.RowIndex].Cells["Photo"].Value;
Image newImage;
using (MemoryStream ms = new MemoryStream(Photo, 0, Photo.Length))
{
ms.Write(Photo, 0, Photo.Length);
newImage = Image.FromStream(ms, true);
}
pictureBox1.Image = newImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textSearch_TextChanged(object sender, EventArgs e)
{
if (textSearch.Text == "")
{
show_detail();
}
else
{
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from info where Id LIKE '" + textSearch.Text + "%'", con);
DataSet ds = new DataSet();
da.Fill(ds, "info");
dataGridView1.DataSource = ds.Tables["info"];
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
No comments:
Post a Comment