Connect to SQL Server container from C# application - c#

Have a C# App and SQL Server database from a domain environment; restored the DB in a non-domain support environment (SQL Server running in a Docker Container). Connections are successful via ODBC using SQL Server authentication:
Server 10.0.0.1
LoginID <username of dbOwner>
Password <password>
Default db ThisProject
The C# App, however, always returns "Login failed: The login is from an untrusted domain" with every variation of a normal connect string:
data source=10.0.0.1;
initial catalog=ThisProject;
integrated security=False;
User Id=<dbOsner>;
Password=<password>;
Trusted_Connection=False;
How does one configure the connect string so that the C# App connects with SQL Server authentication?
TIA!
Tested with variations:
integrated security=False, True, SSPI, <missing>;
trusted_Connection=False, True;

Related

.Net Core 2.1 Web Api connection string using supplied User Id and Password for SQL Connection

I've been trying to get my application to use the supplied user id and password for connecting to my SQL Server but can't get anything to work.
Here is the setup, I have a laptop that I am developing on and a desktop that I am hosting the webapi on as well as the sql server. When I run locally on the laptop connecting to the desktop sql server everything works (using User Id and Password). However when I publish to the desktop and try to connect I get errors connecting to the sql server.
I've tried all settings for Trusted_Connection and Integrated Security as well as not supplying either.
Anything with Trusted_Connection set to false gives me these errors in the Windows Event Logs
"Windows API call SHGetKnownFolderPath returned error code: 5. Windows system error message is: Access is denied."
"Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user."
Anything with Trusted_Connection set to true tries to connect to the SQL Server using the IIS APPPOOL\… identity.
"DefaultConnection": "Server=xxx\\SQLEXPRESS;Database=databaseName;User Id=userId;Password=password;Trusted_Connection=False;Integrated Security=False;"

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

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.

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.

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

Cannot connect from c# to sql locally but can remotely

I have a c# application i want to connect it to sql server , the server and the application are used on the same machine , but I application cannot connect to the sql however if I installed the application on another machine and keeping the server in the first server they are connected !
The server is installed on windows 8.
Thanks
Please do the following -
Make sure your local database server has both Windows Authentication and SQL Server Authentication mode enabled. But
If you are using Windows Authentication then change the connection string to -
connectionString="Data Source=LENOVO;Initial Catalog=APP;Integrated Security=True;"
If you are using SQL Server authentication mode, which it seems since you are providing username yourself then
connectionString="Data Source=LENOVO;Initial Catalog=APP;Integrated Security=False;User ID=sa;Password=111"
Also, APP is usually a reserve keyword in some systems. Its better to avoid such name as the name of the DB.
If you are using default server instance you can use . or (local) instead of LENOVO
Check that the firewall is not preventing you from connecting.

Categories

Resources