Connecting to server vs connecting to database - c#

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.

Related

The Sql Database is not deleted

I'm getting an error deleting a database I created about a year ago. in the error, when I run these codes, it says:
USE MASTER;
GO
DROP DATABASE StoreDatabase;
GO
Cannot drop the database 'StoreDatabase', because it does not exist or you do not have permission.
when I try to manually delete it manually
Right-click => Delete => Delete Backup and restore history information for database and Close Existing connections
in the error message it says:
The object of type "Database" named "StoreDatabase" does not exist on the server (SqlManagerUI)
how can I solve this error. thank you for your help.
The Sql Database is not deleted
You do not delete a database but DROP it
Cannot drop the database 'StoreDatabase', because it does not exist or you do not have permission.
Error includes two parts: (1) database does not exist or (2) you do not have permission. Please connect the server using SSMS or Azure Data Studio
(1) Use SSMS and execute the following query in order to confirm that the database exists
select DatabaseName = [name] FROM sys.databases
where [name] = 'StoreDatabase'
GO
(2) In order to DROP a database you need to have CONTROL permission on the database, or ALTER ANY DATABASE permission on the server, or to be membership in the db_owner fixed database role. Execute the following query to check that you have the permissions
USE StoreDatabase;
SELECT * FROM fn_my_permissions (NULL, 'DATABASE')
where permission_name = 'CONTROL'
GO
SELECT * FROM fn_my_permissions(NULL, 'SERVER')
where permission_name = 'ALTER ANY DATABASE'
GO
USE master;
select DatabaseName = [name], DatabaseOwner = suser_sname(owner_sid)
FROM sys.databases
where [name] = 'StoreDatabase'
GO
Try to DROP the database when you are connected as sysadmin (for example the USER sa)

How Can I join Multiple database SQL query in POWERBI

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.

Total sql connection consuming by application

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

How can I access all databases in SQL Server Management Studio?

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

How to check if a database exists?

I want to check if a database is working as a "Publisher" to other databases.
To do this I was planing on checking if the database "distribution" exists on that instance.
after reading this I thought I could just do
new Sqlcommand("SELECT name FROM master.dbo.sysdatabases WHERE name = #name")
and solve my problem...but I dont have that table in my database...:s
is there another way to solve my problem?
If you want to know if a database is a publisher then looking for a distributor is the wrong check. A database can have a remote distributor, in which case you'll get a false negative. Or the distributor may exist but the database may not be a publisher, in which case you get a false positive. Not to mention that the distribution DB may have any name, so looking for a database named distribution is also wrong.
The proper way to do it is to sue the built in replication helper procedures:
exec sp_helppublication will return information about all publications in a database. IF the database is not a publisher, it won't return anything (yoru cue to action).
exec sp_helpdistributor will return information about the distributor of a publisher
exec sp_helpdistributiondb will return information about a distribution database
In addition, the simple facts whether the DB is a publisher, subscriber or distributor can be discovered in sys.databases:
is_published Database is a publication database in a
transactional or snapshot replication topology.
is_merge_published Database is a publication database in a merge replication topology.
is_subscribed Database is a subscription database in a
replication topology.
is_distributor Database is the distribution
database for a replication topology.
Assuming you have sufficient permissions to view database metadata you can use
SELECT CASE
WHEN DB_ID('distribution') IS NULL THEN 0
ELSE 1
END AS distributionExists
select *
from sys.databases
where name = #name
Very close. It looks like that is SQL Server 2000 catalog view. What you are looking for is querying sys.databases.

Categories