Databases Reference
In-Depth Information
7.
Before you run the application, make sure your Azure firewall settings are up to date (via the
SQL Database 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 Database. The caveat is what your application contains. As mentioned earlier, if you have any inline T-SQL, at
a minimum you need to ensure that its syntax is supported by SQL Database. The likelihood is that it is, but it's always
safest to check and test .
Even though you've connected to SQL Database, does that affect your data-access code? The next two sections
discuss using a data reader and a dataset when connecting to SQL Azure.
Using a Data Reader
As you become more and more familiar with SQL Database, 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.
2.
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 Database
instance 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 FirstName, LastName FROM Person.Person",
conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
try
{
while (rdr.Read())
{
listBox1.Items.Add(rdr[0].ToString());
}
rdr.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
3.
Run the application, and click the button on the form. Within a few seconds, the list box
populates with names from the Users table.
 
Search WWH ::




Custom Search