Database Reference
In-Depth Information
5.
On your SQL Azure Server Administration page, select the database you want
to connect to.
6.
Click the Connection Strings button, and click the Copy to Clipboard link for
the ADO.NET connection string.
Back in your Visual Studio project, replace the local connection string in the
GetConString method with the SQL Azure ADO.NET connection string, as
shown in the following code. Be sure to enter your correct password into the
connection string:
string GetConString()
{
return "Server=tcp: servername .database.windows.net;Database=TechBio;
UserID=SQLScott@ servername ;Password= password ;
Trusted_Connection=False;Encrypt=True;";
}
7.
Before you run the application, make sure your Azure firewall settings are up
to date (via the SQL Azure Server Administration page). Then, run the
application, and click the button on the form. If everything is configured
correctly, you should get a message box that says “Connection Made.”
Granted, this is a very simple example, but it illustrates how easy it is to take an existing application
and point it to SQL Azure. The caveat is what your application contains. As mentioned earlier, if you
have any inline T-SQL, you at a minimum need to ensure that your inline T-SQL is supported by SQL
Azure. The likelihood is that it is, but it's always safest to check and test .
Even though you've connected to SQL Azure, does that affect your data-access code? The next two
sections discuss using a data reader and a dataset when connecting to SQL Azure.
8.
Using a Data Reader
As you become more and more familiar with SQL Azure, you'll find that you don't need to make a lot of
changes to your application code except possibly any inline T-SQL. The beauty of all this is that you're
using a proven and trusted data-access technology, ADO.NET. Thus, nothing really changes. Let's
modify the application and click event code to illustrate this. Follow these steps:
1.
Add a new list box to the form.
In the click event, add the code in bold in the following snippet. This new code
uses the SqlDataReader class to execute a simple SELECT command against the
SQL Azure database and then iterate over the SqlDataReader to populate the
list box:
private void button1_Click(object sender, EventArgs e)
{
string connStr = GetConString();
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand("SELECT Name FROM Users", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
try
2.
Search WWH ::




Custom Search