SQLConnection connection error "Login failed for user server/guest" - c#

I keep receiving a connection error. I know that the information is right, as I tried them out with SSMS now several times in the last few minutes, so the problem is in the C# connection string. According to various Google searches, the syntax is right, just it does not work.
try
{
// Get the connection information.
GetSQLConnectInfo(ref sConnect);
// Formulate the connection string.
String strConnect = String.Format("Server=myserver;Database=mydb;User Id=myusername;Password=mypassword;Trusted_Connection=yes;connection timeout=30");
// DATABASE: Create the connection.
SqlConnection oConnection = new SqlConnection(strConnect);
oConnection.Open();
if (ConnectionState.Open != oConnection.State)
return false;
return true;
}
catch
{
}
Error comes back with: Login failed for user 'myserver\Guest'.
Error Code: -2146232060
Error Number: 18456
It would seem that judging by the error message, the Open method does not recognize the user name, as Open tries to use a guest account, which obviusly will not work.
Am I missing something? I am using SQL Server authentication.

Remove Trusted_Connection=yes. That will override your SQL Authentication (username/password) settings and try to log in as the user running the app.

Related

error connection to database c#

I'm trying to connect to a database from VS2017
var str = str1.ConnectionString =
"Data Source=141.*****.199;" +
"Initial Catalog=****;" +
"User id=***;" +
"Password=****;";
using (SqlConnection conn = new SqlConnection(str))
{
try
{
conn.Open();
var text = "SELECT * FROM Users u WHERE u.Id=76769";
...
}
catch (Exception e)
{
return req.CreateResponse(HttpStatusCode.OK, e.ToString());
}
and get 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.
It's azure function. If I set this connection from LogicApp, then there are no errors.
Why the connection does not work from the azure-function?
can someone help. Functions refer to the database not with only ip , which are listed on the portal. They have several variants. And on the portal these addresses do not look, only through https: //resources.azure.com. And yes, you just need to specify in the firewall all valid values
I am connecting to an SQL DB from Azure Function, and I noticed a little difference in connection string that I am using.
Pls try and see if it works for you:
"Server=tcp:141.*****.199,1433;Initial Catalog=****;User ID=*****;Password=*****;"
1433 - is the default MS SQL port number.

C# SQL Server connection using server Login

I have a problem with connecting to my SQL server. I keep getting "Login failed for user" error messages, but the user I am trying to login as exists as a login on the server. The connection string I use looks like this:
Data Source=[server name];Initial Catalog=[database];User ID=[login name];Password=[login password];
Is it because the login exists on the server, but not as a user on the database that I can't use it to login?
If so is it not possible for me to connect to the server using a server login?
Thanks in advance.
It may be that there is something else wrong with your connection string. I know if I have the wrong database or server in the string it will be give me an "unable to login with user..." message.
Data Source=[server name];Initial Catalog=[database];User ID=[login name];Password=[login password];
Use this code to connect SQL Server (preferrd Language is C#)
Public Sub Start_DB_Connection(servername As String,user As String,password As String,dbname As String)
Try
db_con As New SqlConnection
If Not db_con Is Nothing Then db_con.Close()
db_con.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; connection timeout=1;", servername, user, password, dbname)
db_con.Open()
Catch ex As Exception
// show error
End Try
End Sub

Npgsql : is the server down or a bad password?

I am trying to connect to PostgreSQL database from my c# application like so:
NpgsqlConnection MyConnection = new
NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=mypassword;Database=mydatabase;");
try
{
MyConnection.Open();
}
catch (NpgsqlException pe)
{
//Code "28P01" = user name or password is wrong
// server ip or port is wrong
}
the question is : NpgsqlException.code does not differentiate between the following conditions:
server ip /port number is wrong
user name and password combination is wrong
Code "28P01" is returned in both cases. obviously Npgsql can see that the server is there and responding with some data indicating bad user name or password (condition #2 above) or nobody seem to be there (condition #1 above)
how can i differentiate between those 2 cases in my code?
with Npgsql v 3.0.2.0 (not sure about older versions) if the IP/hostname is wrong an Exception with code =-2146233083 with be thrown. if however the IP/hostname is correct but the port number is wrong Exception with code= -2147467259 with be thrown. if the IP/port is correct but username /password is wrong a NpgsqlException with code = "28P01" will be thrown.
You're probably using Npgsql 2.x; the new Npgsql 3.x throws NpgsqlException only when errors are received from a PostgreSQL server. Network connection errors are raised as SocketException etc. You're encouraged to upgrade.
By the way: I couldn't reproduce your exact findings even with Npgql 2.x, connecting to a wrong port or a wrong IP resulted in an NpgsqlException with Code being an empty string, not 28P01.

Entity framework connection string enable to connect to DB server

I'm using the entity framework in a winforms application.
When i set scsb.DataSource ="localhost" every thing works fine but when i try to connect to onother DB server i got an exception:
"The underlying provider failed on Open."
public DistributionSSEntities1 Connection()
{
var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "192.168.1.100";
scsb.InitialCatalog = "DistributionSS";
scsb.IntegratedSecurity = true;
//------------------------
EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
builder.Metadata ="res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = scsb.ConnectionString;
DistributionSSEntities1 db = new DistributionSSEntities1(builder.ToString());
return db;
}
Has the remote Sql been setup to allow remote connections? Has the remote Sql been allowed access through the windows firewall... there's so many reasons why it wouldn't connect.
You're using Integrated Security - which may work great for a local Sql; but the network user that your WinForm app is running under must have the correct rights to access the remote box.
I'd suggest to start eliminating possibilities do the following:
Check the Sql logs on the target server. That always has the exact reason why an attemp failed - not the watered down version you get through the exception. (eg. C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Log)
Connect to it using a sql username password - not integrated security to make sure it's not that
Firewall
EDIT
It's important to remember that the error messages return to the client regarding login attempt failures are purposefully obscure or without information - to limit an attacker gaining enough information to improve the attack (see the technet article for proof). So checking the Sql Server logs is a necessity - if your login/connection attempt actually made it to the server.
From Article:
To increase security, the error message that is returned to the client
deliberately hides the nature of the authentication error. However, in
the SQL Server error log, a corresponding error contains an error
state that maps to an authentication failure condition. Compare the
error state to the following list to determine the reason for the
login failure.
public DistributionSSEntities Connection()
{
string ConString = "SERVER=192.168.1.100;DATABASE=DistributionSS;UID=sa;PASSWORD=125;";
SqlConnectionStringBuilder SCB= new SqlConnectionStringBuilder(ConString);
//------------------------
EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
builder.Metadata = "res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = SCB.ConnectionString;
DistributionSSEntities db = new DistributionSSEntities(builder.ToString());
return db;
}

Connect to SQL Server CE database with password

How can I connect to the .sdf database with a password and still be able to use the tableadapter to FILL out my listbox with this code:
try
{
tbl_sClipManagerTableAdapter1.Fill(db_sClipManagerDataSet1.tbl_sClipManager);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
Produces this error:
The specified password does not match the database password. [ Data Source = PATH TO .SDF FILE ]
I have password protected the database. How can I connect with password and still through the tableadapter?
EDIT:
Oh, I don't want to store the password with the connection string - cause it is to easy readable through the config file.
SOLUTION:
I found the solution by myself.
In DataSet.Designer.cs find something like:
private void InitConnection() {
this._connection = new global::System.Data.SqlServerCe.SqlCeConnection();
this._connection.ConnectionString = global::sClipManager.Properties.Settings.Default.db_sClipManagerConnectionString + "; password=PASSWORDHERE";
I just added the + "; password=PASSWORDHERE".
Everything works now. And the password can't be easily read in the config file.
Best regards
I'm hoping that query is being executed by some kind of soap/rest service. If so, you can have that service run with domain credentials. Then you could give that domain-login access to the db. Then you could use integrated security in the connection string. That way the connection string has -zero- credential info.

Categories