Anda di halaman 1dari 4

Modul C#

Database SQL, Connection,manipulasi data dan Report


1. Rancanglah Database pada Sql Server dengan field seperti dibawah ini.

2. Rancanglah tampilan interface pada lingkungan windows C# .Net seperti dibawah ini
sesuai dengan database yang anda buat




Listing Program
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.Data.SqlClient;


namespace lat_c_shap
{
public partial class Form1 : Form
{
public SqlConnection con1;
public SqlCommand com1, com2;

public SqlDataAdapter adp1;
public DataSet ds1;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'ds1.tbmhs' table.
You can move, or remove it, as needed.
this.tbmhsTableAdapter1.Fill(this.ds1.tbmhs);

string cstr;
cstr = "Data Source=ZEBAOTH-779C970;Initial
Catalog=pop;Integrated Security=True";
con1 = new SqlConnection(cste);
con1.Open();
com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
ds1 = new DataSet();
adp1 = new SqlDataAdapter(com1);
adp1.Fill(ds1, "tbmhs");
dg1.DataSource = ds1;
dg1.DataMember = "tbmhs";
con1.Close();

}

private void BtnClear_Click(object sender, EventArgs e)
{
this.txtnim.Clear();
this.txtnama.Clear();
this.txtjurusan.Clear();
this.txtsemester.Clear();
}

private void BtnDelete_Click(object sender, EventArgs e)
{

con1.Open();
com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "delete from tbmhs where nim='" +
this.txtnim.Text + "'";
com1.ExecuteNonQuery();
com2 = new Sqlcommand();
com2.Connection = con1;
com2.CommandType = CommandType.Text;
com2.CommandText = "Select * from tbmhs";
ds1 = new DataSet();
adp1 = new SqlDataAdapter(com2);
adp1.Fill(ds1, "tbmhs");
dg1.DataSource = ds1;
dg1.DataMember = "tbmhs";
con1.close();
this.dg1.Refresh();

}

private void BtnAdd_Click(object sender, EventArgs e)
{
con1.Open();
com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "insert into tbmhs values ('" + txtnim.Text +
"','" + txtjurusan.text + "','" + txtsemester.text + ")";

com1.ExecuteNonQuery();
com2 = new Sqlcommand();
com2.Connection = con1;
com2.CommandType = CommandType.Text;
com2.CommandText = "Select * from tbmhs";
ds1 = new DataSet();
adp1 = new SqlDataAdapter(com2);
adp1.Fill(ds1, "tbmhs");
dg1.DataSource = ds1;
dg1.DataMember = "tbmhs";
con1.Close();
this.dg1.Refresh();

}

private void btnUpdate_Click(object sender, EventArgs e)
{
con1.Open();
com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "update tbmhs set nama='" + txtnama.text +
"',jurusan= " + txtjurusan.text + ",semester=" + txtsemester.text + " where
nim ='" + txtnim.text + "'";

com1.ExecuteNonQuery();
com2 = new Sqlcommand();
com2.Connection = con1;
com2.CommandType = CommandType.Text;
com2.CommandText = "Select * from tbmhs";
ds1 = new DataSet();
adp1 = new SqlDataAdapter(com2);
adp1.Fill(ds1, "tbmhs");
dg1.DataSource = ds1;
dg1.DataMember = "tbmhs";
con1.Close();
this.dg1.Refresh();
}
private void BtnFind_Click(object sender, EventArgs e)
{
con1.Open();
com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "select * from tbmhs";
ds1 =new DataSet();

//com1.ExecuteNonQuery();
//com2 = new Sqlcommand();
//com2.Connection = con1;
//com2.CommandType = CommandType.Text;
//com2.CommandText = "Select * from tbmhs";
ds1 = new DataSet();
adp1 = new SqlDataAdapter(com1);
adp1.Fill(ds1, "tbmhs");
DataTable tbmhs = ds1.Tabel["pop"];
DataColumn[] col1= new DataColumn[1];
col1[0]=tbmhs.Columns["nim"];
tbmhs.PrimaryKey=col1;
DataRow rw1=tbmhs.Rows.Find(txtnim.Text);
txtnim.Text=(string)rw1 ["nim"];
txtnama.Text=(string)rw1 ["nama"];
int A =(int)rw1 ["jurusan"];
txtjurusan.Text =A.ToString();
int B =(int)rw1 ["semester"];
txtsemester.Text =B.ToString();

//report variabel = new report();
//variabel.MdiParent = form1;
//variabel.Show();
}

}
}