Data insert, update, delete and view data from database using C# :

Here I am going to discuss a full concept about how to insert , update, delete and view data from database after connecting database SQL server 2008 and visual studio 2008.
 
Consider this window,
 

Now take a class named StudentMark :

class StudentMark
{
  private string studentRoll;
  private string bangla;
  private string english;
  private string math;
  
  public string StudentRoll
  {
    get { return studentRoll; }
    set { studentRoll = value; }
  }
  public string Bangla
  {
    get { return bangla; }
    set { bangla = value; }
  }
  public string English
  {
   get { return english; }
   set { english = value; }
  }
  public string Math
  {
   get { return math; }
   set { math = value; }
  }
}
                                 INSERT DATA

 

Now double click on SAVE BUTTON :
 
private void saveButton_Click(object sender, EventArgs e)
{
 StudentMark studentData = new StudentMark();
  
 studentData.StudentRoll = studentRollTextBox.Text;
 studentData.Bangla = banglaTextBox.Text;
 studentData.English = englishTextBox.Text;
 studentData.Math = mathTextBox.Text;
 
 MessageBox.Show(“Information has been saved.”);
 
 Gateway gateway = new Gateway();
 gateway.Save(studentData);
}
Now create a class named Gateway.cs  :
class Gateway
{
 public bool Save(StudentMark student)
 {
 string connectionString = @”Here write our own Data Source”;
 SqlConnection connection = new SqlConnection(connectionString);
 connection.Open();
 string query = @”insert into t_Mark values (‘” + student.StudentRoll + “‘,'” + student.Bangla + “‘,'” +     student.English + “‘,'” + student.Math + “‘)”;
 SqlCommand command = new SqlCommand(query, connection);
 command.ExecuteNonQuery();
 return true;
 }
}

 

Now we can save data normally by running this code just like this,
  Now if we see our database table and refresh our table then we can see this,

                                UPDATE DATA

Now double click on UPDATE BUTTON :

private void UpdateButtonClick(object sender, EventArgs e)
{
StudentMark studentData = new StudentMark();

studentData.StudentRoll = studentRollTextBox.Text;
studentData.Bangla=banglaTextBox.Text;
studentData.English=englishTextBox.Text;
studentData.Math=mathTextBox.Text;

MessageBox.Show(“Information has been updated”);

 Gateway gateway = new Gateway();
gateway.Update(studentData);

 

Now in Gateway.cs class write down this code :
 
public bool Update(StudentMark student)
{
string connectionString = @”Here write our own DATA SOURCE“;

 SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

string query = @”update t_Mark set bangla='” + student.Bangla + “‘, English='” + student.English + “‘, Math = ‘” + student.Math + “‘ where Student_Roll = “+student.StudentRoll+””;

 SqlCommand command = new SqlCommand(query, connection);
command.ExecuteNonQuery();
return true;
 
Now we can update our data which we save before under roll 001 just like this,
  
 Now see our database table and refresh it then we will see,
 
 
 
                                 DELETE DATA 

Now I am going to delete that data which we save in our data table t_Mark.
Now double click on DELETE BUTTON :

private void deleteButton_Click(object sender, EventArgs e)
{
StudentMark studentData = new StudentMark();

studentData.StudentRoll = studentRollTextBox.Text;

MessageBox.Show(“Information has been deleted”);

 Gateway gateway = new Gateway();
gateway.Delete(studentData);

}

Now in Gateway.cs class add this code,

public bool Delete(StudentMark student)
{
string connectionString = @”Here write our own DATA SOURCE”;

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

string query = @”Delete from t_Mark where Student_Roll = “+student.StudentRoll+””;
SqlCommand command = new SqlCommand(query, connection);

command.ExecuteNonQuery();
return true;
}

Now we can delete data from our database. Give an input in studentRoll Text Box and click delete button like this,
 
 

If we now see our database table and refresh it then we can see all the information under roll 001 become NULL.

Now we can delete data.

                                   VIEW DATA

Now I am going to work with VIEW BUTTON. Now add view button on this window box just like this,

 

 

 

Now double click on VIEW BUTTON :

private void viewButton_Click(object sender, EventArgs e)
{
connectionString connectionString = @”
Here write our own DATA SOURCE“;
SqlConnection connection = new SqlConnection(connectionString);

 string cmdString = ” Select * from t_Mark where Student_Roll = “+student.StudentRoll+” “;
SqlCommand command = new SqlCommand(cmdString, connection);

try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
banglaTextBox.Text = reader[“Bangla”].ToString();
englishTextBox.Text = reader[“English”].ToString();
mathTextBox.Text = reader[“Math”].ToString();
}
}

catch (Exception exp)
{
throw exp;
}
  finally
{
connection.Close();
}

}

Now if we run our program and give a student roll number into studentRollTextBox and then click view button then we can see our result in text boxes.

So,we can now insert any data in our database table and also can update, delete and can view our data from database table into our window box.

Leave a Reply