Execute SQL Script within Solution Explorer in VS 2008 Professional - sql

Does anybody know how I can execute a .sql file from within solution explorer in VS 2008 Professional?
I know you can do this in VS 2005 but cannot find this feature in VS 2008 professional

If it's within a database project, then you can right click on it and choose "Run" or "Run On...".

Use the "Server Explorer" (Ctrl+W, L) to add a connection, then once a connection is open you can right-click the server node and select "New Query". This will allow you to paste in the contents of an SQL file.
Alternatively open the SQL file, then from the "T-SQL Editor" toolbar you can choose "Connect" from your saved connections, and then select the database in the drop-down box.

According to this link you go to menu Data->Transact-SQL Editor->New Query Connection I think you can figure out the rest by yourselves.

Related

How to change the connection in Sql Server Data Tools Editor in Visual Studio

My goal is to keep SQL Server stored procedures under source control. I also want to stop using SQL Server Management Studio and use only Visual Studio for SQL related development.
I've added a new SQL Server Database project to my solution. I have successfully imported my database schema into the new project, and all the SQL objects (tables, stored procedures) are there in their own files.
I know that now if I run (with F5) the .sql files then my changes will be applied to my (LocalDB). This if fine, but what if I want to very quickly run something on another machine (like a dedicated SQL Server shared by the entire team)? How can I change the connection string of the current .sql file in the Sql Server Data Tools editor?
I have the latest version of Sql Server Data Tools extension for Visual Studio 2012 (SQL Server Data Tools 11.1.31203.1). I don't know if this is related to the current version, but I cannot find anymore the Transact-SQL Editor Toolbar.
I have also tried to Right-click on the sql editor, choose Connection -> Disconnect. If I do the reverse (Connection -> Connect...) the editor directly connects automatically (probably to my LocalDB), without asking me a dialog to choose my connection.
Another strange thing I've observed, if I try to run a simple SQL query (like select * from dbo.ApplicationUser I receive the following message (even if the autocomplete works):
Thanks.
(Note: I have the same issue with Visual Studio 2013)
Inspired by srutzky's comments, I installed the latest SSDT pack (12.0.41025). And bingo, like srutzky said there is a Change Connection option. But what's more, you can specify your Target DB by right clicking on the Project in the Solution Explorer, and going to Properties->Debug and changing the Target Connection String! If you're stuck on an older SSDT, then the below instructions will still work.
For SSDT 12.0.3-
I've also been plagued by this problem! My solution is below, but it has some Pros and Cons to it...
SOLUTION
I'm assuming that you are using a SQL Server Project in VS (I'm using VS2013 and SQL Server 2012).
Right click on your .sql file in the Solution Explorer and view Properties.
Change Build Action to None.
If the file is open for editing, then close it.
Reopen the file, and the T-SQL Editor should appear at the top.
Click Connect, and you will connect to your (localdb).
Click Disconnect.
Click Connect again and the SQL Server Connection dialog should appear.
Switch the connect string of '(localdb)\Whatever' to '.' (for some reason using '(localhost)' didn't work for me).
Voila, you should now be able to query against your SQL Server DBs! Rinse and repeat for every file you want this capability with... :/
PROS
You can finally run queries directly against your SQL Server DB
Your code can be organized in a nice VS solution (SSMS doesn't allow folders! :/)
You can (after switching Build Action setting back) Build the project
CONS
I'm not seeing any autocomplete/intellisense against the remote DB, although if you import your DB, then you could gain the intellisense from that
Requires each file to switch Build Action to None
This should be a fairly simple and straight-forward thing to do, that is, if you are using SSDT version 12.0.41025.0 (or newer, one would suppose):
Do either:
Go to the SQL menu at the top of the Visual Studio window
Right-click inside of the SQL editor tab
Go to Connection ->
Select Change Connection
Then it will display the "Connect to Server" modal dialog window.
If you do not see the options for "Disconnect All Queries" and "Change Connection...", then you need to upgrade your SSDT via either:
Visual Studio:
Go to the "TOOLS" menu and then "Extensions and Updates..."
Direct download:
Go to: http://msdn.microsoft.com/en-us/data/tools.aspx
The fastest way to achieve this is create a new SQL Connection, copy and paste the code then execute.
What I do is Tools->SQL Server->New Query.
Enter the database credentials (And make sure that the Database at the top is correct - I have hundreds of sp's in my master db on local :) )
Copy the source code from the editor, paste into the new query window.
Then Execute (CRTL-Shift-E).
You can leave this 'scratch' window open and pinned for easy access for subsequent executes.
If you want to deploy (i.e. publish) the entire database then you can setup a publish destination for each server, right click on the xml and select publish..

Visual Basic using SQL Database

So this is a homework question but I'm probably missing something really simple. I am working on a project where I was given a database. I'm not in the programming part of the assignment but I have to write the test case scenerios for the project before starting the project. Because I am working with an SQL Database that was provided. I am supposed to write the expected output from the database. If the user chooses shampoo from the products menu then the different brands of shampoo should then populate in my datagrid.
Because they gave us a sample database. How do I view it? I am I supposed to be able to write the expected output if I don't know what has been pre-programmed into the database?
Thanks in advance. I have already designed my layout and I guess I'm just stuck because I have been taught to plan your output before you program that way you know the program is working correctly. I don't want to write the assignment to find my output in case I did it wrong I guess, if that makes sense.
Here is how to do it in Visual Studio 2010
To create a data connection to the Northwind database—SQL Server
database file (.mdf)
On the View menu, click Server Explorer/Database Explorer.
In Server Explorer/Database Explorer, right-click Data Connections and
click Add Connection.
After you click Add Connection, either the Add Connection dialog box
or the Choose Data Source dialog box will appear.
If the Choose Data Source dialog box appears, select Microsoft SQL
Server Database File, and then click OK.
If the Add Connection dialog box appears, verify that the Data source
is set to Microsoft SQL Server Database File (SqlClient). If it is not
set to Microsoft SQL Server Database File (SqlClient), click Change to
open the Change Data Source dialog box, click Microsoft SQL Server
Database File, and then click OK.
Click Browse to locate the .mdf file that contains the Northwind
database.
Depending on the requirements of your version of the Northwind
database, either click Use Windows Authentication or click SQL Server
Authentication and type a user name and password to log on to the
computer running SQL Server. For more information, see Add/Modify
Connection (Microsoft SQL Server Database File).
Click OK.
A data connection to the Northwind database is added to Server
Explorer/Database Explorer.
Okay, so this should be pretty straight forward then:
Download and install SQL Management Studio Express.
Attach the database.
I wouldn't normally just list links in an answer, but this one really calls for it. These are very foundational URL's and won't be moving.

SSMS 2008 R2 - Create Table Dialog

I just recently upgraded to SSMS 2008 R2, and I'm working with a few Azure databases in our development environment.
I was surprised to see that, when I right click on the Tables folder and click New Table, a new query window is opened with a predefined CREATE TABLE script template, rather then the more UI friendly "design view" that I'm used to seeing.
My question :
Is there any way to enable the design view for creating tables?
Your best bet is to use SQL Server Data Tools for Visual Studio 2010 - 2012.
Unlike SSMS 2008 R2; this still allows you to access the Design view for a Windows Azure SQL Database.
As indicated here you don't need to have Visual Studio installed initially to use the tools; it will install the Visual Studio Shell for you.
To do so:
Install SSDT
Open Visual Studio
On the left you should have a tab called "SQL Server Object Explorer" (If it isn't there, you can find it under the View menu on the top bar)
Within the tab; right click on SQL Server and click "Add SQL Server"
Enter you SQL Azure connection details
Once the connection has loaded; open the Tables folder inside the database
Right click the desired table and click "View Designer"

When opening SQL Server Management studio with ssms command (+parameters) I need the Object Explorer to be filled and point to the right location

I'm using SQL Server management studio 2008.
In order to save me time, I open management studio with ssms -S -d -U -P.
It's working but the object explorer doesn't show anything until I:
click "connect".
re-insert credentials
And then I need to find the right DB.
How can I make object explorer be opened and in the right place?
As far as I know, there is no native functionality in SSMS to implement it.
There is a shortcut Alt+F8 which focuses SERVER in object exlorer, but not the database.
I have developed add-in for SSMS and have implemented function that you are looking for: it syncronizes current editor window with ObjectExplorer - database will be focused and expanded in the tree (you do not even need to have Server be opened in Object Explorer - it will be added in this case).
Follow this Link to see feature description

Is there a way to import SQL script into MDF database in Visual Studio?

I created a "Service-Based Database" (.MDF) in Visual Studio and now want to import a SQL script into it, but cannot find any way to do this:
I can right-click on the .mdf file and choose "New Query" but there is no place to copy in query text as in Management Studio
Does the Visual Studio 2008 Database Explorer have a way to enter or import SQL query text?
When you choose "New Query", the default behavior is to open the Query Editor. So you have to close the Add Table. After that, you should see a window with four panes - Diagram, Criteria, SQL and Result pane. You should be able to put your script to SQL pane and run from it. The Studio may complain about being unable to parse the script, just ignore it and hit ok, the script should run.
If you cannot see these panes, ensure you have "Query Designer" toolbar enabled and "SQL pane" button pushed.
However, I would recommend installing SQL Management Studio, there is a free express version available.
Why don't you just attach the database directly to your SQL Server (or SQLExpress) and then copy the database objects. The detach from the sql server again.