SQL Server Express LocalDB – Portable Client Tool for Query Execution

command linesql serversql-server-localdb

I am running out of ideas on this little problem:

I need to execute a query on a LocalDB database directly on user's computer. I do not want to install anything there and there is only .NET (4.5.2) and SQL Server 2014 Express LocalDB installed.

I do not want to copy database file to my computer and back againt with a little modification, I need to run just one or two queries directly on that machine.

Is there any portable command line tool that would allow me to do this? SQLCMD is not usable, since it requires installation of ODBC driver, SSMS requires installation too.

Best Answer

I suggest just writing a simple .NET console app to do just that. It can accept parameters for "ConnectionString" and "Query". This will be flexible enough to use in various situations.

using System;
using System.Data;
using System.Data.SqlClient;

namespace SimpleSqlCmd
{
    class Program
    {
        static void Main(string[] args)
        {
            using(SqlConnection _Connection = new SqlConnection(args[0]))
            {
                using(SqlCommand _Command = new SqlCommand(args[1], _Connection))
                {    
                   _Connection.Open();

                   _Command.ExecuteNonQuery();
                }
            }

            return;
        }
    }
}

Usage would be:

SimpleSqlCmd "Server=(localdb)\InstanceName;" "Do some SQLs;"

UPDATE:

I have created a project on GitHub to host a more functional / complete version of the code shown above. The code on GitHub will be updated over time whereas the code here will not be updated (since that is really not the purpose of this site). The repository URL for Simple SQL Exec (renamed, to avoid confusion, from "SimpleSqlCmd" as it is shown above):

https://github.com/SqlQuantumLeap/SimpleSqlExec