• subscribe
July 11, 2006 12:00 AM

Creating T-SQL Statements in SQL Server 2005 and Visual Studio 2005

Windows IT Pro
InstantDoc ID #50502
Downloads
50502.zip

Of course, you might do all your development in Visual Studio, in which case you probably won't want to build and test your T-SQL statements in SSMS or in a sqlcmd command. Fortunately, Visual Studio 2005 Professional Edition and higher includes the Server Explorer interface, from which you can add a database connection, then select New Query to build a query. To create the SELECT statement in Listing 2, take the following steps:

  1. Open Visual Studio 2005.
  2. Select Server Explorer from the View menu to open the Server Explorer pane.
  3. In Server Explorer, right-click the Data Connections node, then click Add Connection. The Add Connection dialog box appears, as Figure 4 shows. We've added configuration settings to Figure 4 for creating a connection to the Inventory database on the local machine. (The connection is automatically saved in Server Explorer unless you explicitly delete it.) Notice that the Server name text box shows the name localhost. You can specify a NetBIOS computer name, an IP address, or a host name to connect to the local computer or a remote computer running SQL Server 2005.
  4. From Server Explorer, right-click the Inventory database node, then click New Query to begin the Query Build process. The node directly below the Data Connections node should be <computer name>. Inventory.dbo where <computer name> is the name of the computer running the Inventory database. The Query Designer window appears with the Add Table dialog box active. At this point, you're at Step 3 in the Query Designer query-building procedure we walked through earlier.
  5. Follow Steps 3 through 6 in the query-building procedure to build a SELECT statement from Visual Studio 2005. Once you've built the SELECT statement, you can run it against the Inventory database to return results. To run the SQL statement, click Execute SQL on the toolbar (the button looks like a red exclamation mark—!).

Like SSMS, Visual Studio 2005 lets you perform many database operations. For example, you can use Visual Studio templates to create a variety of scripts—such as table, trigger, and stored-procedure scripts—and you can use Query Designer to build INSERT, UPDATE, and DELETE statements. Visual Studio 2005 also provides an interface for using the Common Language Runtime (CLR) to program databases. Even if your primary function is programming and you use the Visual Studio 2005 IDE, we recommend you install SSMS because it's dedicated to managing SQL Server databases and related services.



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here