Linq to SQL Connection String for Sql Auth - c#

tI'm using Sql Server with Windows Authentication enabled. Recently I added a dropdown to allow for different connections. One of them uses SQL Authentication.
I consulted msdn and it seems all I need to do is add a Username and Password entry to my string.
http://msdn.microsoft.com/en-us/library/aa905872(v=sql.80).aspx
My windows authentication string :
Data Source=PC\Slike;Initial Catalog=db_slike_articles;Integrated Security=True
The second string I added for SQL Authentication :
Data Source=Data Source=PC\Dev;Initial Catalog=db_dev_articles;Integrated Security=True;User ID=root;Password=rootpwd
For some reason when I choose the 2nd SQL connection it's throwing an SqlException that login failed with my PC\Slike username. The connection string is the second one with the User ID and Pwd, I don't understand why it's trying to connect against my local account.
The second string is located on a remote machine, is there something I have to enable to switch from Windows Auth to Sql Auth or does specifying the the credentials within the string is enough?
Thanks

when using SQL Authentication, remove this from the connection string:
Integrated Security=True;
there is a web site listing all possible/supported syntax of connection strings for all database engines, I can't find it now, connectionstrings.com does not load properly.

Related

SQL Connection String appsettings.json

This is my connection string
{
"Data": {
"PhotoGalleryConnection": {
"ConnectionString": "Server=WINDOWS-B7MJR5T\\SQLEXPRESS;User Id=sa;password=allah;Database=PhotoGallery;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
And I am facing this exception
An exception of type 'System.Data.SqlClient.SqlException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: Cannot open database "PhotoGallery" requested
by the login. The login failed.
It should work after You delete Trusted_Connection=True; from connection string.
If you specify either Trusted_Connection=True; or Integrated
Security=SSPI; or Integrated Security=true; in your connection string
THEN (and only then) you have Windows Authentication happening. Any
user id= setting in the connection string will be ignored. Found in here: When using Trusted_Connection=true and SQL Server authentication, will this effect performance?
Please search services in the window menu, it will show you the local services then go to SQL server services and refresh all, then go to the SQL server management studio and by using SQL authentication please provide username and password, you will be able to use the SQL server. for appsetting.json update, trusted_connection is used for window authentication not for SQL server authentication by using username and password

Why I cant connect to the database by connection string?

I have a database on a local network.
I can connect to the database in SSMS:
But when I want to connect to the database by this connection string in c#:
"Data Source=192.168.0.3,14330;Network Library=DBMSSOCN;Initial Catalog=master;Integrated Security=True;User ID=sa;Password=123456789;"
I get the following error:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
Your are telling sql server to use integrated security, ie windows security, however you trying to use a username and password
turn integrated security off, see the below example
"Data Source=192.168.0.3,14330;Network Library=DBMSSOCN;Initial Catalog=master;Integrated Security=False;User ID=sa;Password=123456789;"
Pertinent information
If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
You can find more information on connection strings here, SqlConnection.ConnectionString Property
Update
maybe this question may help you more How to get the connection String from a database
Update 2
For more in depth troubleshooting, there is a great resource here How to Troubleshoot Connecting to the SQL Server Database Engine

After deployment of win app with SQL Server Express database I got error as "cannot open database login failed"

I made a win forms app with SQL Server Express with Visual Studio 2010.
When I deploy my app by right click on Solution Explorer selecting Properties and then Publish and following all the procedure.
When I install on other system, error occured. The error was:
cannot open database [dbname] login requested by user [username] failed...
I googled much but no use. I think it might be in connectionstring, if it is so, what and how should be connectionstring or any other alternative.
If you have Integrated Security = true in your connection string you will have to provide a sql server login and a database user in SQL Server for your users. Alternatively, you can set up a specific SQL Server login and database user for your app, grant it least-privaledged permissions in SQL Server, and then specify it in your connection string as user=myuser;password=mypassword;
Then remove the integrated security = true portion altogether.
1st- check if your connection string is correct and make it specific like dont use dot. try to use IP address on your server name(dont forget to include \SQLexpress if needed). also if u are using windows authentication(without user id and password) you need to add on the connection string: "integrated security=SSPI"
2nd- off firewall. firewall blocks all remote connection
3rd- check hardware, ping the server and make sure the client can see the server

Connection string works for OLE DB connection but not Sql Server Native Client 10.0

The following is a connection string generated when I connect to a database using a configuration tool with Microsoft OLE DB Provider for SQL Server.
Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=sa;
Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Use Encryption for Data=False;Tag with column collation when possible=False
If I connect to the same database but with SQL Server Native Client 10.0 I get this connection string.
Provider=SQLNCLI10.1;Integrated Security=\"\";Persist Security Info=False;
User ID=sa;Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Initial File Name=\"\";Use Encryption for Data=False;
Tag with column collation when possible=False;
MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False\0
I have a c# application that reads either one of these connection strings and uses it to create a connection to my database like so
SqlConnectionStringBuilder _sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
OleDbConnectionStringBuilder conBuilder = new OleDbConnectionStringBuilder( CONNECTION STRING SHOWN ABOVE);
_initialCatalogValue = (string)conBuilder["Initial Catalog"];
_dataSourceValue = conBuilder.DataSource;
_sqlConnectionStringBuilder.Password = (string)conBuilder["Password"];
_sqlConnectionStringBuilder.UserID = (string)conBuilder["User Id"];
_sqlConnectionStringBuilder.InitialCatalog = _initialCatalogValue;
_sqlConnectionStringBuilder.DataSource = _dataSourceValue;
_sqlConnectionStringBuilder.PersistSecurityInfo = true;
_conn = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
_conn.Open();
The problem that when I use the SQL Server native client the password is empty and my SQLConnection will fail on the login. The error I get is "Login failed for user 'sa'".
The OLE DB connection string is successful. Is my login failing for the sql server native client due to some of the classes I am using in c#? Does the Sql Server Native Client 10.0 encrypt the password? Should I try to identify which provider is in the connection string and have two different code paths? If so what would it take to connect?
Basic question is, how can I ensure a successful connection regardless of which connection string I receive (only the two above)?
N.B. I have no control over the connection strings. I can only work with what I am receiving.
The second connection string you provide does not include a password; thus conBuilder["Password"] returns an empty string when you set conBuilder.ConnectionString to the second string.
The problem was solved as follows.
There was a main application that was making use of the above connection strings and was doing the following.
The application would take the connection string.
If the connection string's provider is SQL Server Native Client (SQLNCLI10.1) the application checks for persistent security. If it cannot find any it adds IntegratedSecurity=SSPI to the connection string and then connects using windows authentication instead. Whether or not this is the right (or secure thing to do) that is what was being done.
To 'answer' the question. You can take in the connection string and if you do not find a password you can set IntegratedSecurity=SSPI. This will allow you to connect using windows authentication instead of SQL Server authentication. I am not advising that you do, but it will work.

SQL Server: Login failed (for win user, instead the specified one)

While connecting to a remote SQL Server 2005 from my C# code I get a login error (Login failed for user X) This 'user X' appears to be the windows user, instead of the one I specified in my connection string, that was a SQL Server user.
Anyone knows the problem here? The server is in another machine, and I cannot make changes on it, but I can check its configuration.
The connection string:
server=XXX; database=XXX; user id=XXX; password=XXX; Trusted_Connection=yes;
connection timeout=30
Thanks,
Trusted_Connection=yes; has the effect of using the Windows user instead of the credentials specified in the connection string. Try removing Trusted_Connection and it should work better.

Categories