In my first post on SQL Azure (SQL Azure: A First Contact) I talked about how you can access SQL Azure using SQL Server 2008 Management Studio (SSMS).
In this post, the journey continues with “walking” on the cloud using ADO .NET 🙂
Here’s the recipe!
Ingredients:
- A valid SQL Azure account
- Visual Studio 2008 SP1
- .NET Framework 3.5 SP1
- C# (or respective VB.NET) namespace: System.Data.SqlClient
Objects used from System.Data.SqlClient namespace:
- SqlConnection
- SqlCommand
- SqlDataReader
- SqlDataAdapter
Process:
- Step 1: Add namespace for using SqlClient
- Step 2: Set up connection to database
- Step 3: Open database connection
- Step 4: Set the T-SQL command(s) – Use the “SqlCommand” object
- Step 5: Open the SqlDataReader – Execute the T-SQL command(s)
- Step 6: Display the results (if any)
- Step 7: Close the SqlDataReader
- Step 8: Close the database connection
Source Code (for obvious reasons I removed the connection string from the source code file)
- You can find the sample source code for this post here.
- *Note: This sample code is for demo purposes and should not be used as production code.
Considerations
- My example is based on the example in my previous post on SQL Azure (Database Name: sqlazure, table name: CLOUD_MSGS)
- For this post I used a new Visual C# “Console Application” project named ADO .NET – SQL Azure like in the following screenshot:
Screenshot of the output:
Learn more about Azure SQL Database – Enroll to our Online Course!
Enroll to our online course on Udemy titled “Introduction to Azure SQL Database for Beginners” and get lifetime access to high-quality lessons and hands-on guides about all the aspects of Azure SQL Database.
Featured Online Courses:
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Administration Tips
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Development Tips for SQL Developers
- Working with Python on Windows and SQL Server Databases
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Read Also
- Azure Cosmos DB: Learn by Example
- Learn Azure SQL Database: Creating your First Database
- How to Create an Azure SQL Server Virtual Machine
- How to Backup a SQL Server Database from On-Premises to Azure Storage
- Learn Azure Data Lake Analytics by Example
- What is Azure Advisor?
- … all our Azure-related articles
Subscribe to our newsletter and stay up to date!
Subscribe to our YouTube channel (SQLNetHubTV)!
Like our Facebook Page!
Check our SQL Server Administration articles.
Check out our latest software releases!
Check our eBooks!
Benchmark SQL Server memory-optimized tables with In-Memory OLTP Simulator.
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.
I am not able to connect to SQL Azure.
Here is what my code is:
using (SqlConnection conn = new SqlConnection("Server=ufh1cinawq.database.windows.net;Database=MASTER;User ID=vksingh24;Password=xxx;Trusted_Connection=False"))
{
using (SqlCommand command = conn.CreateCommand())
{
conn.Open();
// Create a table
command.CommandText = "CREATE TABLE Vikash(Col1 int primary key, Col2 varchar(20))";
command.ExecuteNonQuery();
// Insert sample records
command.CommandText = "INSERT INTO Vikash(col1, col2) values (1, 'Vikash Kumar Singh'), (2, 'Kumar Saurabh'), (3, 'RockStar')";
int rowsAdded = command.ExecuteNonQuery();
// Query the table and print the results
command.CommandText = "SELECT * FROM Vikash";
using (SqlDataReader reader = command.ExecuteReader())
{
// Loop over the results
while (reader.Read())
{
MessageBox.Show(reader["Col1"].ToString().Trim());
MessageBox.Show(reader["Col2"].ToString().Trim());
}
reader.Close();
}
conn.Close();
}
}
I also check the firewall setting. Please help me to connect to SQL Azure.
Hi,
You need to properly configure the Firewall settings from within the SQL Azure – Server Administration web page.
To this end you can add a new Firewall rule, give it a name you like, and in the IP range include your public IP (the "Add Firewall Rule" dialog displays your public IP).
Then wait for a few minutes and try again.
I have also noticed that in the connection string you are using the "master" database and then you are trying to create a new table in it. Note that you cannot create a table in the master database as it is a system database. You can create a new database instead and work with it.
Hope this helps!
Cheers,
Artemakis