Sei sulla pagina 1di 5

11/10/2016

Creating CRUD windows form vs 2015 C# sample in C# for Visual Studio 2015

Developer resources

Windows Dev Center

Explore

Search Dev Center

Docs

Downloads

Samples

Support

Dev Center - Desktop > Samples > Creating CRUD windows form vs 2015 C#

Get SDK and tools

Creating CRUD windows form vs 2015 C#

this is a CRUD form c# with entity framwork and linq querythis needes vistual studio 2015 Enterprisesql server 2012 or
anyDescriptionscreenshotthe following screenshot show CRUD / insertion, update, delete with images into the sql server databasethe da
file in inde ziped f

Quick access
My samples
Upload a sample

Download

C# (4.7 MB)

Browse sample requests

170
Points
Top 10%

Hassan-kafi A. Mohamed

Ratings

(0)

Updated

12/15/2015

Downloaded

1,228 times

License

MIT

Favorites

Add to favorites

Share

Requires

Visual Studio 2015

Technologies

LINQ to SQL, Entity Framework, C# programming, CRUD with images into the sqlserver c#

Topics

CRUD windows applciation C#

Joined Dec 2015


View samples
1

Report abuse to Microsoft

Show activity

Description

Browse code

Q and A

Introduction
this is a CRUD form c# with entity framwork and linq query

Building the Sample


this needes vistual studio 2015 Enterprise
sql server 2012 or any

Description
screenshot
the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.
so you can download copy pate into new query window of the sql server then excute.

the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.

so you can download copy pate into new query window of the sql server then excute.
the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.
so you can download copy pate into new query window of the sql server then excute.

the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.
so you can download copy pate into new query window of the sql server then excute.

https://code.msdn.microsoft.com/windowsdesktop/Creating-CRUD-application-1b0028e5

1/5

11/10/2016

Creating CRUD windows form vs 2015 C# sample in C# for Visual Studio 2015
the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.
so you can download copy pate into new query window of the sql server then excute.
the following screenshot show CRUD / insertion, update, delete with images into the sql server database
the database file in inde ziped file.
so you can download copy pate into new query window of the sql server then excute.

YOU CAN ALSO COPPY THE SQL SCRIPT FROM HERE

C#
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.IO;

namespace CRUD_app
{
public partial class Form1 : Form
{
employeeDataContext ed;
string imagename;
public Form1()
{
InitializeComponent();
ed = new employeeDataContext();
}
private void Form1_Load(object sender, EventArgs e)
{
}

private void button1_Click(object sender, EventArgs e)


{
employeetab empTable = new employeetab(); // calling the employee class in the employee.designer.cs file

var empid = from data in ed.employeetabs


where data.empid == textBox1.Text.ToString()
select data;
if (!empid.Any())
{
if (imagename != "")
{
FileStream fs;
fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

https://code.msdn.microsoft.com/windowsdesktop/Creating-CRUD-application-1b0028e5

2/5

11/10/2016

Creating CRUD windows form vs 2015 C# sample in C# for Visual Studio 2015
//a byte array to read the image
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

empTable.empid = textBox1.Text;
empTable.empname = textBox2.Text.ToString();
empTable.empdep = textBox3.Text.ToString();
empTable.empimage = picbyte;

ed.employeetabs.InsertOnSubmit(empTable);

ed.SubmitChanges();
MessageBox.Show("Record Inserted Successfully");
}
}
else MessageBox.Show("Record allready existing");
}

private void button2_Click(object sender, EventArgs e)


{
string empid = textBox1.Text.ToString();
//Here "ed" is the employeeDataContext
employeetab emp = ed.employeetabs.Single(e1 => e1.empid == empid); //To Retrieve one single record from t
database for the given empid.
if (emp != null)
{
ed.employeetabs.DeleteOnSubmit(emp);
ed.SubmitChanges();
MessageBox.Show("Record Deleted");
}
}

private void textBox1_TextChanged(object sender, EventArgs e)


{
string empid = textBox1.Text;
var empresult = from data in ed.employeetabs // Ling Query to retrive data from table
where data.empid == empid
select data;
if(empresult.Any()) {

// Data Binding
foreach (var emp in empresult)
{
MemoryStream stream = new MemoryStream();

textBox2.Text = emp.empname.ToString(); // Retriving column data and binding to the textbox


textBox3.Text = emp.empdep.ToString(); // Retriving column data and binding to the textbox
if(emp.empimage != null) {
byte[] image = (byte[])emp.empimage.ToArray();
stream.Write(image, 0, image.Length);
//Bitmap bitmap = new Bitmap(stream);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromStream(stream);
;
}
}
}
else {
textBox2.Text = ""; textBox3.Text = "";
pictureBox1.Image = null;
}
}

private void button3_Click(object sender, EventArgs e)


{
string empid = textBox1.Text;
employeetab emp = ed.employeetabs.Single(e1 => e1.empid == empid); //To Retrieve one single record from t
database for the given empid.

emp.empid = textBox1.Text; // Mapping the data to the column in the table


emp.empname = textBox2.Text.ToString(); // Mapping the data to the column in the table
emp.empdep = textBox3.Text.ToString(); // Mapping the data to the column in the table
//emp.EMP_LOCATION = txtEmpLocation.Text.ToString(); // Mapping th
ata to the column in the table
if (imagename != "")
{
FileStream fs;

fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

https://code.msdn.microsoft.com/windowsdesktop/Creating-CRUD-application-1b0028e5

3/5

11/10/2016

Creating CRUD windows form vs 2015 C# sample in C# for Visual Studio 2015
//a byte array to read the image
byte[] picbyte = new byte[fs.Length];
fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
emp.empimage = picbyte;

ed.SubmitChanges();
imagename = "";
}
MessageBox.Show("Record Updated");
}
private void button4_Click(object sender, EventArgs e)
{
try
{
FileDialog fldlg = new OpenFileDialog();
//specify your own initial directory
fldlg.InitialDirectory = @"C:\";
//this will allow only those file extensions to be added
fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldlg.ShowDialog() == DialogResult.OK)
{
imagename = fldlg.FileName;
//Bitmap newimg = new Bitmap(imagename);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(imagename);
}
fldlg = null;
}
catch (System.ArgumentException ae)
{
imagename = " ";
MessageBox.Show(ae.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
}
private void cmdClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
}

More Information
For more information on this , contact and ask to me Hassan-kafi A. Mohamed

https://code.msdn.microsoft.com/windowsdesktop/Creating-CRUD-application-1b0028e5

4/5

11/10/2016

Creating CRUD windows form vs 2015 C# sample in C# for Visual Studio 2015

Downloads and tools

Essentials

Learning resources

Programs

Windows 10 dev tools

API reference (Windows apps)

Microsoft Virtual Academy

Get a dev account

Visual Studio

API reference (desktop apps)

Channel 9

App Developer Agreement

Windows SDK

Code samples

Video gallery

Windows Insider Program

Windows Store badges

How-to guides (Windows apps)

Windows 10 by 10 blog series

Microsoft Affliate Program

English

https://code.msdn.microsoft.com/windowsdesktop/Creating-CRUD-application-1b0028e5

Privacy and cookies

Terms of use

Trademarks

5/5

Potrebbero piacerti anche