Quantcast
Channel: Windows Management and Scripting » Visual Studio
Viewing all articles
Browse latest Browse all 10

Working with SQL Azure Databases fromVisual Studio 2008

$
0
0

SQL Azure Services fall under the umbrella of the Windows Azure Platform. Windows Azure Platform is one of the four online services that include The Windows Azure storage and compute, and Azure AppFabric offered by Microsoft. The other services are Bing, Live, and Microsoft Advertising.

In this chapter, we will be looking at how SQL Azure data may be accessed from the client premises by applications using well-known Microsoft technologies for data access. The emphasis will be more on administering the SQL Azure databases using the client and server APIs. We will also discuss the best practices of accessing data from the client. For the practical elements of this chapter, we will be mostly using Visual Studio 2008 SP1, and with some comments on using Visual Studio 2010. The following topics will be covered:

  • SQL Azure architecture
  • Microsoft data access technologies
  • Easy way to connect to SQL Azure with Microsoft data access technologies
  • Preferred way to connect to SQL Azure
  • Connecting to SQL Azure using server APIs
  • Creating database objects using ADO.NET

SQL Azure architecture

SQL Azure has a four-layered architecture, a Client Layer followed by a Services Layer, a Platform Layer, and finally the Infrastructure Layer. A schematic of the architecture is shown in the following diagram:

The Infrastructure Layer has all the associated hardware in the Microsoft Data Center as described in Chapter 1, Cloud Computing and Microsoft Azure Services Platform, managed automatically by the SQL Azure Fabric. The Platform Layer, which is above the Infrastructure Layer, has all the SQL Server clusters managed by the fabric as well as the Management Services. The Services Layer above the Platform Layer does all of the business logic, provisioning, connection routing, billing, and so on. The Client Layer above the Platform Layer can be at the client site or at the client’s hosted site with all the applications that need access to SQL Azure.

Microsoft client technologies such as ODBC, ADO.NET, residing in the client site (or, in the Windows Azure Platform) converse with SQL Azure using TDS while WCF Data Services uses HTTP/REST. Applications such as PHP, SQL Server Applications and Tools, and WCF Data Services can work with ODBC and ADO. NET. Hence, TDS is the carrier of choice for information from SQL Server Tools, as well as the various applications.

The four-layered architecture shown in the previous diagram is based on the Microsoft documentation. As seen here, all applications access the Services Layer using TDS+Secure Sockets Layer (SSL) mediated by either ODBC or ADO.NET. The Services Layer routes the client requests to the Platform Layer where the SQL Server comes in, to process the client requests mediated by the SQL Azure Fabric.

The following diagram shows the TDS Gateway Layering in SQL Azure with some more details, copied, with permission, from a PowerPoint presentation by Jeff Currier to PDC 2009. This corresponds to the Services Layer of the previous image.

The Gateway layer enforces the authentication and authorization policy. This security enforcing layer isolates the application from the databases. The TDS Gateway takes care of a number of tasks, such as provisioning the endpoint, administering the AdminService, connection management, and so on. SQL Azure login requests arrive at the Gateway, which then accesses the Master and User DBs. Credentials to the User DB are matched to those on the Master and, after validation, a TDS session opens to forward requests to User DB. Make sure you watch the video presentation (Microsoft SQL Azure Database: Under the Hood) by Jeff Currier at the PDC here: http://microsoftpdc.com/Sessions/SVC12.

Application access to SQL Azure

Onsite client applications access the SQL Azure Services (databases) on the cloud using standard client libraries such as ODBC, ADO.NET (using TDS protocol), and so on. What this means, is that, all of these technologies are familiar to application developers and they are not required to learn new developmental techniques. In addition to Microsoft technologies, open source programming languages can also be used to develop Azure applications. We will look at the details of how client libraries access SQL Azure in this chapter. We will also learn how to manipulate database objects after accessing SQL Azure using T-SQL.

The client applications using TDS protocol arrive at the SQL Azure databases after passing through a load balancer, which forwards the TDS packets to the TDS Gateway layer, which then passes them on to the SQL Azure databases.

TDS and SQL Azure

Tabular Data Stream (TDS) was the technology originally created by Sybase to allow applications to access data stored in relational tables. Before SQL Azure came into existence, its predecessor SQL Data Services (SDS) was only able to access data using HTTP(s) or REST. Leveraging TDS, Microsoft skillfully morphed SDS into SQL Azure, so that the SQL Servers can be accessed in their native protocol and T-SQL code can be run in the cloud.

Presently, as shown in the following diagram based on the Microsoft documentation, data can be accessed by HTTP(s), as it is done by web facing applications (Scenario B) as well as using TDS from onsite applications and tools (Scenario A). Ultimately, however, it is TDS + SSL that finally reaches SQL Azure.


Microsoft data access technologies

 

There are two ways to interact with data using Visual Studio, either by designing an application using datasets and data adapters, or by performing direct operations on the data source, in this case the SQL Azure database. When and why you use one or the other depends on the situation you are trying to address. In situations where you are performing a database lookup, creating and modifying database structures, such as tables, views, stored procedures, or executing queries to get a single aggregate value, and so on, you directly interact with the database. This is because if you are creating or modifying database objects, you obviously cannot use datasets. On the other hand, if you are trying to access the data on the SQL Azure server from the web, you will be connecting using HTTP/HTTPS or REST-based technologies and will be using datasets and data adapters. The SQL Azure architecture accommodates both types of interaction. In this chapter, we will be looking at direct interaction with the database.

Using Visual Studio, you use data commands to work directly with the database. The steps to execute commands are as follows:

1. Create a connection.

2. Configure a command that uses the connection with a SQL statement or the name of a stored procedure.

3. Execute the command.

4. Retrieve the data that the command produces by a data reader.

Connecting to the database

In connecting to the SQL Azure database, you can use the following clients that are supported by Visual Studio 2008 by default:

  • SqlConnection
  • OdbcConnection
  • OledbConnection
  • EntityConnection

Data providers

When you use the assembly System.Data (C:WindowsMicrosoft.NET Frameworkv2.0.50727System.Data.dll), you can access the namespaces as shown in the following screenshot:

In order to access the EntityConnection, however, you should reference the System.Data.EntityClient.

The best way to understand the various namespaces/classes that help in connecting to the SQL Azure is to look at the namespaces/classes in the Object Browser.

The SqlConnection class member details are easily seen in the Object Browser when you search for SqlConnection.

Similarly, you search for OdbcConnection in the Object Browser to get all the related members.

Additionally, you can look up the details for OledbConnection and EntityConnection classes by referencing the corresponding namespaces. It is recommended that you review some of the members that create and manage database objects that we will be using later.

Connection string

In order to access the SQL Server you need to know the connection string. The connection string is a list of key/value pairs specific to each type of provider (SqlClient, ODBC, and OLEDB). If you know the connection string, you can directly specify it in the code, as shown here for SqlConnection without waiting for the intellisense to guide you, by typing it as an argument to SqlConnection(). Similar arguments may be made for the other providers.

This is easily available in the Visual Studio IDE as an intellisense drop-down. Intellisense is a great help in being productive.

In the following screenshot, the connection string to the SQL Azure is provided but partially hidden in the view. The screenshot shows everything that is accessible to the SqlConnection:

Following this, you will need to open the connection, which happens to be one of the methods of SqlConnection. The connection is necessary for you to create a command that you can execute.

Commands

The commands that we mainly use in SQL Azure are as follows:

  • SqlCommand
  • OleDbCommand
  • OdbcCommand
  • EntityCommand

Once the connection is established, you can create commands that can be run on the data source by setting up the code to create a command as follows:

CommandType shown in the previous screenshot is another property associated with commands. You should exercise caution when this property is of type Text. This is where SQL injection attacks take place. A preferred type to deter injection attacks is StoredProcedure or a parameterized query.

What do the commands accomplish? SQL commands are issued to accomplish several tasks such as the following:

1. You can execute to return result sets that can be read with an associated reader such as:

°°SqlDataReader

°°OleDbDataReader

°°OdbcDataReader

°°EntityDataReader

Search for everything that the SqlDataReader can do in the Object Explorer.

2. Execute Data Definition Language (DDL) commands to create, edit, and delete tables, stored procedures, and so on, provided you have permissions to do these operations.

3. Get database information by executing dynamic SQL commands to update, insert, and delete records.

4. Execute commands to return a single scalar value like an aggregate value.

5. Execute to return XML values and query the XML code.

In the following section, you will use connection builders to create tamper-free code that will prevent code injection by external means.

Using connection string builders

 

The previous section showed you how to use the connection strings to access the database. However, it is not good practice to send the strings in the form shown, as it can be tampered with. Connection strings must be carefully protected and secured. This is especially true when you are accessing them over the internet. One of the security considerations to prevent SQL injection attacks is to prevent externally injected script getting into the connection string. The injected script, while being syntactically correct, can introduce malicious code. If the connection string is obtained at runtime from user inputs, this is even more important.

While connecting to SQL Azure over the internet, make sure that the ADO.NET Encrypt (=true) and TrustServerCertificate (=false) connection properties are in place. This will ensure an encrypted connection and prevents the man-in-the-middle attacks (http://msdn.microsoft.com/en-us/library/ff394108.aspx).

In versions earlier to ADO.NET 3.5, compile-time checking of connection strings, formed by concatenating string values, did not occur, so at runtime, additional values of a malicious nature could be injected (for example, by adding a semi-colon followed by a key value pair). Review this article, for example: http://www. codeproject.com/KB/database/Connection_Strings.aspx.

Also, different providers supported a different syntax for connection string keywords (Password or PWD, and so on) making it difficult to string keywords manually and validate them. In ADO.NET 2.0, the new connection string builders for each .NET framework provider were introduced. The data providers, since then, included a connection string builder class, which would build a string for only acceptable key values for that provider. This acts as a filter for inserted code, allowing only those acceptable to the provider. You will find the details of these functions in SqlConnectionStringBuilder.

The different connection string builders trace their roots to the DBConnectionstringBuilder class. The .NET framework Connection String Builder class has the following connection string builders (EntityClient provider was added in Framework 3.5):

Provider Connection String Builder
System.Data.SqlClient SqlConnectionStringBuilder
System.Data.OleDb OleDbConnectionStringBuilder
System.Data.Odbc OleDbConnectionStringBuilder
System.Data.OracleClient OracleConnectionStringBuilder
System.Data.EntityClient EntityConnectionStringBuilder

In a manner similar to the connection strings, you can get a full appreciation of the properties that these classes support by looking them up in the Object Browser.

Using the connection builder is a recommended way of forming a connection string, not only for SQL Azure, but also for any place where a connection string is going to be used.

Accessing SQL Azure data using the Server Management Objects (SMO)

The Server Management Object model diagram available here: http://msdn. microsoft.com/en-us/library/ms162209.aspx reveals a rich feature set that this model offers to manage the SQL Server. SMO is based on SQL Server Foundation Classes (SFC). This model is based on the SQL Server API and replaces the older SQL-DMO (Distributed Management Objects) and is very comprehensive with many new features announced for SQL Server 2008. With SQL Azure you can use SMO but only a subset of the features is supported. Features like snapshots, trace, replay SQL Server events, service broker, and so on are not supported.

The SMO model is built on a hierarchy of objects with the server at the very top. The rest of the objects are all instance class objects. Using this model you can program all aspects of the SQL Server. The objects are only loaded when specifically referenced.

To access a server you need to establish a connection first. This applies to using SMO as well. You create an instance of the server object and establish its connection to an instance of the SQL Server. In the present context, the connection we would like to establish is to the SQL Azure database. Following the creation of a server object, a ServerConnection object is created, a variable that can be used again and again. One difference with the Client APIs described earlier is that it is not necessary to call a Connect method. SMO will automatically connect when required and after the operation it is going to perform is finished, it releases the connection to the pool. It is also possible to call a Non-pooled Connection property of ServerConnection object.

If you are in doubt at any time using a method or property, make sure you access the SMO in the Object Browser.

In order to view this in the Object Browser, after adding the three references, right-click Microsoft.SqlServer.Smo and choose View in the Object Browser. The assembly node is then revealed in the Object Explorer. This is because, sometimes, even though the assembly is added to the project in the Solution Explorer, it does not get displayed in the Object Browser.

We will look at connecting to an SQL Azure database using SMO in this chapter with a practical example.

Accessing SQL Azure from Visual Studio 2010 Express

 

Visual Studio 2010 Express is a free program from the Visual Studio suite that can work with SQL Azure. The details of downloading this program are described here: http://hodentek.blogspot.com/2010/06/get-these-web-development-tools-for.html. You may download and install the program in a few easy steps starting from here: http://www.microsoft.com/express/Web/. After installing, you will have a shortcut in Start | All Programs from where you can launch the application. Visual Studio 2010 Express installs a Microsoft Visual Web Developer 2010 Express and a Microsoft Visual Studio 2010 Express for Windows Phone. You will be using the web developer.

• Launch the application as an administrator (Run as administrator option after a right-click).

• We will connect to SQL Azure from, for example, a web application.

• Click on File | New Project and create an ASP.NET Web Application.

• Change the default name from WebApplication1 to one of your own, say ConSQLAzure.

• Click the menu item Data and click Add New Data Source.

• In the New Data Source window, click on the Database icon and click Next>.

• In the Choose Your Data Connection window, you may make a new connection using the New Connection… button.

• Click the New Connection… button to display the Add Connection window, as shown in the following screenshot:

 

The easy way to connect to SQL Azure using ADO.NET 3.5, ODBC, and OLE DB

You will now see how easy it is to connect to an SQL Azure database using Microsoft client programs such as ADO.NET, ODBC, and OLE DB. In connecting to the database, the single most important item is the connection string. The connection strings for SQL Azure are readily available at the portal as described in Chapter 2, SQL Azure Services. However, we may need to use appropriate arguments while constructing the connection string for OLE DB.

In the following steps a connection is opened and later closed for the Bluesky database, created in the first chapter with the ADO.NET 3.5, ODBC, and OLE DB.

• Run Visual Studio 2008 as an administrator from its shortcut in Start | All Programs.

• From File | New Project… (CTRL + N) |Visual Basic, choose from Visual Studio installed templates; a Windows Forms Application project in the default Framework option (3.5). Change the default name and click on OK. This adds a form Form1.vb and a My Project folder to the project (herein named TestConnect).

• Drag and drop three buttons on to the Form1.vb, as shown in the following screenshot:

Using ADO.NET to connect to the SQL Azure database

The SqlClient is used for establishing an ADO.NET connection to the SQL Azure database as described previously.

In the following steps, you will be writing the code to one of the form’s button click event that establishes a connection to the SQL Azure database.

1. To the click event of the button Connect Using ADO.NET, insert the code shown here:

Imports System.Data.SqlClient

‘Imports System.Data.Odbc

‘Imports System.Data.OleDb

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim conn As New SqlClient.SqlConnection

conn.ConnectionString = “Server=tcp:Your SQL Azure Server Name.database.windows.net;” & _

“Database=Bluesky;User ID=Your User ID@ Your SQL Azure Server Name;Password=Your Password;” & _

“Trusted_Connection=False;Encrypt=True;”

conn.Open()

If conn.State = ConnectionState.Open Then

MessageBox.Show(“Connection Opened”)

End If

conn.Close()

MessageBox.Show(“Connection Closed”)

End Sub

End Class

The trick to insert the connection string easily without errors, is to copy and paste from the portal.

2. Open IE browser and log in to your Windows Live ID account. Open http://Sql.Azure.com in your IE Browser. The Your User ID in the portal is the same as the project name in the portal. Click on the Database Name you want to connect to in the portal after highlighting the project.

3. You should see the tabbed folder with the tabs Databases and Firewall settings.

4. Click on Connection Strings in the Databases tabbed page and click on Copy to Clipboard, the ADO.NET connection string.

5. Paste the code into the statement, conn.ConnectionString=” “.

6. Now replace my password with your password.

7. Build and run the form.

8. Verify that you can connect to the database.

Alternatively, you can also establish an ADO.NET connection to the database concatenating the following parameters:

Initial Catalog=Bluesky

Data Source=tcp:Your Server Name.database.windows.net

User ID=Your Project Name @Your Server Name

Password=Your Password;

Trusted_Connection=False

Encrypt=True

Using ODBC to connect to the SQL Azure Database

This is no different from connecting to ADO.NET as shown in the previous steps except that, you must include the imports System.Data.ODBC in your code for the click event of the button Connect Using ODBC. Again, cutting and pasting from the portal is the easiest way.

From the portal, cut and paste the connection string for ODBC, as described in the previous steps, into the code for the click event of the button marked Connect Using ODBC, as shown here, and change the password:

‘Imports System.Data.SqlClient

Imports System.Data.Odbc

‘Imports System.Data.OleDb

Public Class Form1

+ Private Sub Button1_Click …(shown collapsed)

Private Sub Button2_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click

Dim conn As New Odbc.OdbcConnection

conn.ConnectionString = “Driver={SQL Server Native Client 10.0};” & _

“Server=tcp:Your server Name.database.windows. net;Database=Bluesky; ” & _

“Uid=Your Project Name@Your Server Name;Pwd=Your Password;Encrypt=yes;”

conn.Open()

If conn.State = ConnectionState.Open Then

MessageBox.Show(“Connection Opened”)

End If

conn.Close()

MessageBox.Show(“Connection Closed”)

End Sub

End Class

Again, build the project and verify that you can connect to the database.

Using OLE DB to connect to the SQL Azure database

Only ADO.NET and ODBC are supported on the SQL Azure platform. However, client connection to the SQL Azure database using OLE DB is possible. You need to construct the correct string that is supported by the OLE DB provider, SQLOLEDB. The connection string parameters are:

Provider = SQLNCLI10.1;

Server = tcp:Your Server Name.database.windows.net;

Database = Bluesky;

UID= Your Project Name @ Your Server Name;

Password = Your Password;

Enter the code to the click event of the button marked Connect Using OLE DB:

‘Imports System.Data.SqlClient

‘Imports System.Data.Odbc

Imports System.Data.OleDb

+ Private Sub Button1_Click …(shown collapsed)

+ Private Sub Button2_Click …(shown collapsed)

Private Sub Button3_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button3.Click

Dim conn As New OleDbConnection

conn.ConnectionString = “Provider=SQLNCLI10.1;” & _

“Server=tcp: Your server Name.database.windows.net;” & _

“Database=Bluesky;” & _

“UID= Your Project Name@Your Server Name;” & _

“Password= Your Password;”

Try

conn.Open()

If conn.State = ConnectionState.Open Then

MessageBox.Show(“Connection Opened”)

End If

conn.Close()

MessageBox.Show(“Connection Closed”)

Catch ex As OleDb.OleDbException

MessageBox.Show(ex.Message.ToString)

Finally

End Try

End Sub

End Class

Again, verify that you can establish the connection by building the project and running the form.

In the previous code, a Try…Catch exception handling routine is added, which helps in trapping errors arising, while connecting to the database.

Using ADO.NET to connect to a SQL Azure database in C#

Although the code shown earlier is in VB, it could be easily written in C#, as shown here for one of the cases:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace TestConCSharp

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection();

con.ConnectionString=”Server=tcp:Your Server Name. database.windows.net;Database=Bluesky;User ID=Your Project Name@ Your Server Name;Password=Your Password;Trusted_Connection=False;Encrypt=T rue;”;

con.Open();

if (

conn.State==ConnectionState.Open

)

MessageBox.Show(“Connection Opened”);

else

MessageBox.Show(“Connection not open”);

con.Close();

MessageBox.Show(“Connection closed”);

}

}

}

 

Using SQL Server Management Objects (SMO) to connect to SQL Azure

For working with the SQL Server 2008, Microsoft has provided a collection of namespaces (SMO), which contain different classes, interfaces, and so on that help to programmatically manage the server. We will now use the elements of this namespace to access the SQL Azure server. This is a powerful tool, as it is based on the SQL Server API object model.

In the following steps, we will create a Windows Forms Application (even a console application can be used) and add references to the Server API that works with SMO and show how a connection can be established to SQL Azure.

1. Create a Windows Forms Application project (herein SmoSqlAzure) and to the default drag-and-drop a button.

2. Right-click on the References node and from the drop-down click on Add Reference….

3. The Add Reference window gets displayed, as shown in the following screenshot:

4. In the Add Reference window under .NET scroll down and add the following references:

Microsoft.SqlServer.ConnectionInfo

Microsoft.SqlServer.Management.sdk.Sfc

Microsoft.SqlServer.Management.Smo

5. The project folders in the Solution Explorer should appear, as shown, after the references are added.

6. To the code page of Form1.vb, add the following code:

Imports Microsoft.SqlServer.Management.Smo

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

‘Provide SQL Azure connection Parameters as follows

‘Use the ConnectionContext of the server

Dim srv As New Server(“Your Server Name.database.windows. net”)

srv.ConnectionContext.LoginSecure = False

srv.ConnectionContext.Login = “Your ProjectName@Your Server Name”

srv.ConnectionContext.Password = Your Password”

srv.ConnectionContext.DatabaseName = “Your Database Name”

‘create a strcutured exception block as shown

Try

srv.ConnectionContext.Connect()

Catch ex1 As Exception

MessageBox.Show(ex1.InnerException.ToString)

Finally

MessageBox.Show(srv.ConnectionContext.IsOpen)

End Try

If (srv.ConnectionContext.IsOpen) Then

srv.ConnectionContext.Disconnect()

MessageBox.Show(srv.ConnectionContext.IsOpen)

End If

End Sub

End Class

The code shows some of the members of the SMO such as ConnectionContext, IsOpen, Connect, Disconnect, and so on.

Creating database objects using ADO.NET

Here, you will now connect to the SQL Azure with your connection string stored in the application’s settings file. You will also create and drop a database, create a table, and populate it, and so on. Although only the code for ADO.NET is demonstrated, ODBC and OLE DB may also be used.

Using connection string information in application settings

The very first thing, in this task, is to save the connection string information to the application settings. There are two databases in the SQL Azure server we have been working with, the master database and the database named Bluesky. In the next step, we will store the master database’s connection string to the settings file.

1. Create a Windows Application project using the Windows Forms Application template and give it a name of your own (herein it is CreateDBObjects).

2. The program creates a project folder in the Solution Explorer, as shown in the following screenshot:

3. Add four buttons and a label and configure their visual appearance as follows:

The buttons are numbered from 1 to 4 and must be associated with the code that follows.

Inserting connection string information to the application settings file

In the following steps you will copy the connection string information from the SQL Azure portal and save it in the configuration file of your application.

1. Copy the connection string from the SQL Azure portal as you have done before (shown here details are masked).

Server=xxxxxxxxxx.database.windows.net; Database=Bluesky;User ID=yyyyyy@xxxxxxx;Password=myPassword;Trusted_ Connection=False;Encrypt=True;

2. From the Projects menu click open the projects properties page.

3. Click on the Settings tab in this window. This opens the Settings page of the application, as shown in the following screenshot:

4. Set the following values for the four fields in the previous window:

Name: Give a name (herein, it is mdbs).

Type: String (no change).

Scope: Application (choose from drop-down).

Value: Enter the connection string of the master database you copied from the portal here. Make sure it is all in one line. If necessary, click on the value field, which opens a wider and a longer window.

5. Build the project. An app.config file gets added to the project.

6. Click on the app.config file.

7. The app.config file gets displayed as shown in the follwoing screenshot. Your connection string information will appear in the indicated position between <value> and </value>. Review the file and make sure there are no syntax errors, characters, such as “& _”, which are common line continuation character strings in VB.NET, extra white spaces, and so on.

Connect to the database on the server using the settings

 

In this code, you will be connecting to the database using the connection string you saved to the configuration file, in the previous section.

1. Click on the app.config file.

2. To the click event in the code page of Form1.vb, add the following code:

Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

GetConnected()

End Sub

Private Sub GetConnected()

‘Use the connection string in the settings page

Dim constrg = New CreateDBObjects.My.MySettings

Dim con As New SqlConnection

‘the current connection will use value in the ‘settings

con.ConnectionString = constrg.mdbs

con.Open()

If con.State = ConnectionState.Open Then

MessageBox.Show(“Connection is open now”)

End If

con.Close()

End Sub

3. Build the project and run the form.

4. Verify that you can connect to the SQL Azure server.

5. The database you will be connecting to will be the database in the connection string you supplied.

Summary

 

In this article, the SQL Azure architecture was briefly reviewed. Accessing SQL Azure to manipulate objects, therein using client technologies such as ADO.NET, ODBC, and OLE DB, was described with examples. Connecting to SQL Azure for data manipulation, using Server APIs with examples, was considered using Visual Studio 2008 SP1. Also considered was the best practice of using ConnectionBuilder to thwart SQL injection attacks in detail. Examples of using ADO.NET for creating database objects were detailed using both Client APIs as well as Server APIs. The method to connect to SQL Azure using the entity framework was not described here, but is described with a complete example in next article, Database applications on Windows Azure Platform accessing SQL Server databases.


Filed under: Azure

Viewing all articles
Browse latest Browse all 10

Trending Articles