1
I have an application with all interfaces and features. In the same project I put a simple code running on console to check if there is already a database created on the machine. If not, he must execute the script that creates this. The code is as follows:
public static class Program
{
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool FreeConsole();
[STAThread]
public static int Main(string[] args)
{
if (args != null && args.Length > 0)
{
Console.ReadLine();
return 0;
}
else
{
String connString = @"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=True";
String cmdText = "select * from master.dbo.sysdatabases where name= 'db_teste'";
Boolean bRet;
SqlConnection sqlConnection = new SqlConnection(connString);
SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection);
try
{
sqlConnection.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
bRet = reader.HasRows;
sqlConnection.Close();
}
catch (Exception e)
{
bRet = false;
sqlConnection.Close();
Console.Write(e.Message);
}
if (bRet == true)
{
Console.WriteLine("Atualizando Tabelas...");
}
else
{
string sqlConnectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
var file = new FileInfo(@"C://script.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
FreeConsole();
var app = new App();
return app.Run();
}
}
This is always the first part to run by the program when it runs. Well, I create installers with Visual Studio Installer - > setup Wizard.
How to access the SQL script?
var file = new FileInfo(@"C://script.sql");
In this passage FileInfo
will seek as a reference what is already setate, but if during the installation the user chooses another location to install the application the application will not be able to locate the file .sql
. How can I fix this?
In the project setup added the file to the project folder where some DLL’s are located.
This file will always be in the folder where the application is running?
– Maniero
@mustache good that I want but don’t know how to do it during the process of creating the installer. Maybe with the option to add folders to the project, but finally how can I reference if it is in the folder where the application is running?
– Wesley Heron
I answered on top of what you told me, if you need to take an arbitrary directory, you need to modify the installer to tell the application where it is, that’s another answer but it’s not even about the application and it would be another question.
– Maniero