C# ADO.NET - Create, Select, Rename and Delete Database

In this tutorial you will learn:

1. How to create, select, rename and delete sql database using ado.net c#

Most of the times you need to create SQL Database programmatically. With C# ADO.Net you can easily do it.

Create a Database using ADO.NET C#

Here, I am going to create ComputerShop database programmatically. You can use this code in windows application or web application or even console application.

Programming Example

  1. using System;
  2. using System.Data.SqlClient;
  3.  
  4. namespace CreateDatabase
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
  11. string query = "Create Database ComputerShop";
  12. SqlCommand cmd = new SqlCommand(query, con);
  13. try
  14. {
  15. con.Open();
  16. cmd.ExecuteNonQuery();
  17. Console.WriteLine("Database Created Successfully");
  18. }
  19. catch(SqlException e)
  20. {
  21. Console.WriteLine("Error Generated. Details: " + e.ToString());
  22. }
  23. finally
  24. {
  25. con.Close();
  26. Console.ReadKey();
  27. }
  28. }
  29. }
  30. }
ComputerShop Database

Rename a Database using ADO.NET C#

 

You can rename a database using ado.net c# like this.

  1. using System;
  2. using System.Data.SqlClient;
  3.  
  4. namespace RenameDatabase
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
  11. string query = "ALTER DATABASE ComputerShop MODIFY NAME = MobileShop";
  12. SqlCommand cmd = new SqlCommand(query, con);
  13. try
  14. {
  15. con.Open();
  16. cmd.ExecuteNonQuery();
  17. Console.WriteLine("Database Renamed Successfully");
  18. }
  19. catch(SqlException e)
  20. {
  21. Console.WriteLine("Error Generated. Details: " + e.ToString());
  22. }
  23. finally
  24. {
  25. con.Close();
  26. Console.ReadKey();
  27. }
  28. }
  29. }
  30. }
MobileShop Database

Select a Database using ADO.NET C#

You can select SQL Database in ado.net by passing database name in the connection string.

  1. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=MobileShop;Integrated Security=True");

Drop or Delete a Database using ADO.NET C#

In order to delete a database using ado.net run the following block of code.

  1. using System;
  2. using System.Data.SqlClient;
  3.  
  4. namespace DeleteDatabase
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
  11. string query = "DROP DATABASE ComputerShop";
  12. SqlCommand cmd = new SqlCommand(query, con);
  13. try
  14. {
  15. con.Open();
  16. cmd.ExecuteNonQuery();
  17. Console.WriteLine("Database Deleted Successfully");
  18. }
  19. catch(SqlException e)
  20. {
  21. Console.WriteLine("Error Generated. Details: " + e.ToString());
  22. }
  23. finally
  24. {
  25. con.Close();
  26. Console.ReadKey();
  27. }
  28. }
  29. }
  30. }
Deleted Database

Summary

In this chapter, you learned how to create, select, rename and delete SQL database using ADO.NET C#. I have given the complete programming example and you can use this code in your web application as well as a windows application. In the next chapter, you will learn to Create, Update, Rename and Delete SQL Table using ADO.NET C#.

 

Share your thought