Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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(); } } }
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 = ""; } } }
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);
} }
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(); }
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(); }
public void delete(int no) { string sql = "DELETE FROM Personal WHERE empNo=" + no; SqlCommand com = new SqlCommand(sql, ConnectionManager.connection()); com.ExecuteNonQuery(); }
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(); }
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; }
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 = ""; }