SQL Server Connection
SQL Server Connection
It will prompt for database connection. Provide the server name and
authentication.
2. Creating Database
Now, create database by selecting database option then right click
on it. It pops up an option menu and provides couple of options.
Click on the New Database then it will ask for the database name.
Here, we have created a Student database.
Click on the Ok button then it will create a database that we can see
in the left window of the below screenshot.
3. Establish connection and create a table
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database
=student; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("create table
student(id int not null,
20. name varchar(100), email varchar(50), join_date dat
e)", con);
21. // Opening Connection
22. con.Open();
23. // Executing the SQL query
24. cm.ExecuteNonQuery();
25. // Displaying a message
26. Console.WriteLine("Table created Successfully");
27. }
28. catch (Exception e)
29. {
30. Console.WriteLine("OOPs, something went wrong."+
e);
31. }
32. // Closing the connection
33. finally
34. {
35. con.Close();
36. }
37. }
38. }
39. }
Execute this code using Ctrl+F5. After executing, it displays a
message to the console as below.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database
=student; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("insert into st
udent
20. (id, name, email, join_date)values('101','Ronald Tru
mp','ronald@example.com','1/12/2017')", con);
21. // Opening Connection
22. con.Open();
23. // Executing the SQL query
24. cm.ExecuteNonQuery();
25. // Displaying a message
26. Console.WriteLine("Record Inserted Successfully");
27. }
28. catch (Exception e)
29. {
30. Console.WriteLine("OOPs, something went wrong."+
e);
31. }
32. // Closing the connection
33. finally
34. {
35. con.Close();
36. }
37. }
38. }
39. }
Execute this code by using Ctrl+F5 and it will display the following
output.
5. Retrieve Record
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database
=student; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("Select * fro
m student", con);
20. // Opening Connection
21. con.Open();
22. // Executing the SQL query
23. SqlDataReader sdr = cm.ExecuteReader();
24. // Iterating Data
25. while (sdr.Read())
26. {
27. Console.WriteLine(sdr["id"] + " " + sdr["name"]+"
"+sdr["email"]); // Displaying Record
28. }
29. }
30. catch (Exception e)
31. {
32. Console.WriteLine("OOPs, something went wrong.\
n"+e);
33. }
34. // Closing the connection
35. finally
36. {
37. con.Close();
38. }
39. }
40. }
41. }
Output:
6. Deleting Record
This time student table contains two records. The following C# code
delete one row from the table.
// Program.cs
1. using System;
2. using System.Data.SqlClient;
3. namespace AdoNetConsoleApplication
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. new Program().CreateTable();
10. }
11. public void CreateTable()
12. {
13. SqlConnection con = null;
14. try
15. {
16. // Creating Connection
17. con = new SqlConnection("data source=.; database=stud
ent; integrated security=SSPI");
18. // writing sql query
19. SqlCommand cm = new SqlCommand("delete from stude
nt where id = '101'", con);
20. // Opening Connection
21. con.Open();
22. // Executing the SQL query
23. cm.ExecuteNonQuery();
24. Console.WriteLine("Record Deleted Successfully");
25. }
26. catch (Exception e)
27. {
28. Console.WriteLine("OOPs, something went wrong.\n"+e);
29. }
30. // Closing the connection
31. finally
32. {
33. con.Close();
34. }
35. }
36. }
37. }
Output:
Keep Watching
Competitive questions on Structures in Hindi
00:00/03:34
Keep Watching
Competitive questions on Structures in Hindi
00:00/03:34