Managing SQL Azure ADO.NET

Posted: December 3, 2011 in Windows Azure
Tags: , ,

In the previous post, we were talking about creating servers, databases and tables using the Windows Azure portal. Now we will talk about developing it using ADO.NET.

First of all what do we need to start the development?

Your username, password, server name and database name.

Create the first connection string to the master database to be able to create other databases like the following:

            SqlConnectionStringBuilder constrbuilder;
            constrbuilder = new SqlConnectionStringBuilder();
            constrbuilder.DataSource = dataSource;
            constrbuilder.InitialCatalog = “master”;
            constrbuilder.Encrypt = true;
            constrbuilder.TrustServerCertificate = false;
            constrbuilder.UserID = userName;
            constrbuilder.Password = password;

Now you are able to create a new database like the one we did in the previous post but using the ASO.NET.

SqlConnection con = new SqlConnection(connString1Builder.ToString());
SqlCommand cmd = con.CreateCommand();
con.Open();
cmd.CommandText = string.Format(“CREATE DATABASE {0}”, databasename);
cmd.ExecuteNonQuery();
con.Close();

as the previous example as long as you have the data required to access the database you can normally do anything like the SQL server

            SqlConnection con = new SqlConnection(constrbuilder.ToString());
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = “ENTER ANY COMMAND YOU WOULD LIKE TO BE DONE ON YOUR DATABASE”;
            cmd.ExecuteNonQuery();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s