using System;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] filePaths = Directory.GetFiles(@"c:\myListOfScripts\", "*.sql");
            Array.Sort(filePaths);

            string connectionString = "server=myServer\\myInstance;database=myDatabase;uid=sa;pwd=mySaPwd";

            SqlConnection cn = new SqlConnection(connectionString);

            Console.WriteLine("Last how many scripts you want to execute?");
            var lastNoScript = Convert.ToInt16(Console.ReadLine());

            FileStream ostrm;
            StreamWriter writer;
            TextWriter oldOut = Console.Out;
            try
            {
                ostrm = new FileStream("./log.txt", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(ostrm);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open log.txt for writing");
                Console.WriteLine(e.Message);
                return;
            }

            Console.SetOut(writer);
            cn.Open();
            for (int i = filePaths.Length - lastNoScript; i < filePaths.Length; i++)
            {
                Console.WriteLine("Executing script: ");
                Console.WriteLine(filePaths[i]);
                Console.WriteLine();

                FileInfo file = new FileInfo(@filePaths[i]);
                string script = file.OpenText().ReadToEnd();

                ServerConnection svrConnection = new ServerConnection(cn);
                Server server = new Server(svrConnection);
                try
                {
                    server.ConnectionContext.ExecuteNonQuery(script);
                }
                catch (Exception e)
                {
                    Console.WriteLine(script + " : Message: " + e.Message + " Inner exception: " + e.InnerException.Message);
                }
            }
            Console.SetOut(oldOut);
            writer.Close();
            ostrm.Close();
            Console.WriteLine("Press any key...");
            Console.ReadKey(true);
        }
    }
}


For:

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

you will need to add reference to files:

C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.ConnectionInfo.dll

and

C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Management.Sdk.Sfc.dll

C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll

Your app.config file must to look like:

<?xml version="1.0"?>
<configuration>
<!--<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>-->
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <requiredRuntime version='v4.0.20506' safemode='true'/>
    <supportedRuntime version='v4.0'/>
  </startup>
</configuration>

To add app.config file do next:

1. On the Project menu, click Add New Item.
2. The Add New Item dialog box appears.

Select the Application Configuration File template and then click Add.

A file named app.config is added to your project.