SQL Server debug Stored Procedure from .NET code - sql-server

I'm wondering if there's a way to debug a stored procedure at the time it's called from .NET code? What are my possibilities?
I'm using Visual Studio 2013 Community Edition and SQL Server 2014

I have managed to figure it out myself.
It's quite similar to what you can find in VS 2010.
I had to import database as a project to my solution.
Turn on SQL Debugging in my application project properties.
Re-open my project.
Connect to my SQL Database through SQL Server Object Explorer.
Right click on my SQL Server I connected to earlier and check "application debugging".
Find SP I wanted to debug, double click on it, set my breakpoint (it says it will not be hit, but eventually it will).
Launch my application using debugger.
Everything now works as expected, the debugger stops at my breakpoint with no problem.

Related

Need detailed instructions for debugging SQL Server stored procedure in Visual Studio 2019

I am a victim of the debugger having been removed in SSMS v18. You are now supposed to do it in Visual Studio 2019, but I am failing. I have read many webpages on the subject. For example:
Why aren't my breakpoints hitting SQL In Visual Studio 2019 debugging script from network server
How to: Debug a SQL Server CLR Integration stored procedure
How to debug SQL from Visual Studio 2019
What I try: here is the procedure:
CREATE PROCEDURE [dbo].[sp_DevTest]
AS
DECLARE #dHrs decimal(12,4)
SELECT #dHrs = dbo.TNPF_OperHours(592,'2020/10/31 23:59 -07:00')
RETURN #dHrs
Its purpose is to test function dbo.TNPF_OperHours.
I set a breakpoint on the SELECT statement. From the SQL main menu item I click on 'Execute With Debugger'. The entire procedure is highlighted in yellow; the breakpoint disappears. I cannot set a breakpoint. I press F11. The yellow highlighted procedure disappears and I am back at the editing window with the breakpoint highlighted.
I thought maybe I need a SQL project in the solution. After creating one I could not find a way of debugging from there.
My development environment is a single Win 10 computer with this SQL Server installation:
I use Visual Studio 2019 Community Edition. I develop websites for tiny non-profit organizations. I am retired and I donate my time. And I am stuck and would greatly appreciate your help.
Just install the latest SSMS v.17.9.1 that supports inline debugging.
It can coexist with SSMS 18.* on the same machine.
SSMS 17.9.1 download

How to debug CLR database object (stored procedure) using Visual Studio 2019

I have written a CLR stored procedure (.NET Framework 4.5 and IDE VS 2019) and trying to debug the CLR stored procedure before deploying it to SQL Server 2016. However, I am unable to locate the debug option for CLR DB object.
I did try steps provided on Microsoft document but unable to find [Test Scripts] folder in Solution Explorer. I am using Visual Studio 2019.
https://learn.microsoft.com/en-us/sql/relational-databases/clr-integration/debugging-clr-database-objects?view=sql-server-2016
What exactly do you mean by:
debug the CLR stored procedure without deploying it on SQL server
? The code only runs in SQL Server. It needs to exist within SQL Server in order to be callable as the environment that calls it is SQL Server. Visual Studio comes with SQL Server Express LocalDB so that you can easily test stuff like this on your local dev box without having to deploy it anywhere outside of your personal dev environment. When you create a database project, the debug connection string (i.e. where it deploys to when publishing to test, whether you start debugging or not) should be preconfigured for your local instance of LocalDB.
Please try the steps I outlined in my answer to "How to debug a CLR Stored procedure in VS 2013". That was tested on VS 2015, and I expect the behavior to be the same in VS 2019. Either way, that answer contains some required steps that are missing from the MS documentation.

How to debug SQL Server T-SQL in Visual Studio 2012

How does one debug a T-SQL stored procedure in a multi-tier application in Visual Studio 2012?
To be clear, I want to set a breakpoint in a sproc in VS 2012, and hit it when the sproc is called from an ASP.NET WebForms app in the same debugging session.
When following the same steps as for VS 2010, the breakpoints aren't hit inside the sproc.
Debugging T-SQL in a sproc on a SQL Server 2008 R2 Express database works as expected in Visual Studio 2010.
To be sure everything was enabled properly, I went over the instructions for VS 2010 (here), but no such page exists for VS 2012 or .NET 4.5.
It seems the missing step is to enable "Application Debugging", but no such option exists in the Server Explorer > Data Connections context menu in VS 2012.
VS 2010 Application Debugging
VS 2012 No Application Debugging
You need to open "SQL Server Object Explorer. Not "Server Explorer". That is what is different between 2010 & 2012. Then right click on the server and select "Application Debugging".
This is for VS2012 and SQL2012. Yes things are somewhat different for other versions, but kinda follow similar setup. It is tricky because one has to have various settings just right or it won't work.
vanilla install of both VS and SQL with all correct options (how to do this is outside scope of this article)
full admin rights to entire environment and sysadmin rights to sql (fundamentally a development environment; you would not want to do this in a production environment)
i always debug .net web apps under full local IIS which is a windows feature installed from control panel
go .net project properties, web, use local iis web server (i also stick to default port 80)
while i develop my databases etc using VS sql server database project, i always work under full standalone local SQL; i use the schema compare to refresh that sql with changes made in VS (how to do this is outside scope of this article)
do not place your breakpoint on that copy of the sp; that version of the sp is only a blueprint and not the runtime version
go .net project properties, web, debuggers (at bottom) enabled for .net and sql server
view sql server object explorer (not server explorer)
click add sql server, add your full local sql server using sysadmin credentials (i always use sa for such work)
right click on your full local sql server and enable application debugging
there go find the sp and right click and view code, then add your breakpoint there; this is runtime version of sp (note that i never make changes to sp there, i go do these in my master copy under the database project)
rebuild solution, and execute .net web app in debug mode from within ide by clicking green arrow internet explorer
I hope I have not forgotten anything. If I have I'll come revised my post.
All this may sound complicated. It is. But with a little discipline and patience it is priceless.
Good luck.
I believe the easiest way to do this would be to add DB as a project to your solution. You can do this by right clicking on the DB in the SQL Server Object Explorer (SSOX). This is a really good way to develop and debug your DB's. After you have added the DB to your project, you can add breakpoints anywhere you want, and debug against LocalDB (or another target if you wish). The largest drawback to this approach is that your existing data will not migrate with you (although you will be able to easily publish any changes back to the SQL DB at will). See the documentation on SSDT on MSDN for further guidance.
The following in detail article explains exactly how to enable debugging of a Stored Procedure when a .Net application is executed.
http://www.sqlmag.com/content1/topic/debugging-stored-procedures-142054/catpath/sql-server

How to fix Visual Studio's error "This server version is not supported..." for SQL Server 2012

I recently installed Visual Studio 2010 and SQL Server 2012 on a Windows Server 2008 R2 machine - (including all updates/Service Packs).
When attempting to work on a SQL Server database from within Visual Studio, VS throws the following error:
This server version is not supported. Only servers up to Microsoft SQL
Server 2008 are supported
I recall getting this same error a while back (on a different computer) when I updated a SQL Server 2008 instance to SQL Server 2008 R2. The fix, back then, was to install (or reinstall) Visual Studio 2010 SP1.
Now that I'm receiving this error again (on a new computer), I thought it reasonable to reinstall VS SP1 just like I did a while back. However, doing so did not fix the problem.
It appears as though Visual Studio may need another update to support SQL Server 2012 instances.
For clarity, here are the steps that I did to produce the error: (using VS 2010sp1 and SQL2012)
Using VS, create a Windows Forms application
Right-click the project and choose, Add > New Item
Select "Service-based Database"
Cancel out of the "Data Source Configuration Wizard"
A Database will appear under your Project in Solution Explorer. Double-click the new database
The Database will be added to Server Explorer's list of Data Connections
In Server Explorer, open the database, right-click the Tables directory, then select "Add New Table"
The error "This server version is not supported...." is thrown
Screenshots for whatever its worth..
My question is, is there a fix for this? If so, what is it?
Maybe get SQL Server Data Tools, which work with SQL Server 2012? I believe there are some good tutorials and other materials in the Developer Training Kit. I don't think there are any tricks yet to make an older version of Visual Studio / Data Dude compatible with SQL Server 2012.
Following MSDN blog link discusses about the issue that you are facing with local SQL Server database instance in Visual Studio 2010.
Using LocalDB in Visual Studio 2010
My problem was that I couldn't get VS2012 to work right with SQL Server 2012. I installed the latter product first, but after installing VS2012 and attempting to open an existing project, my existing data file just would not work. I know the software is supposed to automatically take you through a wizard to update your database in the project, but that didn't happen, and I couldn't find a way to make it happen. I'm on the point now of getting rid of SQL Server 2012 and going back to 2008. I'd rather use the most current releases, but I can't spend any more time on configuration issues.
I tried reinstalling the SSDT as well, but kept getting the same error you mentioned Jed, this seemed to let me get around the error message:
http://forums.asp.net/t/1721552.aspx/1?Working+with+Denali+SQLEXPRESS+in+VS+11+Database+issue
This error could also occur if you use the wrong provider. A recently came across the "This server version is not supported...." error and after changing the provider from sqlClient Data Provider to SQL Server Native Client 11.0 in the connection string for the Data Source configuration the problem was solved.
For me, the error arises whenever I try creating a new database. So after unsuccessfully attempting all methods advised, I bypassed the error by clicking on "Add connection" and putting my server name in the dialog box. After adding the non-existent database name, it notifies that it can't find such database and asks whether to create it. Voila! Database created (even though server version is supposedly unsupported).

SQL Server Management Studio -> New Query -> "is not a valid win32 application"

I have been using SQL Server Management Studio for years. Yesterday, when I started hitting "new Query", I would get the following error in pop up form:
"is not a valid win32 application".
I did a lot of googling, and I found this:
http://blog.sqlauthority.com/2009/04/30/sql-server-fix-error-is-not-a-valid-win32-application-exception-from-hresult-0x800700c1/
Which is not at all applicable (but I did set all of my application pools in my IIS to allow 32 bit applications on both the server and my client. I'm not sure how that would make any difference and it didn't). I thought maybe it was a problem with VSSHell, but I have been unable to find any valid information to fix it if it is.
I am using Windows 7 64 bit Ultimate.
My colleagues, who connect to the same SQL Server instance, do not have this issue (so it is a problem with my env).
I tried uninstalling and reinstalling.
I tried installing SQL Server 2008 R2 Management Studio (and express version).
I tried installing SQL Server 2005 Management Studio Express.
I even tried the SQL Server 2012 R0 Management Studio. In all, of these, when I click, New Query, I get a pop up that says "is not a valid win32 application".
Please help!
Had the same issue - it turns out one of the MS C Runtime libraries got corrupted somehow. I got the name from the exception details. In my case it was MSVCM80.dll in C:\Windows\winsxs\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.6195_none_d09154e044272b9a, that was 0kB.
Once I replaced it with the actual file, all started working again.
My advice is - try to locate the faulty file that SQL Studio is trying to open (in my case the same error happened when I tried to start SQL Profiler, and I could trace the exception details there).
Good luck

Resources