Sunday, 26 June 2016

Connect Microsoft Azure SQL Database Using C#


Firstly, create a Database and Table in the Microsoft Azure SQL Database. Here's the screenshot:
SQL Database
After creating the DB connect the Database using SQL Server.

Go to Server Explorer Object Explorer and Add connection.


Get the server name and login credential from https://manage.windowsazure.com. And login to the server using the SQL Server

login to the server

After login to the server it shows the database in the Server Explorer Object Explorer Window like the following screenshot:

database

Now create the table using New Query. (I have created a table with two columns namely Name and Age. We can create any number of columns like the normal DB)

New Query

After creating the table, the explorer will look like the following:

demo database

Now its time for us to write the code. Create a form with two text box, two label and a command button.

Enter the following code inside the command button.
  1. String name = textBox1.Text.ToString();  
  2. String age = textBox2.Text.ToString();  
  3. SqlConnection con = new SqlConnection("Your connection String with password");  
  4. con.Open();  
  5.   
  6.   
  7. String query = "INSERT INTO dbo.demo (Name, Age) VALUES ('" + name.ToString() + "','" + age.ToString() + "' );";  
  8. SqlCommand cmd = new SqlCommand(query, con);  
  9. cmd.ExecuteNonQuery();  
  10. MessageBox.Show("Sucessfully inseted");  
Compile and execute the code.

execute the code

After insertion of the data check with the db.

data check

We can check the Dashboard.

Dashboard

Have fun.

No comments:

Post a Comment