Hello guys!

I am developing simple desktop application that suppose to connect to SQL DB launched on remote server.

Everything going fine, exept that when connecting to DB I get exeption "Login failed for user 'guest'."

So, looks like i need to create some user with password and give him at least 'read rights'. How can I do it.

What I tried:

1. 'sa' name doesn't work.
2. Allow remote connection - enabled.

SqlConnection sqlconn = new SqlConnection("Data Source=64.191.121.53,1433;Network Library=DBMSSOCN;Initial Catalog=remoteDB;User ID=guest;Password=;");

Dani AI

Generated

This thread shows the classic mix of three separate problems: the client was trying to authenticate with a non-existent or unsecured account, the database file was copied instead of being attached to a running SQL Server, and the server-side network/auth settings needed work. As noted, credentials supplied by the client must be SQL Server credentials (not the desktop logon), and was on the right track about using SQL authentication — but several server-side steps are usually missing.

Practical checklist to resolve the issue

  • Ensure the target machine is actually running SQL Server and the database is attached (use SSMS -> Databases -> Attach or restore a backup). Simply copying an .mdf to a webroot will not make the server host the database.
  • Enable SQL Server authentication (mixed mode) if using SQL logins.
  • Create a server-level login and map it to a user in the target database, then grant only the necessary role(s). Example T‑SQL (run in SSMS on the server):
    CREATE LOGIN app_user WITH PASSWORD = 'Str0ng!Pass';
    USE remoteDB;
    CREATE USER app_user FOR LOGIN app_user;
    EXEC sp_addrolemember 'db_datareader', 'app_user';
  • Enable TCP/IP in SQL Server Configuration Manager, set the instance port (default 1433), restart the service and open that port in the server firewall. Test remote access from a client with SSMS before trying the app.

Troubleshooting & security notes

  • Check the SQL Server error log for the login-failure state (helps pin down cause). Verify the login isn’t disabled and its default database is online. Remember a SQL “Credential” is not the same as a login.
  • Do not use guest or blank passwords. Prefer an app-tier service or VPN/SSH tunnel over exposing SQL directly to the Internet. Require least-privilege accounts, enforce strong passwords, and use encrypted connections and parameterized queries for safety.

Recommended Answers

All 5 Replies

NFurman, Looking at your connection string I am unable to determine what credentials you are using, but the credentials in the connection string should be the SQL server credentials, not the client logon credentials.

You also need to set "Integrated Security=False;" when using a username/password connection string. Here are example connection string builders:

public static string BuildSqlNativeConnStr(string server, string database)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
    }
    public static string BuildSqlNativeConnStr(string server, string database, string username, string password)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};Password={3};", 
        server, 
        database,
        username,
        password);
    }

Hello again. thanks for reply. Can you please advise me step by step how to create such db i need.

I made this:

1. Created db in SQL 2008 Express
2. Created an empty table (ID,Name...there will be no any use of it)
3. In Object Explore > Security > Credentials created new credential with Username Password.
4. In Object Explore > Logins created new login and password with the same logs and passes as my new credential.
5. Saved database.
6. From Microsoft SQL Server > MSSQL.1>MSSQL>DATA copied RemoteDB.mdf on the desctop and that after sent this file by FTP into remote webserver. Placed him in root.
7. In my program changed connection string as sknake has adviced.
8. tried to connect. Alas, the same problem cannot connect and throws me MessageBox (exeption) "Login failed for user 'NathanBase'."

Where have I went wrong?

Can you post the existing connection string? Of course, removing any confidential information.

Ok guys. thank you for answers. the way to resolve this problem I found by myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.