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.
Related
I have an ASP.NET MVC app hosted on Azure. I use OpenIdConnect/Owin and Azure AD Integrated to authenticate. I have an error log table with a UserId column that only displays the SA username. I also have tables with CreatedBy/ModifiedBy columns that only display the SA username. These records are created via stored procedures.
I already granted execute access to my user account, but the columns continue to be set to the SA username. The only thing that I can think of is a connection string that is causing the value to only be the SA username.
How can I get the stored procedures to run as the user that created/updated the records to set the UserId, CreatedBy and ModifiedBy columns to its username?
UPDATE: I have narrowed down the reason for this. When I deploy the application to Azure, under Publish -> Settings -> Databases, connection strings are entered for the DbContexts. Currently, I have one as Data Source=tcp:servername.database.windows.net,1433;Initial Catalog=dbname;User ID=myusername;Password=StrongPassword123;Encrypt=True;TrustServerCertificate=False
I previously tried the following: Data Source=tcp:servername.database.windows.net,1433;Initial Catalog=dbname;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=Active Directory Integrated;, but got the error "The format of the specified domain name is invalid."
How can I get the stored procedures to run as the user that
created/updated the records to set the UserId, CreatedBy and
ModifiedBy columns to its username?
You don't. You pass those values into the stored procedure. Think of it this way: do you think csharpdev has write permissions on the Stack Overflow database? Of course not; the back end code passes your user id into the sproc that creates the record.
I am looking to programmatically lock an account on a database server. I have hundreds of them to do so I need a programmatic solution.
I am running the following 2 commands
string query = "ALTER LOGIN [" + itemJson.Account + "] DISABLE;";
query = "USE MASTER DENY CONNECT SQL TO [" + itemJson.Account + "];";
which turns into
ALTER LOGIN [TestAccount] DISABLE;
USE MASTER DENY CONNECT SQL TO [TestAccount];
and it works great.
However if I change the commands to
ALTER LOGIN [domain\TestAccount] DISABLE;
USE MASTER DENY CONNECT SQL TO [domain\TestAccount];
The process fails with
Cannot alter the login 'domain\TestAccount', because it does not exist or you do not have permission.
If I run the same commands in SSMS running as the same account as the application it works perfectly fine.
Any thoughts or recommendations?
OK, just to answer what was going on.
I was keeping json in my database that contained the value.
{"Instance":"Server","Database":"Database","Account":"Domain\TestAccount"}
When I brought this into the application I was using Newtonsoft to convert the json into an object. Unfortunately, the conversion saw \t and turned account into "Domain estAccount".
I reset the json to Domain\TestAccount and it fixed the problem.
Thanks for your help.
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
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
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.