I have multiple C# applications and all applications use the same database(SQL server 2014) and same credentials(Same connection string). All application run on the same server.
Now, my question is anyhow can I get the total number of SQL connections consuming(current open connection) by particular application right now?
I.e
1. 3 connections open in Application1
2. 2 connections open in Application2
I tried using "App Name" in connection string but I don't know how to get total connection consuming by "App Name"?
Query the Dynamic Management Views:
SELECT
COUNT(*),
program_name
FROM
sys.dm_exec_connections cn
LEFT JOIN
sys.dm_exec_sessions sn
ON
sn.session_id = cn.session_id
GROUP BY
program_name
I also found another sql query to get open connection application wise.
SELECT count(*),program_name
FROM master.dbo.sysprocesses sp
group by program_name
Related
When I get data from SQL Server ,
It asks me database(optional) but I cant skip it.
example query below
select
*
from example1.dbo.table1 CP
inner join example2.dbo.table2 CC on CC.exampleId=CP.Id
inner join example3.dbo.table3 CT on CC.Id=CT.exampleId
where B.Id='" & Id & "' and CP.Id in
(
select xId from example1.dbo.table1
where Id='" & Id & "'
)
So I need to join 3 tables from 3 database.
BTW I know server and I have admin account.
With a single admin account I can connect that 3 database.
I tried import mode,
It asks me database.
When I write one of that 3 database name into that place,
Details: "Microsoft SQL: The target principal name is incorrect. Cannot generate SSPI context.
How can I solve that problem.
You can find additional details on that specific error here
When you connect to the SQL Database make sure you are selecting the correct authentication mechanism. Windows is for your network login and password. Database is for a SQL username and password and Microsoft account is for an Azure AD account (e.g. what you would use to log in to Exchange Online or Office 365).
I used OLE DB mode Instead of SQL Server - Import to solve the problem.
I'm working on a query editor in which user enters a SQL query and in code behind I pass this query to a SqlCommand and execute it and display result to the user.
But there is one problem: how can I access all the databases in SQL Server which the user created? How can I set initial catalog= to access all databases in SQL Server, so that user enters any query, then it will be execute against all those databases.
For example:
use db_compiler
select * from std
use student
select * from student
So I'm going to say this - what you are requesting to do is a fundamentally BAD idea. SQL Injection is a concern among many, many other things.
However, if you want a list of the databases to set initial catalog, check out the answer to this question:
SQL Server query to find all current database names
you don't require to set initial catalog in order to be able to access to other databases.
Ability to access other database is determine by the permission of the login. If the login is able has the permission to access to other database, you can you use the 3 part naming convention to access it.
Example, even if the initial catalgo is DB1, it will be to access the TABLE3 in DB2
SELECT *
FROM [DB2].[SCHEMA].[TABLE3]
For example instead of
use db_compiler
select * from std
you can
select *
from db_compiler.dbo.std
I am getting a timeout when executing an SQL SELECT.
I connect to the database thus:
using (SQLConnection conn = SQLConnection(#"Data Source=mydbServer;Initial Catalog=mydb;Integrated Security=true;Timeout=180");
{
conn.Open();
and that successfully connects.
Inside the using{} I set:
string query = #"SELECT a.field, a.timestamp " +
"FROM mydb.dbo.myTable1 a WITH(NOLOCK) " +
"LEFT JOIN [myOtherdbServer].myOtherdb.dbo.MyTable2 b WITH(NOLOCK) " +
"ON a.field = b.field " +
"WHERE b.field is NULL " +
"AND a.timestamp >= '2015-05-01 00:00:00.000' " +
"AND a.timestamp < '2015-06-01 00:00.00.000'";
and execute the command thus:
using (SQLCommand queryCmd = new SQLCommand(query, conn)
{
queryCmd.CommandTimeout = 300;
using (SQLDataReader rdr = queryCmd.ExecuteReader())
{
The SQLCommand throws a timeout exception: "The timeout period elapsed prior to the completion of the operation or the server is not responding".
If I use SQL Server Management Studio on the same system my program is running on, and as the same user under which my program is running, and execute the same SELECT, it completes in under 1 second and returns several thousand rows.
This is happening consistently now. It was working a couple of days ago. What should I be looking for? I'm baffled because the same SELECT works from SQL Server Management Studio. Does SQL SMS use a different connection?
"If I use SQL Server Management Studio on the same system my program is running on, and as the same user under which my program is running, and execute the same SELECT, it completes in under 1 second and returns several thousand rows."
There can be several reasons to why the execution time in SQL server management studio is better. For once, results are Cached, it may also very well be that you are lacking indexes on the timestamp column
In addition, is your application server located on the same server as you sql server? If not, this may increase latency and cause timeouts.
Linked servers may be an issue, except for latency considerations, I'm not sure the NOLOCK statement is sufficient to ensure what youre trying to achieve on the remote server. There's a good chance you may need to create a view on the remote server that contains the NOLOCK statement.
This is happening consistently now. It was working a couple of days ago. What should I be looking for? I'm baffled because the same SELECT works from SQL Server Management Studio. Does SQL SMS use a different connection?
Usually when something was working and now it has stopped working, then something was changed. Start troubleshooting until you find what it is, look for index changes, code architecture modifications, even windows updates, anything which may give you a lead, until you are able to restore it.
Additional advice,
try limiting the select statement to TOP 10, just to see you are able to get back results, this may indicate the issue is in the object's size / query execution time, and not with your server / application configuration.
What's the difference between connecting to a server as opposed to connecting to a database?
The context of the question is that I'm in charge of developing a proof of concept where a user can select one of our servers, a database within that server, a table within that database, and a column within that table. I am using Visual C# and ASP.NET. I believe I can get the servers from the connection strings in the web.config, but I'm not quite sure how.
If it helps at all (I do like examples), you can assume SQL servers.
(Answer to original question)
There is a hierarchy:
Server: A piece of physical (or virtual) hardware that runs an OS and applications. You will address it via IP address or DNS name, in can host multiple Database Servers
Database Server (aka Instance): A piece of software that runs that can host multiple Databases. when you use a connection string it is in the format "ServerName\InstanceName"
Database: A data structure that can host multiple Data Tables
Data Table: A data structure that can host multiple Columns and Rows
Column: The smallest division of information seperation, holds information about a specific topic
Row: Holds a single set of columns.
(Answer to updated question)
It is different per SQL provider, but using Microsleft SQL server you just connect to the server (don't provide a default instance in the connection string) and do the following:
select * from sys.databases
Once you have your database, connect to that database and do the following to get the tables
select * from sys.tables where type = 'U'
to get the columns you do
select * from sys.Columns
however to get the name of the table the column is in you need to match Object_id to Object_id on sys.tables
select t.name as TableName, c.Name as ColumnName
from sys.tables t
inner join sys.columns c on t.object_id = c.object_id
where t.Type = 'U'
You can achieve your goal. Initially, you would connect to database master on the server and query the databases on that server.
SELECT * FROM sys.databases
Then, you would initiate a new connection to the selected database and query that database's information schema, to get a list of tables.
SELECT * FROM INFORMATION_SCHEMA.TABLES
Repeat for selecting a column.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'foo'
If by server you mean database server, you would connect to the server to gain access to the database hosted on that server.
I have a SQL Server database located at http://192.168.10.3/MyDB. I have created a C# MVC application, and I need to know the steps to connect my application to the above database.
Is it only replacing the connection string in the web.config file ?
Data Source=?? ;Initial Catalog=??;Integrated Security=SSPI;
User ID=??;Password=pwd;
If so what am I to replace where I have placed the ?? sign ?
DataSource = 192.168.10.3
Initial Catalog = MyDB
User ID = whatever sql login you are using to access your SQL Server
Password = password for the sql login above
The other answers here are good. In addition, ConnectionStrings.com can be your friend, especially if you are going to connect to various types of databases in the future. Select the database that you need to connect to and then you'll see the different connection strings you can use for that database.
http://connectionstrings.com/sql-server-2012#sqlconnection
you can try this
create a new text document on your desktop - conn.txt
change file extension to udl (conn.udl)
double click to open the file in the first tab select appropriate provider
4 . in the second tab enter server name (ip address,port), username, password (check Allow saving password) and database name.
test connection
if the test reports success close the window.
open the file with notepad, copy everything but the provider name and paste it back to connectionString
Below is connection string you need for:
Data Source="192.168.10.3" ;Initial Catalog=MyDb;Integrated Security=SSPI;
User ID=sa (for example);Password=whatever you set before;