Training 4 - Simple CRUD On C# Programming

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

SIMPLE CRUD C#

Part 1
Environment C#?

• MSSQL SERVER
• Microsoft Visual Studio
• Microsoft Sql Server Management Studio
MSSMS C#?

• Buka Microsoft Sql Server Management


Studio
• Akan muncul halaman login
• Lalu Klik Connect
MSSMS C#?

• Click kanan pada server


• Pilih properties
MSSMS C#?

• Pilih Tab Security


• Pilih SQL Server and Windows
Authentication Mode
MSSMS C#?

• Restart
MSSMS C#?

• Membuat user untuk aplikasi


• Buka Section Security
• Lalu klik kanan pilih new login
MSSMS C#?

• Click kanan pada section Databases


• Pilih New Database
MSSMS C#?

• Masukan login name


• Masukan password
MSSMS C#?

• Masuk ke tab status


• Grant dan enable login
• Setelah itu klik OK
MSSMS C#?

• Masukan database name


• Pilih owner
MSSMS C#?

• Pilih user yang akan di grant


MSSMS C#?

• Jika sudah berhasil maka akan


tergenerate database baru
MSSMS C#?

• Click kanan pada section tables untuk


membuat table baru
MSSMS C#?

• Buat column baru sesuai kebutuhan


MSSMS C#?

• Setelah itu save dan beri nama


MSSMS C#?

• Table baru akan tergenerate


Simple CRUD C#?

• Pilih Create New Project


Simple CRUD C#?

• Pilih Windows Form


Simple CRUD C#?

• Input nama project


Simple CRUD C#?
Simple CRUD C#?
Simple CRUD C#?

1 Label
1 2 3
2 TextBox

3 Button
4
4 DataGrid View
Simple CRUD C#?

• Click kanan pada form


• Pillih View Code
Simple CRUD C#?

class Employee
{
public Employee(int intId, string txtName)
{
this.intId = intId;
this.txtName = txtName;
}
public int intId { get; set; } = 0;
public string txtName { get; set; } = string.Empty;
}

• Tambahkan class berikut


Simple CRUD C#?

SqlConnection sqlConnection = new SqlConnection(AppTraining.Properties.Resources.connection);


SqlCommand cmd = new SqlCommand();
Employee employee = null;
Refresh C#?

private void refresh() {


loadData();
txtID.Clear();
txtBoxName.Clear();
employee = null;
}
Load Data C#?
private void loadData()
{
try
{
if(sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
SqlDataAdapter sdp = new SqlDataAdapter("select * from mEmployee", sqlConnection);
DataSet ds = new DataSet();
sdp.Fill(ds);
tbl1.DataSource = ds.Tables[0];
tbl1.Refresh();
}catch(Exception e)
{

throw e;
}
finally
{
if(sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
}
}
Fill Data C#?

private void fillSelectedData(Employee employee)


{
if(employee != null)
{
txtID.Text = employee.intId.ToString();
txtBoxName.Text = employee.txtName.ToString();
}
else
{
txtID.Clear();
txtBoxName.Clear();
}
}
Save Data C#?
private void simpan(string name)
{
try
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
SqlParameter parameterName = new SqlParameter("name", SqlDbType.VarChar);
parameterName.Value = name;
string sql = @"insert into mEmployee(txtName) values( @name )";
cmd.CommandText = sql;
cmd.Parameters.Add(parameterName);
cmd.Connection = sqlConnection;
int row = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
cmd.Parameters.Clear();
if (sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
refresh();
}
}
private void update(int id, string name)
Update Data C#? {
try
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
SqlParameter parameterName = new SqlParameter("name", SqlDbType.VarChar);
parameterName.Value = name;
SqlParameter parameterId = new SqlParameter("id", SqlDbType.Int);
parameterId.Value = id;
string sql = @"update mEmployee set txtName=@name where intId = @id";
cmd.CommandText = sql;
cmd.Parameters.Add(parameterName);
cmd.Parameters.Add(parameterId);
cmd.Connection = sqlConnection;
int row = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
cmd.Parameters.Clear();
if(sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
refresh();
}
}
Load Data C#?

Tambahkan Event saat form di load


Click Event C#?

Double click setiap button buat


menambahkan even click
Event Button C#?

if (employee == null)
{
simpan(txtBoxName.Text.ToString());
}
else
{
update(employee.intId, txtBoxName.Text.ToString());
}

refresh();

delete();
CRUD C#?
Event Button C#?

You might also like