SQL Server connection using Get-StoredCredential - sql-server

I'm trying to update my script to use Get-StoredCredential for creating a connection to SQL-Server. Below is what I've tried so far without success:
$creds = Get-StoredCredential -Target dbStoredCredentials
$newConnection = New-Object System.Data.SqlClient.SqlConnection
$newConnection.ConnectionString = "Data Source=my-apps-server;Initial Catalog=myDatabase;"
$newConnection.Credential = $creds
This causes the following error:
Exception setting "Credential": "Cannot convert the "System.Management.Automation.PSCredential" value of type
"System.Management.Automation.PSCredential" to type "System.Data.SqlClient.SqlCredential"."
How can I get the SQL connection to authenticate with StoredCredential?

Using a System.Data.SqlClient.SqlCredential means you're using SQL Server Authentication. It's equivalent to putting the username and password in the connection string.
If you want to use a stored Windows credential, then create a Windows Credential targeting YourSQLServer:port (eg myserver.mydomain.com:1433) and use
Integrated Security=true in your connection string. The driver will access the credential store for you and perform NTLM authentication using the stored credential rather than the identity of the thread opening the connection.

Related

Not able to login via specified user in localhost SQL Server connection string

I am new to SQL Server and I am trying to connect a localhost SQL Server. And I am having the following exception:
Cannot open database "MyDatabase" requested by the login. The Login failed Login failed for user "MyCodingPC\Cyborg".
And also I don't understand why it is getting logged in via that username when I have specified another user in the connection string.
My connection string
Data Source=xxx.xxx.xx.xxx,1433\(localdb)\MSSQLLocalDB;Network Library=DBMSSOCN;Initial Catalog=MyDatabase;User Id=MyNewUser;Password=pass##$word;Integrated Security=True
LocalDB cannot be used over TCP. So to accomplish my requirements I used SQLExpress instead.
LocalDB is supposed to work only for local access; remote access is not possible here.

Database connection in ASP Classic

I got an ASP-Classic Page from 2001.
I upgraded/migrated the Database to an actual SQL Server Version (2016).
My problem is, when I try to connect to the database I always get an error message, that I cannot connect to the server because of the given user. I created a user on my SQL Server, this is the one I want to use for the connection but it doesn't work.
Here's the code:
Provider=sqloledb;Data Source=localhost\SQLEXPRESS;Initial Catalog=LV; User ID=lvsupervisor;Password=analogis;
The login credentials are correct. Maybe someone can help me here.
ASP Classic 3 itself works smoothly even with latest version of MS SQL (2019).
My problem is, when I try to connect to the database I always get an error message, that I cannot connect to the server because of the given user.
First of all verify that user itself able to login to MS SQL Server. Run MS SQL Management Studio and try to login to server by using user credential you have. Does it works? Could you see data in Tables?
migrated the Database to an actual SQL Server Version (2016)
Connection string: try different provider, for example Provider=SQLOLEDB.1
Delete your current DSN and create a new one using "SQL Server Authentication" instead of "Windows Authentication". Create a new user with admin/read/write permissions and nominate that account for the new DSN connection. Use a 64-bit DSN for default server or if you have enabled 32-bit applications for the site, then you must use a 32-bit DSN connection. Then you can use...
strConnection = "DSN=ExampleName;DRIVER={SQL Server};UID=ExampleUser;PWD=ExamplePass"
Set dbConnection = server.CreateObject("ADODB.Connection")
dbConnection.ConnectionString = strConnection
dbConnection.Open
SQLFunction = "SELECT ID FROM Example Where CExample.Key = '" & strInputCom & "' "
Set rsFunction = dbConnection.Execute(SQLFunction)
if not rsFunction.EOF then
strClientId = rsFunction("ID").value
else
strClientId = ""
end if
rsFunction.Close
Set rsFunction = Nothing

Cannot connect to SQL Server via VB.net but I can login to the server from SSMS with the same cridentials

I'm working on an application in VB which has to connect to a SQL Server 2014 Express database with the following info:
DBName: testDB
DBHost: USER-PC\SQLEXPRESS
DBUser: testuser
DBPass: testpass
and the following code to make the connection:
Public Shared Sub connect(DBName As String, DBHost As String, DBUser As String, DBPass As String)
Dim builder As SqlConnectionStringBuilder = New SqlConnectionStringBuilder()
builder.DataSource = DBHost
builder.UserID = DBUser
builder.Password = DBPass
builder.InitialCatalog = DBName
connection = New SqlConnection(builder.ConnectionString)
connection.Open()
End Sub
I'm able to login normally with this information (using SSMS) but not with the code here. Now I know that this code works as I've used it on a different machine where it worked fine. In both cases the server is hosted locally. I've enabled all protocols in the Network Configuration but I'm still not able to login via this code. Also I set the login to also work with SQL login.
I'm printing the exception it gives me and it sais
Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'.
Why does it allow me to login via SSMS and not via my program?
Try to vary this row:
Dim builder As SqlConnectionStringBuilder = New
SqlConnectionStringBuilder("Integrated Security=SSPI;")
OR
Dim builder As SqlConnectionStringBuilder = New
SqlConnectionStringBuilder("Integrated Security=true;")
What this means:
Integrated Security = False : User ID and Password are specified in the connection.
Integrated Security = true : the current Windows account credentials are used for authentication. However, be carefully it does not work in all SQL providers. It will throw an exception with OleDb provider.
Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers.
Please, see connection strings at MSDN.
Your error Cannot open database "TestDB" requested by the login. The login failed. Login failed for user 'testuser'. means:
you've written incorrect login or user of database TestDB
user testuser does not exist in your database
Run to see whether testuser exists in your database:
USE YourDatabase
SELECT * FROM sysusers
In addition, be sure that you've set a correct password of database user.
Update by Nick.McDermaid:
I was able to detirmine that I need to grand acess to the database for that user!

Excel VBA connect to SQL Server Login Error

I am a beginner in Excel VBA and faced a problem: I can connect to remote SQL Server through Management Studio, but got error when connecting through Excel VBA.
Below is my connection string and the error code.
Const connection_string As String = "Provider=SQLOLEDB.1;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=RPGTFDB;Data Source=122.xxx.xxx.xxx;User ID=sa;Password=xxx"
Error is:
Run-time error (80004005): Login failed for user ". The user is not associated with a trusted SQL Server connection.
Is it not able to get the User ID?
However when I connect to local computer, like 127.0.0.1, the above connection string works.
Thanks a lot!
In your connection string you should not use
User ID=sa;Password=xxx
As the error seems to be indicating that only Windows Logins are enabled on the SQL Server and this connection string item is only for SQL Logins.
Do you have to punch in a login and password when connecting with management studio? If not then I suggest you have a database that authenticates a connection using your windows login.
If this is the case, then remove the User ID and Password bits and insert Trusted_Connection=Yes instead.
Finally, the connection string is created using UDL application.
Despite I still cannot find any difference between the string and the manual created one, it works!
Follow the instructions in this link:
http://social.technet.microsoft.com/wiki/contents/articles/1409.how-to-create-a-sql-connection-string-for-an-application-udl-file.aspx

Connecting to SQL Server over network using ADO from Excel VBA

I need to connect to a SQL Server DB running on my laptop from another computer on a network. I am doing this both from a C# app as well as in a VBA macro. Both run perfectly on my laptop and the C# app runs perfectly over the network. However I cannot connect using VBA over the network. This is my connection string:
ConnectionString = "Driver={SQL Server};Server=MY-LAPTOP; DAtabase=SAFEXlive; UID = MyUsername; PWD=MyPassword"
Aside from the 'Driver={SQL Server}' this is the same as the connection string I am using in the C# app which works.
I am then using the following code (With a reference to Microsoft ActiveX Data Objects 6.0 Library in VBE) to open the connection:
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionString
oConnection.Open
This works correctly if I run it on my laptop but if I run it on another computer on the network I get the error: "Run-time error '-2147217843 (80040e4d) [Microsoft][ODBC Server Driver][SQL Server]Login failed for user..." and the user it specifies it the windows log in for the computer.
Are there some security settings I need to set in code or in excel? Or am I constructing the connection string incorrectly? What am I doing wrong?
Solved. The answer is rather infuriating. The problem is in fact with the connection string, with the UID. Believe it or not changing ...UID= MyUsername;... to ..UID=MyUsername;..., i.e. removing the space character, was all it took! Thanks for suggestions though.
Try this Connection string,
ConnectionString = "Provider=SQLOLEDB;Data Source=MY-LAPTOP;Initial Catalog=SAFEXlive;User ID=MyUsername;Password=MyPassword"
Is this an AD Domain login? Make sure you have appended the domain to the username e.g, domain\user .
I suggest using integrated security instead of this.

Resources