SQL Server Profiler showing EF queries against master database? - sql-server

What am I missing here? The queries I see in SQL Server Profiler are all targeted against the master database, which makes it difficult to filter by database name ... which event or events should I be watching so I can filter by database name.
The bigger question, what exactly is going on here?

You should remove this 'MultipleActiveResultSets=True' from your EntityFramework connection
string
after that, you can see the target database name show in the Profiler , instead of master.
In my option, maybe ADO.NET team want to make use the MultipleActiveResultSets feature to get
data from DB, so they have to access master.
MultipleActiveResultSets is about raise one query and don't return all its result (like in foreach statement in LINQ) , and in the same time ,raise another query to get another data in the same session.
By default, this behavior is not allowed by DB. SO.....

I was able to get around this issue, including leaving MARS active by adding an application name to my connection string:
Data Source=database_server;Initial Catalog=MyDatabase;Trusted Connection=true;MultipleActiveResultSets=True;Application Name=MyDatabase;
Then you can filter on application name.

If this is for SQL Server 2008 R2, in your trace properties, on the 'Events Selection' tab, check 'Show all columns'. You should then be able to create a column filter based on DatabaseName.
I believe you'll have to pause or stop your trace to make these changes.

As K Ivanov pointed out, having MARS (MultipleActiveResultSets) enabled will show the DatabaseName as master in SQL Profiler. By setting this to false, it'll show the proper DatabaseName, but then you lose the ability to have MultipleActiveResults.

you can use LoginName or HostName to filter in the profiler

For some reason, if I select the SP:CacheHit event, it now shows the queries against the correct database and I am able to filter by it. What is that event exactly?

Related

View Connection String Information inside MS Access

I have been given a task, which is the eventual re-write, but in the meantime, I need to document all that is going on.
We have an Access database that doesn't actually store any data. The Access database is simply the UI (MS Access Forms) that a user uses and the data is actually maintained in a SQL Server database. One thing I cannot seem to find is: the connection string used for MS Access to connect to the SQL Server. I need to find what database / server is used to store the information, but cannot seem to figure this out, nor has Google been able to give me the answers. Would anyone be able to help?
Open the Immediate Window (Ctrl+G)
? CurrentDb.TableDefs("a_linked_table").Connect
will give the connect string.
Or open a table in design view and open the properties.
you can run this query:
SELECT * FROM msysobjects WHERE connect <> '';
the result is the list of the objects with a connection string that is not empty.

creating view in sqlserver

I trying to create view linking 2 tables admins and news
create view v_news as
SELECT [n_id]
,[n_title]
,[n_detail]
,[n_date]
,[n_sdate]
,[n_edate]
,[n_admin]
,[a_name]
,[a_email]
,[a_role]
,[a_status]
FROM hed2.dbo.hed_news,hed2.dbo.hed_admins
where hed_admins.a_id=hed_news.n_admin
This message is displayed:
Command(s) completed successfully.
but there is no view in VIEWS folder.
When I try to run the same query again then it says:
There is already an object named 'v_news' in the database.
I am connected with windows authentication
I tried reconnect and restart sql server but ....
Your view is using ANSI 92 syntax. While this will work for older database versions, it will not work in SQL Server 2012. See Mike Walsh's blog on this topic.
1 - When using SSMS views do not show up right away. Right click and hit refresh.
2 - It is very important to make sure you are in the correct database. I am sure many people, including me, have create an object or two in master. This is the default for a new login.
This can be changed by changing the default database for your login.
3 - Execute the USE command to change the database context (default).
The snippet below is a SQL Server 2012 compliant version.
USE [hed2]
GO
create view v_news as
SELECT [n_id]
,[n_title]
,[n_detail]
,[n_date]
,[n_sdate]
,[n_edate]
,[n_admin]
,[a_name]
,[a_email]
,[a_role]
,[a_status]
FROM dbo.hed_news JOIN dbo.hed_admins ON hed_news.n_admin = hed_admins.a_id
GO
Three things:
You must use JOINS and the way your query is written is bad. Just an observation. Nothing to do with your question.
Did you try running SELECT * FROM v_news? That would return results.
Right click and views in SSMS and select refresh. It will show up in the list
Raj

Find Which applications access my server using Profiler Trace with Application Name column

I Need to find out what are all the applications that use my sql server.
I'm using Profiler trace to do this (if there's another way to do this I would appreciate it)
On Profiler I'm using a Replay template, and after looking at the trace result I see that there's a column called Application Name, I'm wondering if there's a way to get the distinct ones (the trace is on a .trc file).
(By the way is this supposed to be posted on stackoverflow or serverfault?)
Thanks,
Gabriel
Try this:
SELECT DISTINCT ApplicationName
FROM ::fn_trace_gettable('C:\YourFolder\YourTraceFile.trc', DEFAULT) t
You can actually do this right from within Profiler in SQL Server 2008.
Create a trace with the following two events:
Security Audit : Audit Login
Security Audit : Existing Connection
For those two events, capture the following columns:
Event Class
Application Name
SPID (required)
Event Sub Class
Add a filter to Event Subclass to restrict it to values of 1. This filter will only capture non-pooled logins. This should give you all your existing connections and any new logins that occur during the time you are running your trace.
Next, in the organize columns, move Application Name up to the "Groups" section. This will now group all the results by the Application Name.
This is a pretty light weight trace and shouldn't put much (if any) load on the server if you restrict it to just those events and apply the filter.
(I'm pretty sure previous versions work the same way. I just don't have one in front of me to test.)

What is the point of "Initial Catalog" in a SQL Server connection string?

Every SQL Server connection string I ever see looks something like this:
Data Source=MyLocalSqlServerInstance;Initial Catalog=My Nifty Database;
Integrated Security=SSPI;
Do I need the Initial Catalog setting? (Apparently not, since the app I'm working on appears to work without it.)
Well, then, what's it for?
If the user name that is in the connection string has access to more then one database you have to specify the database you want the connection string to connect to. If your user has only one database available then you are correct that it doesn't matter. But it is good practice to put this in your connection string.
This is the initial database of the data source when you connect.
Edited for clarity:
If you have multiple databases in your SQL Server instance and you don't want to use the default database, you need some way to specify which one you are going to use.
Setting an Initial Catalog allows you to set the database that queries run on that connection will use by default. If you do not set this for a connection to a server in which multiple databases are present, in many cases you will be required to have a USE statement in every query in order to explicitly declare which database you are trying to run the query on. The Initial Catalog setting is a good way of explicitly declaring a default database.

Grouping sys.dm_exec_connections by database (it's not quite like sys.sysprocesses)

Following on from my last question:
I've written some code to upgrade a SQL Server database. Before I upgrade the database, I plan to limit access to the database with the following statement:
ALTER DATABASE Test SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Before running this code, I'll give the user an opportunity to opt out. At the time of prompting the user, I thought it would be nice to show the list of active connections (continuously polled at a set interval); providing the user with a tool to identify applications/users they would like to boot off the server before proceeding.
In SQL 2000, you can use the sys.sysprocesses table to see all connections that apply to a database. This includes connections that have no active request (like when you open a Query Analyser window and select a database).
However, using:
sys.dm_exec_connections
sys.dm_exec_sessions; and
sys.dm_exec_requests
I couldn't figure out a way to achieve the same outcome. It appears that these views only tie connections to a database through a request. Is there a way to mimic the behaviour of sys.sysprocesses? I'd prefer not to use this table for SQL Server 2005/2008 databases.
Er... I recommended these for your other question.
Sorry, but, I've found out that you still have to use sysprocesses
It's logged as a bug in Microsoft Connect 144515 to be fixed, I found it here
Personally, I still use sysprocesses because I'm comfortable with it, however lazy and luddite that may be...

Resources