I couldn’t solve the problem, so I created a C# executable that does the same thing.
I found out that we can use SqlCommand to achieve the same thing :
conn = new SqlConnection($"Data Source ={server};Initial Catalog={dbname};" +
$ "Persist Security Info=True;User ID={id};Password={password}");
conn.Open();
SqlCommand cmd = new SqlCommand($"USE MASTER RESTORE DATABASE {dbname} FROM DISK = '{bakPath}' WITH REPLACE;", conn);
cmd.ExecuteNonQuery();
conn.Close();
Here is the code of my .exe :
static void Main(string[] args) {
string server;
string dbname;
string id;
string password;
string bakPath;
SqlConnection conn;
if (args.Length > 0) {
server = args[0];
dbname = args[1];
id = args[2];
password = args[3];
bakPath = args[4];
try {
Console.WriteLine("#--------------------------------------------------#");
Console.WriteLine("server=" + server);
Console.WriteLine("dbname=" + dbname);
Console.WriteLine("id=" + id);
Console.WriteLine("password=" + password);
Console.WriteLine("bakfile=" + bakPath);
Console.WriteLine("#--------------------------------------------------#");
Console.WriteLine("Processing restoration...");
conn =
new SqlConnection($"Data Source ={server};Initial Catalog={dbname};" +
$ "Persist Security Info=True;User ID={id};Password={password}");
conn.Open();
SqlCommand cmd = new SqlCommand($"USE MASTER RESTORE DATABASE {dbname} FROM DISK = '{bakPath}' WITH REPLACE;", conn);
cmd.ExecuteNonQuery();
conn.Close();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Restoration done !");
Console.ForegroundColor = ConsoleColor.White;
} catch (Exception ex) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error :" + ex.Message);
Console.ForegroundColor = ConsoleColor.White;
}
} else {
Console.WriteLine("You need to pass the arguments required")
}
}
I can now run my .exe with arguments.