I am using the below-written script to upload my database to the XAMPP server automatically when I install my exe file but unfortunately, it's not happening. I am placing my ".SQL" file in the same folder where my exe file is located manually and
I am using click once a method to make exe file. Kindly check the below code and guide me accordingly where I am wrong
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void MainWindow_load(object sender,EventArgs e)
{
if (!checkifdbexist())
{
generatedatabase();
}
}
private bool checkifdbexist()
{
String conn = "datasource=127.0.0.1;port=3306;username=root;password=;Initial Catalog=Demon;";
MySqlConnection con = new MySqlConnection(conn);
try
{
con.Open();
return true;
}
catch
{
return false;
}
}
private void generatedatabase()
{
List<string> cmds = new List<string>();
if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\demo.sql"))
{
TextReader tr = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\demo.sql");
String line = "";
string cmd = "";
while ((line = tr.ReadLine()) != null)
{
if (line.Trim().ToUpper() == "GO")
{
cmds.Add(cmd);
cmd = "";
}
else
{
cmd += line + "\r\n";
System.Windows.MessageBox.Show(" No file");
}
}
if (cmd.Length > 0)
{
cmds.Add(cmd);
cmd = "";
}
tr.Close();
}
if (cmds.Count > 0)
{
MySqlCommand comm = new MySqlCommand();
comm.Connection = new MySqlConnection("datasource=127.0.0.1;port=3306;username=root;password=;Initial Catalog=MASTER;");
comm.CommandType = System.Data.CommandType.Text;
comm.Connection.Open();
for (int i = 0; i < cmds.Count; i++)
{
comm.CommandText = cmds[i];
comm.ExecuteNonQuery();
}
}
}