In this blog, we will see how to insert, retrieve, update, and delete records in C# windows applications. If you have basic experience with C#, SQL, and Ado.Net, then you can easily write the C# code for CRUD operations.
In order to connect the C# with the SQL database, we will have to use System.Data.SqlClient
namespace and its properties. We will see the steps to connect the database and tables using the System.Data.SqlClient
namespace.
Step 1:
- Create a new windows application in the .Net core
Step 2:
Create the database (CarsDB) and table (CarInfo - columns mentioned below) in MSSQL.
Step 3:
Add the namespace System.Data.SqlClient
and create the SQL connection shown below,
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=CarsDB;Integrated Security=true;");
SqlCommand command;
SqlDataAdapter adapter;
Step 4: Create Operation
You have to specify the insert
query with the appropriate fields and open the SQL connection and add the parameters as mentioned below,
private void btnInsert_Click(object sender, EventArgs e)
{
if (txtId.Text != string.Empty && txtBrand.Text != string.Empty
&& txtColor.Text != string.Empty && txtModel.Text != string.Empty && txtYear.Text != string.Empty)
{
command = new SqlCommand("insert into CarInfo(ID,Brand,Model,Color,MakeYear) values(@id,@brand,@model,@color,@makeyear)", connection);
connection.Open();
command.Parameters.AddWithValue("@id", txtId.Text);
command.Parameters.AddWithValue("@brand", txtBrand.Text);
command.Parameters.AddWithValue("@model", txtModel.Text);
command.Parameters.AddWithValue("@color", txtColor.Text);
command.Parameters.AddWithValue("@makeyear", txtYear.Text);
command.ExecuteNonQuery();
connection.Close();
BindData();
ClearControls();
MessageBox.Show("Car Info Added Successfully");
}
}
Step 5: Update Operation
Specify the update
query with relevant parameters as mentioned below,
private void btnUpdate_Click(object sender, EventArgs e)
{
if (txtId.Text != string.Empty && txtBrand.Text != string.Empty
&& txtColor.Text != string.Empty && txtModel.Text != string.Empty && txtYear.Text != string.Empty)
{
command = new SqlCommand("update CarInfo set Brand=@brand,Model=@model,Color=@color,MakeYear=@makeyear where ID=@id", connection);
connection.Open();
command.Parameters.AddWithValue("@id", txtId.Text);
command.Parameters.AddWithValue("@brand", txtBrand.Text);
command.Parameters.AddWithValue("@model", txtModel.Text);
command.Parameters.AddWithValue("@color", txtColor.Text);
command.Parameters.AddWithValue("@makeyear", txtYear.Text);
command.ExecuteNonQuery();
connection.Close();
BindData();
ClearControls();
MessageBox.Show("Car Info Updated Successfully");
}
}
Step 6: Delete Operation
Specify the delete
query with relevant parameters as mentioned below,
private void btnDelete_Click(object sender, EventArgs e)
{
if (txtId.Text != string.Empty)
{
command = new SqlCommand("delete CarInfo where ID=@id", connection);
connection.Open();
command.Parameters.AddWithValue("@id", txtId.Text);
command.ExecuteNonQuery();
connection.Close();
BindData();
ClearControls();
MessageBox.Show("Car Info Deleted Successfully!");
}
}
Step 7: Read/Retrieve Operation
Use the select
query to get the records from the database as shown below
private void BindData()
{
connection.Open();
DataTable dt = new DataTable();
adapter = new SqlDataAdapter("select * from CarInfo", connection);
adapter.Fill(dt);
dgCars.DataSource = dt;
connection.Close();
}
Screenshots:
Enter the required information and click the "Insert" button.
Record successfully added into the database table.
Select the row which you want to update, modify the record, and click the "Update" button to update the record into the database tables.
Select the row which you want to delete and click the "Delete" button to delete the record from the database table.
Similar article:
Comments (0)