SqlServer2012-Error 40-Could not open a connection in SQL Server - c#

I know this is an answered question , however I have tried all the steps mentioned
like:
In SQL Configuration manager have Enabled TCP/IP,Shared Memory and Named Pipes.
Restarted the SQLSERVER
In Services.msc I have restarted SQLSERVER and SQL Server Agent
Added port 1433 in firewall's Inbound rule
Restarted PC number of times
Even Registered Local Server in SQL Server Management tools
I can open SQL Server Management tools
It was working fine and now its not. However I am able to login , then I cannot do any work on it, it gives me an error as :
Tried all the steps mentioned in the accepted answer HERE
I have some other application accessing same SQL Server and they are working fine.
My Connection string :
public SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Database=RMS_EXPRESS;Integrated Security=True;User Id=sa;Password=xxxxxx");
I also tried with the following connection string
Data Source=.;Database=RMS_TS1;Integrated Security=False;User Id=sa;Password=xxxxxxx
It gave me an error. "Login Failed for User 'sa'"
My application and Database are on the same PC
So , please help.
Thanks

You can not setup in your connection string both Integrated Security=true and User Id=sa;Password=xxxxxx
Integrated Security = true means it is using windows credentials (of the user the application is running into). A database connection cannot use both windows AND SQL Server authentication.
You should either:
Use Integrated Security=true and do not specify User Id nor password. This would use windows authentication.
Use Integrated Security=false or remove it althogether and specify both User Id and Password. This would use SQL Server user/password authentication.
Besides that, there was an adittional problem, originally you put Data Source=.\SQLEXPRESS but had to access default instance via Data Source=. which caused that error.

Allow Remote Connections enabled under Connections in SQL Server Properties.

Related

Connect a Winforms app to a SQL Server from a computer without SQL Server 2017 installed

To start, I am fairly new to both Visual Studio and SQL Server. I am attempting to run an application from computer B which does not have SQL Server installed, and have it connect to a SQL Server database that is hosted on computer A. Both computers are connected over Wi-Fi to the same network.
I have enabled remote connections in SSMS as well as started the SQL Server Browser in services.msc. Exceptions for 1433 and 1434 ports have been added in the Windows firewall out and in rules. In SQL Server config manager I have TCP\IP enabled and the IPAll section in properties has the TCP port set to 1433.
The problem is when I try to add a user to a login on SSMS it only looks for logins on my host machine(I don't know if this is how it should work but I digress). So I am using a SQL Server login username and password (that is enabled in SSMS) in my connection string in the Winforms app.
However, when I run the published app from computer B, I get this error:
Login Failed. The login is from an untrusted domain and cannot be used with integrated authentication
How would I rectify this problem to make computer B a trusted domain?
Like I said I am new to SQL Server and VS so a lot of what I found online was either too complicated or I had already done. Thanks in advance.
Edit: My connection string is "Data Source=DESKTOP-24DR8LC\SQLEXPRESS2; Initial Catalog=HouseFinances; User ID = user; Password = password;Integrated Security = False"
Whenever I need to check a connection string I do the following:
Create a new text file on the desktop called test.txt.
I rename it test.udl.
Double clicking on that file will bring up the Data Link Properties window.
Start on the first tab and select your source, go to the second tab and fill in the properties for your connection.
Click the test connection button.
If everything connects ok, close the window and open the file using notepad.
In there you will find your connection string.
Hope that helps.
Your connection string is specifying Integrated Security or TrustedConnection. You need to remove those options and add the User and Password for the SQL account

Cannot connect to SQL Server using SqlClient in a desktop application

Connecting to SQL Server with a SQL username and password is no problem at all.
These scenarios work just fine:
from C# code using SqlClient in ASP.NET / MVC
with ssms
with Visual Studio Connection Properties Wizard
For example, this works in an MVC project:
new SqlConnection("Server=DB1;Database=testdb;User Id=testuser;Password=testuser1;").Open();
But this identical code fails when run in a desktop application project. It causes the following inner exception error:
user name or password is incorrect
(yes there is a space between user and name)
I've tried:
making sure the protocols are right in SQL Config Manager.
opening up ports in the firewall on client and server.
connecting to another database to see if it's the database and not the client.
the NuGet version of SqlClient.
restarting the dev machine and the server.
ensured the Browser SQL service is running.
ensured the main SQL service is set to automatic and is running.
SqlClient, OLEDB, and ODBC along with appropriate connection strings.
Storing the username and password in a separate credentials object.
Specifying Integrated Security=False in the connection string
But still, nothing.
Why can I connect to SQL Server from everything except from code running in a desktop .NET application? Is something along the way stripping my password out before the connection happens?
Let's try :
data source=DB1;initial catalog=testdb;persist security info=True;user id=testuser;password=testuser1;
User must be a SQL user.

Unable connect to SQL Server 2008 R2

I have a C# project with SQL Server 2008 R2 and the project runs with no problem on my local machine and when I publish it to the server Win2008 R2. But the project doesn't work and can't connect to the server with no error. All the database setting check and I enabled TCP/IP setting and etc. But still not worked. I think the problem is with connection string that is below.
When pages going to connect to the database, nothing happened in browser and browser stick in loading mode (waiting for respond).
Connection string on my computer
Data Source =(Local);Initial Catalog=Bulk;Integrated Security=True;Timeout=0;Max Pool Size=5000;
Connection string on Windows server
Data Source=My Ip Windows Server,1433;Network Library=DBMSSOCN;Initial Catalog=Bulk;User ID=administrator;Password=My Windows Login Pass;Timeout=0;Max Pool Size=5000;
I got this error:
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified
In your connection string of Server you have:
Data Source=My Ip Windows Server,1433;Network Library=DBMSSOCN;
To connect via IP Address and Port of 1433 and over TCP/IP protocol
Initial Catalog=Bulk;
Bulk is your database name
User ID=administrator;Password=My Windows Login Pass;
Authenticate via SQL Server connection by administrator login user and its password.
I think your problem is in third part that you want to connect to SQL Server by your Windows user name; You can see valid logins of your SQL Server in Security branch of your registered server in SQL Server Management Studio that will shows your Windows user name like Domain Name\User Name so you need to change User Id to that.
Configure the Windows Firewall to Allow SQL Server Access. open port 1433 in windows firewall.
see this link. Or this.
Please Try to connect in your local Sqlserver With Live Login Detail,
If you are Success Then There should be Permission Need to set From the Live Hosting Server
if is it possible then please add the More specification.
You should log the errors that occur, e.g. in Application_Error if it's actually an ASP.Net project.
Using the log you should get errormessages that hint you or enable you to give more details so nobody has to guess what your actual issue is.
Regarding your issue:
Did you give your Windows account the necessary READ/WRITE access on SQL Server?
On MS SQL Server the account needs access to:
1. Be added to the general Security/Logins for an actual match of Windows account to Login.
2. Be added to the specific database Security/Users tab with the necessary database role membership.
In addition to specifiying your Windows account + password in the connectionstring: maybe it's better to run the application pool using a service-account and use
Integrated Security=True
and remove
User ID=administrator;Password=My Windows Login Pass;
I have changed my connection string to this and problem solved.
public static string ConnectionString = "Data Source=My Ip,1433;Initial Catalog=My DataBase;UID=sa;Pwd=My Pass;Timeout=0;Max Pool Size=5000;";
In sql server management studio
Click "Security" => "Logins" => "sa" right-click and click properties
in the left side click "Status" and in the right side,Choose enable in login

Connect with SQL Server 2008 Express using Winforms (C#)

I am creating the database from coding in Winforms (C#) and also run the script for creating the tables.
In short I create the database using the Winforms (C#) in SQL Server Express.
But when I run my application I get this error :
Cannot open database '' requested by the login. the login failed. login failed for user 'KETAN\admin'.
My connection string:
Server=.\SQLEXPRESS;Integrated Security=True;database=mydatabasename;User Id=myusername;Password=mypassword;
Any help would be appreciated.
If you have both
Integrated Security=True;
and
User Id=myusername;Password=mypassword;
in your connection string, then the Integrated Security wins and the specific user and password are ignored, and the connection is attempted with your current Windows credentials (KETAN\admin) and that doesn't seem to work
So just change your connection string to:
Server=.\SQLEXPRESS;database=mydatabasename;User Id=myusername;Password=mypassword;
Now, your specific user and its password will be used to connect to SQL Server, and if those are correct, you should be able to connect just fine

Using Windows Authentication with Connection string in IIS to connect to SQL 2008 R2

I'm am making a model first website using MVC4 on IIS7.
Currently my connection string looks like this:
<add name="VaultEntities"
connectionString="metadata=res://*/Models.VaultDB.csdl|res://*/Models.VaultDB.ssdl|res://*/Models.VaultDB.msl;provider=System.Data.SqlClient;
provider connection string="
data source=sqlserver\instance;
initial catalog=vault;
integrated security=True;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
The connection was auto generated by Visual Studio 2011 when I added the database through the Model First approach. The query works perfectly locally on my development machine. But it dosen't work on the IIS server.
My guess is that this is because my user account on my development machine has access to the SQL server and when IIS tries to access the SQL server its user account gets denied.
In a perfect world I would add a user on the SQL server and use
User Id=myUsername;Password=myPassword;
in my connection string, unfortunately I don't have that option.
Without going into details the dba's are...difficult...to work with.
So is there any way to use my AD account in the connection string to login to the SQL server... Or can I make IIS identify itself as me?
Is there any way to use my AD account in the connection string to
login to the SQL server... Or can I make IIS identify itself as me?
You can get the application pool to use any AD user you want.
I would not recommend using your own username though. How many times have you locked your username in the last 12 months, and what happens when you leave?
Create/use a service AD account, map it in the application pool, and get the DBAs to give DB access to that user, using trusted connection. Sorry, you'll have to talk to the DBAs! :-)
Ok, there is two options.
1. Either you change you application pool identity to one which has AD access to database, this can be done also by using impersonation section in web.config file of your website(Check this link).
2. Either you add sql account to your connection string. As I can see you are using EF connection string, it's a little bit different from standard sql connection string. But still has the same part with sql credentials, just replace integrated security = true to used id =...;password=.... That's all.
Hope this helps.

Categories

Resources