Wednesday, September 23, 2009

My first SQL azure Application on the Cloud !!!

Hi Folks,

Few months ago, i worked with Azure Services, and since i'm interested to develop applications over the cloud which extensively use SQL Server as a backend, i used BLOBS to store my data... actually it wasn't that easy to use, but the product was still in beta in that time.

Recently, I have some time to develop and start using SQL Azure services, A big start by allowing us to create the Database over the website, then all other operations ( CRUD ) for the Database design and object model can be done using ADO.Net classes.

C# Code for connecting and creating a table over the cloud:

SqlConnectionStringBuilder connString2Builder;
connString2Builder = new SqlConnectionStringBuilder();
connString2Builder.DataSource = "MyServer.ctp.database.windows.net"; // check your profile to know server name.
connString2Builder.InitialCatalog = "YourDB"; // you can create your own their.
connString2Builder.Encrypt = true;
connString2Builder.TrustServerCertificate = true;
connString2Builder.UserID = "myUserName";
connString2Builder.Password = "myPassword";

// Connect to the sample database and perform various operations
using (SqlConnection conn = new SqlConnection(connString2Builder.ToString()))
{
using (SqlCommand command = conn.CreateCommand())
{
conn.Open();

// Create a table
command.CommandText = "CREATE TABLE MostafaTable(Col1 int primary key, Col2 varchar(20))";
command.ExecuteNonQuery();

// Insert sample records
command.CommandText = "INSERT INTO MostafaTable(col1, col2) values (1, 'string 1'), (2, 'string 2'), (3, 'string 3')";
int rowsAdded = command.ExecuteNonQuery();

// Query the table and print the results
command.CommandText = "SELECT * FROM MostafaTable";

using (SqlDataReader reader = command.ExecuteReader())
{
// Loop over the results
while (reader.Read())
{
Console.WriteLine("Col1: {0}, Col2: {1}",
reader["Col1"].ToString().Trim(),
reader["Col2"].ToString().Trim());
}
}
}
}

-- To read more about SQL Azure services:
http://msdn.microsoft.com/en-us/library/ee336279.aspx

-- SQL Azure Forums:
http://social.msdn.microsoft.com/forums/en-US/ssdsgetstarted/threads/


Hope it helps.


Regards,
Mostafa arafa

1 comment:

Anonymous said...

It is very interesting for me to read that article. Thanx for it. I like such themes and everything that is connected to this matter. I would like to read a bit more soon.