-2

I try to connect to sql-server by calling this class:

class DataCon
{
    public static SqlConnection Connection { get; private set; }

    public static void connectDB(string ip, string dbName, string user, string pw)
    {
        string connectionSt = "Server="+ip+";Database="+dbName+";User Id="+user+";Password="+pw+";";
        ConnectToSql(connectionSt);
    }

    private static void ConnectToSql(string connectionSt) {
        Connection = new SqlConnection(connectionSt);
        Connection.Open();
    }

    public static void connectDB(string ip, string dbName)
    {
        string connectionSt = "Server="+ip+";Database="+dbName+";Trusted_Connection=True;";
        ConnectToSql(connectionSt);
    }
}

It work when using Windows Authentication but the Server Authentication that give login failed for user 'sa'.

On Sql server management studio I could login successfully with both Windows and Server Authentication with 'sa'.

This is the form I work with:

        private void btnConfirm_Click(object sender, EventArgs e)
    {
        try
        {
            string dbsName = "dbsStudent";
            string ip = "localhost";
            string user = txtUser.Text;
            string pw = txtUser.Text;

            if (cbAuthentication.SelectedIndex == 0)
                DataCon.connectDB(ip,dbsName);
            else
                DataCon.connectDB(ip,dbsName,user,pw);
            this.Hide();
            new ViewSt().Show();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
GTHell
  • 603
  • 1
  • 8
  • 20
  • I don't understand the thumb down at all. I said that I could login to management studio with that 'sa' user but not in C#....... – GTHell Jun 04 '17 at 08:28
  • your question is in this links: https://stackoverflow.com/questions/14567939/login-failed-for-user-c-sharp-with-sqlconnection https://stackoverflow.com/questions/9636511/login-failed-for-user-sa-in-connection-string https://stackoverflow.com/questions/17016681/login-failed-for-user-sa-at-sql-server-2008-r2 – Ali Rasouli Jun 04 '17 at 08:31
  • I know you going to point to those two question that I've already read..... It's not the same problem, dude – GTHell Jun 04 '17 at 09:29

2 Answers2

0

If your password contains special chars like ';', your connection string will be incorrect.

Consider using SqlConnectionStringBuilder class to build connection string as described on MSDN.

Oleg Belousov
  • 509
  • 3
  • 8
0

Maybe you should take a look about Integrated Security

Integrated Security When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. 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.

So probably if you want to use user "sa" to log in to a DB you should take a look about setting Integrated security to false in your connection string and try to work it out :)

If you have any question leave me a comment here.

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102