How to use username/password instead of trusted connection

Apr 24, 2012 at 8:33 AM
Edited Apr 24, 2012 at 8:34 AM

In Program.cs, add the following:

using Microsoft.SqlServer.Management.Common;

Below, in private void Run(), replace

            //Create server and database connection
            server = new Server(config.server);
            Database db = server.Databases[config.database];

with the following:

            //Create server and database connection
            if (config.userName == null) //use Trusted Connection
            {
                server = new Server(config.server);
            }
            else //use credentials given
            {
                ServerConnection conn = new ServerConnection();
                conn.LoginSecure = false;
                conn.Login = config.userName;
                conn.Password = config.password;
                conn.ServerInstance = config.server;
                server = new Server(conn);
            }
           Database db = server.Databases[config.database];

Rebuild; afterwards, you may use the /U and /P command line switches.