Can't get SQL code working on Visual Studio - sql

With the exception of a little HTML/Javascript, I've always used C++ on Visual Studio C++ 2010. I have an internship next week that will involve the use of SQL and .Net. I'm currently reading a book on SQL and it instructs us to go to its website to download code so that we can practice. The website is here: http://forta.com/books/0672336073/ I downloaded the one labeled "Microsoft SQL Server" which is basically two .sql files. One creates a simple database and the other one populates it.
However, for the life of me I cant figure out how to do it on Visual Studio 2010 or 2013 (I downloaded the full version of 2010). If anyone knows how to get these file to run on either VS version I would be very happy. I'm used to just hitting the green arrow button in Visual Studio C++ 2010, which I believe is execute. Obviously it isnt working for my sql code.
On VS 2013 I created a new "SQL Server Database Project". I then added two new "Application Role" items for the two .sql files. Now after clicking the "SQL" tab and "Execute" I get error messages about duplicate objects/keys, which I assume means the database is already created...but how do I view it? At one instance I mustve hit the right combination of buttons because I was able to view my database in a table format.
Thanks for any suggestions!

In visual studio go to view and select sql server, (see image) then click the + symbol and connect to the server installed on your machine (if its not yet listed)

Related

Can't create a simple SQL Test

I'm trying to get just basic unit testing on an SQL database through a Visual Studio project.
The first thing I tried was just creating a simple .Net Core Unit Testing library.
Then I added an SQL Test, and it couldn't link to the SqlDatabaseTestClass`. Looking around, this is a fairly ubiquitous issue, but I found an obscure comment at the bottom of https://developercommunity.visualstudio.com/content/problem/29842/database-unit-test-project-does-not-build-in-visua.html that said that it's probably a linking issue.
So I created a .Net Framework Unit Test project. Now that has no option to add an SQL Test.
So I found an article that says that I should try creating an SQLDatabase https://www.codeguru.com/csharp/.net/net_data/create-sql-server-database-unit-tests-using-visual-studio.html
But then I got an error that The server version or database compatibility level is not supported. Import Database in VS 2015 SQL Server Database Project
The solution to that was to go to the SQL Server Object Explorer and right click on the DB and create an SQL project by right clicking on the Database. And that worked! Finally I have a project with database tables.
But now when I right click on a stored procedure, I don't get the option to create a unit test.
I figured it out.
Open the View -> SQL Object Explorer in the top menu bar in Visual Studio 2017. Make sure it's the Object Explorer and not the Server Explorer. These instructions presuppose that you have your connections setup properly.
Right-click on your database and click Create New Project. Make sure the directory is correct because it wasn't for me.
After the database imports, go to your Error List and resolve all those errors. Now you should be able to right-click on a stored procedure in either the SQL Server Object Explorer and select Create Unit Tests.
Allowing Visual Studio to do all the heavy lifting sorted me out.

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 Studio 2013 SQL Query and View Designer not appearing

Not sure why the Query and View Designer is not appearing as per
http://msdn.microsoft.com/en-us/library/vstudio/ms172013.aspx
It's an ASP.net project with database in the AppData folder, and a connection using SQL 2008 Express.
If it helps the connection string is:
Data Source=.\SQLEXPRESS;AttachDbFilename="C:\folders...\App_Data\database.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True
If I open the project in VS2012 (where I've worked on this project for some time), find a table, right click, new query, I get the nice interactive visual design tool.
If I open the project in VS2013 (just installed) and do the same, blank page named SQLQuery1.sql and a basic connection to SQL Express (ie. master, model, etc.)
MS page tells me Tools / Options / Visual Database tools - but this doesn't exist on my installation!
Am I missing something?
Also tried with blank project, added App_Data and a new database (so using LocalDB), added a table, right click, new query - exactly the same blank file.
I have been having the same problem and think I have found the solution.
When you are adding the connection to the Database, in the Add Connection Dialog.
Click 'Change...' next to the Data source
Dialog opens showing 'Change Data Source'
You probable have selected 'Microsoft SQL Server' and then below that there is a drop down where you can select:
.NET Framework Data Provider for OLE DB
.NET Framework Data Provider for SQL Server (This is probably selected by default)
try changing to the OLE DB connection.
Click OK, and then complete the rest of the connection on the 'Add Connection' dialog.
I have then found that using the database works as it did in VS2012 and as you are describing above.
Was searching for a solution to a similar issue. Unable to use Query Designer for a local mdf file database. This is what I found...hope it helps someone.
Reading the comments here (http://social.msdn.microsoft.com/Forums/sqlserver/en-US/ed4675d3-aa84-47db-bdf5-f852355409e6/query-designer-not-displaying-in-visual-studio-2013?forum=ssdt).
According to the moderator of that forum question (Kevin Cunnane of Microsoft), "in Visual Studio 2013 the query designer and database diagram features are no longer available from the Server Explorer. If you wish to keep using the query designer, the only other workaround for now is to access it from SSMS. "
My current workaround is to use Visual Studio 2012 Server Explorer to view data and write queries for a local MDF file. And to use Visual Studio 2013 for everything else.
I have researched about 5 minutes how to run a query in Visual Studio. At the end I found this:
Even though i followed same steps , It did not work at once it gave some error and Studio closed. It was problem at my end and unknowingly it got sorted out on its own.
This component is called "SQL Server Data Tools". You can download it from MSDN. I found this page simply by googling "vs2013 ssdt". It seems that what TsugaSoft posted is true, however you still can install this component separetely... And it also works for Visual Studio 2012.

Database Projects: Visual Studio 2008 vs 2010

I just installed VS 2010 for the purposes of running someone else's DB project that was created using the 2010. In VS 2008 I was able to right click on a SQL file ans select "Run ON" to run the script on a given SQL Server. I don't see this option in 2010. Is it available and if so how do I get access to it?
The database project format that was included in VS 2005 and VS 2008 has been completely removed in 2010.
Rather than holding loose SQL files that could be run independently, it acts a bit more like a compiler. When the project is built, the SQL files are parsed and (for the most part) turned into an XML-description of the database schema.
If you're trying to create or update a database based on all of the scripts in the project, right click on the project name in Solution Explorer and select "Properties". Under the Deploy tab youchange "Deploy Action" to "Create a deployment script and deploy to the database". Specify the connection settings and database name.
When you select "Deploy" on the right-click context menu of the database project, Visual Studio will build the database deployment files and generate the database. It's generally intelligent enough to update existing databases as well, assuming the project was designed well.
If you're attempting a remote deployment to a production SQL Server, things get a bit trickier. There is no single set of SQL files you can run - part of the database schema is stored as XML.
Take a look at Link - it describes how to use the "vsdbcmd.exe" utility to deploy a database file.
If you just want to run a single SQL file or test part of one, open the file, right click, and select "Execute SQL"
DNRTv # 167 talks about VS 2010 Database Projects in detail.
As #ShadowChaser already mentioned, DB Project in VS 2008 simply had the scripts in it, and 2010 version is a lot more complex and has a lot more features.

Editing SQL query with Visual Studio 2008

Would you recommend me the best approach to edit SQL query with Visual Studio 2008 Professional, please?
I know I can open Query window from context menu in Server Explorer and edit text in SQL Pane. But unfortunately I am not allowed to save query to a file and Find and Replace commands are not working there.
Alternatively I can open text file with extension .SQL in Visual Studio editor obtaining syntax highlighting and full editing capabilities but losing possibility to execute the script.
Now I copy query text from one window to another and back but I hope there is better solution.
Many thanks for suggestions!
When I have to connect to Sql Server, I use NetBeans, as it has a number of features that make it much easier to use than Visual Studio. One of these is intellisense, which is very useful when you have an alias for a table name and lots of long field names. The general handling of connections and connection pools is also much cleaner in NetBeans. I could go on, but suggest trying it for yourself. You will almost certainly need the Sql Server jdbc driver, which can be downloaded from http://msdn.microsoft.com/en-us/data/aa937724.aspx
If you create a Database project within your solution in Visual Studio, then you can set up a default database connection for that project. Then any *.sql files that are included in the database project can be executed against that connection. What I usually do is select the text to be exectued and right-click it, then select "Run Selection".
You can set up any number of database connections under the "Database References" node in the solution explorer, and choose the one you want to run your query against.
Wow you are right, this is a real pain. Would it be acceptable for you to use a local copy of the SQL Management Studio (the name might be wrong)? You would be outside of VS but you could still edit your query and run it at the same time. Definitely not optimal but better than nothing.
Good luck.