(localdb)\MSSQLLocalDB
//Create Table
public void CreateTable()
SqlConnection con = null;
try
// Creating Connection
con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Student;Integrated Security=True;Encrypt=False");
// writing sql query
SqlCommand cm = new SqlCommand("create table student(id int not null,name
varchar(100),email varchar(50), join_date date)", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Table created Successfully");
Console.Read();
catch (Exception e)
Console.WriteLine("OOPs, something went wrong." + e);
// Closing the connection
finally
{
con.Close();
//Insert Data in Table
public void InsertTable()
SqlConnection con = null;
try
// Creating Connection
con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Student;Integrated Security=True;Encrypt=False");
// writing sql query
SqlCommand cm = new SqlCommand("insert into student(id, name, email,
join_date)values('101','Priyansh','
[email protected]', '1/12/2017')", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Record Inserted Successfully");
catch (Exception e)
Console.WriteLine("OOPs, something went wrong." + e);
// Closing the connection
finally
{
con.Close();
// Read Data from table
public void readTable()
SqlConnection con = null;
try
// Creating Connection
con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Student;Integrated Security=True;Encrypt=False");
// writing sql query
SqlCommand cm = new SqlCommand("Select * from student", con);
// Opening Connection
con.Open();
// Executing the SQL query
SqlDataReader sdr = cm.ExecuteReader();
// Iterating Data
while (sdr.Read())
Console.WriteLine(sdr["id"] + " " + sdr["name"] + " " + sdr["email"]); // Displaying Record
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
// Closing the connection
finally
con.Close();
//delete Data From the table
public void DeleteRecord()
SqlConnection con = null;
try
// Creating Connection
con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=Student;Integrated Security=True;Encrypt=False");
// writing sql query
SqlCommand cm = new SqlCommand("Delete from student where id = '101' ", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
Console.WriteLine("Record Deleted Successfully");
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
// Closing the connection
finally
con.Close();