Sei sulla pagina 1di 6

C# .

Net Database Connective Steps & Coding

Sept 01

Sept 02 Right click on Project Name Select Add New Item Select Server-based Database Type a Name for database and Click on Add Click on Finish (Then display Data Source Configuration Wizard, Select Tables [or select all of above], Click Next) Select View Menu Select Other Windows, Select Database Explorer Click on + mark in Database Right click on the Table

Select Add New Table Design Table Heading Click on Save, Type Table Name [Personal]

Step 03 Right click on Project Name Select Add New Item Select Class Type a name for Class [ConnectionManager] and Click on Add

using System.Data.SqlClient; namespace Employee_Information { class ConnectionManager { public static SqlConnection connection() { string connectionString=@

Right click on Database Select Properties Select connectionString, copy values in Property value, paste it in to connectionString statement

using System.Data.SqlClient; namespace Employee_Information { class ConnectionManager { public static SqlConnection connection() { string connectionString=@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\C# .Net Database Connection Project New\Employee Information\Employee Information\Employee.mdf;Integrated Security=True;User Instance=True"; SqlConnection con = new SqlConnection(connectionString); con.Open(); return con; } } }

Sept 04 Right click on Project Name Select Add New Item Select Class Type a name for Class [StudentData] and Click on Add

using System.Data.SqlClient; namespace Employee_Information { class StudentData { public void insert(int no, string name, string add) { string sql = "INSERT INTO Personal VALUES (" + no + ",'" + name + "','" + add + "')"; SqlCommand com = new SqlCommand(sql, ConnectionManager.connection()); com.ExecuteNonQuery(); } } }

Step 05 Go to GUI Interface Double click on Insert Button

namespace Employee_Information { public partial class frmEmployee : Form { StudentData sd = new StudentData(); public frmEmployee() { InitializeComponent(); } private void btnInsert_Click(object sender, EventArgs e) { try { sd.insert(int.Parse(txtEmpNo.Text), txtEmpName.Text, txtEmpAdd.Text); MessageBox.Show("Record added to Database", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);

} catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } Clear(); } private void btnClear_Click(object sender, EventArgs e) { txtEmpNo.Text = ""; txtEmpName.Text = ""; txtEmpAdd.Text = ""; dataGrid.DataSource = ""; } } }

Sept 06 Go to StudentData Class


public SqlDataReader select(int no) { string sql = "SELECT * FROM Personal WHERE empNo=" + no; SqlCommand com = new SqlCommand(sql, ConnectionManager.connection()); SqlDataReader dr = com.ExecuteReader(); return dr; }

Step 07 Go to GUI Interface Double click on Select Button

using System.Data.SqlClient; private void btnSelect_Click(object sender, EventArgs e) { SqlDataReader dr = sd.select(int.Parse(txtEmpNo.Text)); if (dr.Read()) { MessageBox.Show("Record Exist in Database", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); txtEmpName.Text = dr[1].ToString(); txtEmpAdd.Text = dr[2].ToString(); } else { MessageBox.Show("Record Does not Exist in Database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

} }

Step 08 Go to StudentData Class

public void update(int no, string name, string add) { string sql = "UPDATE Personal SET empName='" + name + "',empAdd='" + add + "' WHERE empNo=" + no; SqlCommand com = new SqlCommand(sql, ConnectionManager.connection()); com.ExecuteNonQuery(); }

Step 09 Go to GUI Interface Double click on Update Button

private void btnUpdate_Click(object sender, EventArgs e) { sd.update(int.Parse(txtEmpNo.Text), txtEmpName.Text, txtEmpAdd.Text); MessageBox.Show("Recorded updated to Database", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); Clear(); }

Step 10 Go to StudentData Class

public void delete(int no) { string sql = "DELETE FROM Personal WHERE empNo=" + no; SqlCommand com = new SqlCommand(sql, ConnectionManager.connection()); com.ExecuteNonQuery(); }

Step 11 Go to GUI Interface Double click on Delete Button

private void btnDelete_Click(object sender, EventArgs e) { sd.delete(int.Parse(txtEmpNo.Text)); MessageBox.Show("Recorded Deleted from Database", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information); Clear(); }

Step 12 Go to StudentData Class

using System.Data; public DataSet all() { DataSet ds = new DataSet(); string sql = "SELECT * FROM Personal"; SqlDataAdapter da = new SqlDataAdapter(sql, ConnectionManager.connection()); da.Fill(ds, "Personal"); return ds; }

Step 13 Go to GUI Interface Double click on Display Button

private void btnDisplay_Click(object sender, EventArgs e) { DataSet ds = sd.all(); dataGrid.DataSource = ds.Tables["Personal"]; } private void Clear() { txtEmpNo.Text = ""; txtEmpName.Text = ""; txtEmpAdd.Text = ""; }

Potrebbero piacerti anche