Why I cant connect to the database by connection string? - c#

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

Related

SqlConnection works with "Integrated Security =True" but I cannot connect with my login details

I have some working code that is currently sitting on our test dev server that connects to an SQL server using Integrated Security/Trusted_Connection at the moment.
I will need to deploy this to a clients server and it will be scheduled to run every hour regardless of if a user is logged on or not.
as the user may not be logged in I assume that the Integrated security will not work.
The credentials I am putting in are the same as those I use to connect to the dev server. I have tried both with the domain "organisation\username" however this does not work either.
SqlConnection myConnection = new SqlConnection("User id=username;" + "Password=xxxxxx; + "Data Source=datasrc;" + "Initial Catalog=catalog; " + "connection timeout=30");
myConnection.Open();
The above code does not work and results in the program 'hanging' before running myConnection.Open()
However putting "Integrated Security=True;" and removing the "User id" and "Password" sections of the string works.
When I try to use "User id=name;" + "Password=xxxxxx;" it does not work and just hangs.
Below is an error message from trying to login with username and password (windows authentication works fine) on the SQL Server 2014 Management Studio
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
The network path was not found"
Integrated Security=True; means that the current credential being used to run the application pool will also be used to login to the SQL database. It doesn't make sense to use Integrated Security=True; and a User ID + Password. One or the other.
So, if your IIS instance is using an application pool logged in by User123, and the .NET application sitting on that IIS instance connects to a SQL database, then that .NET application will attempt to login to the SQL database using User123, so long as Integrated Security=True;. Typically people do this to ensure no plaintext passwords are present in the web.config, for security reasons. EDIT: you can see which user the application pool is using by right clicking the application pool in IIS manager and going to 'Advanced Settings'.
If it only works when you have Integrated Security=True; then that means only that user has permissions to access the SQL database. I would open up SSMS, navigate to the SQL database, and review the Logins under the Security navigation tree. Keep in mind there is a Login for the database server, as well as for the database itself, so you may have to configure this in two places.

Provide login information to connection string?

Trying to build connection string to connect to SQL Server that runs on the same PC. I use Windows Authentication mode in order to connect by using SQL Server Management Studio.
Database name: [dbo].[LearnCSharp]
App.config connection strings section element:
<add name="LearnCSharpConn"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=LearnCSharp;"/>
I get this exception during connection:
Cannot open database "LearnCSharp" requested by the login. The login failed. Login failed for user 'GM\Iam'.
What is wrong with my connection string? Should I provide password there?
Your connection string looks like a LocalDB connection string, but you are saying to be connecting to a SQL Server instance.
In order to use SQL Server, you need a connection string like this:
Server=.;Database=LearnCSharp;Integrated Security=True;
The . in the Server denotes 'current machine, default instance', which I presume you are using. The Integrated Security=True; indicates to use Windows authentication.
If you are using LocalDB, it should look like this:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\YourDatabaseFile.mdf;Integrated Security=True
Replace YourDatabaseFile.mdf by your actual database file name.

Unable to connect To the SQL server using the given connection String (using C# windows Application)

I am helping my friend in his college final year project, Its an C# application in a client machine and it uses SQL server in the windows server 2008 environment.
VM ware environment:
3 clients(windows 7) and 1 SQL database (in windows 2008)
The connection between he server and the client is working, the port 1433 for SQL is open and its listening.
When I try to run the program am getting the following error.
This is the connection string which am using.
cn = new SqlConnection(#"Data Source=192.168.1.2,1433;Initial
Catalog=Test;Integrated Security=True");
And i have also tried to refer this website for connection strings, but i don't know how it works.
http://www.connectionstrings.com/sql-server-2008
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN; Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;
I am getting an error. Tried to give a simple username and password in the SQL server and tried it, still the same issue. please check the image. The Username and password which i used is the System login(windows authentication)
I just want to access the SQL server in the Windows 2008 machine.
Where should i check the credentials for this connection string ?
Thank you soo much for reading this patiently.
You can go to Connectionstrings.com to get more information on connection strings and its usages.
Try removing the port from your connection string (1433 is the default SQL port):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=True");
For a connection using sql authentication (which would eliminate trust issues between machines) use the following (replacing xxxx and yyyy with the appropriate user name and password):
cn = new SqlConnection(#"Data Source=192.168.1.2;Initial Catalog=Test;Integrated Security=False; User=xxxx; Password=yyyy");

How to access SQL Server 2012 on EC2 server from my ASP.NET website published on the same EC2 server?

My site is not able to establish a connection to the SQL Server 2012 from EC2. My connection string is as follows:
Data Source=AMAZONA-2SSRDM3;Initial Catalog=MyDatabase;Integrated Security=SSPI;
As you can see I am using Windows Authentication; is there something wrong with this approach I am taking? I am new to Ec2
Please help
The New Connection string i am using is
connectionString="Data Source=http://ec2-46-137-228-121.ap-southeast-1.compute.amazonaws.com/;Initial Catalog=MyDb;User ID=something;Password=something"
More Info : I just restored a new database on to my Ec2 Machine . My sql server instance was using only Windows Authentication which i changed to Mixed Mode. I created a new Login something and made it the Db Owner of the Restored Database as well
Properties of the Login User
You should authenticate using user name and password, not integrated security. Your connection string should be like this:
Data Source=AMAZONA-2SSRDM3;Initial Catalog=MyDatabase;User Name=TheUserNameAmazonGaveYou;Password=MyPassword
connectionString="Data Source=localhost;Initial Catalog=MyDb;User ID=something;Password=something"
Just Had to make this Connection String Data Source as localhost it worked fine then

Linq to SQL Connection String for Sql Auth

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.

Categories