Connect to SQL Server 2012 using SQL Server authentication in C# RRS feed

  • Question

  • Hello everyone,

    I have been looking for a while for a way to connect to a database using server authentication mode and so far this is the only thing I have found, if someone can help me out please. I got this from Microsoft pages but I haven't being able to find how to query the database either.

    Note: I'm able to connect to DB using the first approach but if I want to use the second I cannot do it

    // Connecting to an instance of SQL Server using SQL Server Authentication
          Server srv1 = new Server();   // connects to default instance
          srv1.ConnectionContext.LoginSecure = false;   // set to true for Windows Authentication
          srv1.ConnectionContext.Login = sqlServerLogin;
          srv1.ConnectionContext.Password = password;
          Console.WriteLine(srv1.Information.Version);   // connection is established
          // Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection
          ServerConnection srvConn = new ServerConnection();
          srvConn.ServerInstance = @".\" + instanceName;   // connects to named instance
          srvConn.LoginSecure = false;   // set to true for Windows Authentication
          srvConn.Login = sqlServerLogin;
          srvConn.Password = password;
          Server srv2 = new Server(srvConn);
          Console.WriteLine(srv2.Information.Version);   // connection is established


    Wednesday, August 13, 2014 5:51 PM