I have 2 forms, first collecting the details like datasource, database name, username and password. Second form collects the sql script file to be executed and the database name to be connected to..
What I want to achieve is that I should be able to execute the sql script file in the selected database.
The code used in the second form is like this:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
string sel = comboBox1.SelectedItem.ToString(); //Here "sel" is the database selected
if (sel == "master")
{
comboBox2.Items.Clear();
//Selects a default file
DirectoryInfo dinfo = new DirectoryInfo(@"D:\Testpgm");
FileInfo[] Files = dinfo.GetFiles("master.sql", SearchOption.AllDirectories);
foreach (FileInfo file in Files)
{
comboBox2.Items.Add(file.Name);
}
}
else
{
comboBox2.Items.Clear();
//Selects a default file
DirectoryInfo dinfo = new DirectoryInfo(@"D:\Testpgm");
FileInfo[] Files = dinfo.GetFiles("dbscript.sql", SearchOption.AllDirectories);
foreach (FileInfo file in Files)
{
comboBox2.Items.Add(file.Name);
}
}
}
And the code used in combobox 2 is:
private void comboBox2_TextChanged(object sender, EventArgs e)
{
string textsel = comboBox2.SelectedItem.ToString(); //textsel is used to select the sql file selected
if (textsel == "master.sql")
{
richTextBox1.Clear();
//Read the selected sql file and display it in richtextbox
System.IO.StreamReader myFile = new System.IO.StreamReader(@"D:\Testpgm\master.sql");
string textentry = myFile.ReadToEnd();
richTextBox1.AppendText(textentry);
}
else
{
richTextBox1.Clear();
//Read the selected sql file and display it in richtextbox
System.IO.StreamReader myFile = new System.IO.StreamReader(@"D:\Testpgm\dbscript.sql");
string textentry = myFile.ReadToEnd();
richTextBox1.AppendText(textentry);
}
}
Now, I want to connect to the database which I selected in combo box 1 and then exceute the sql script included in the sql file that is selected in combo box 2, at the click of a button.
Is this possible? How can I achieve this?
Any comments would be really helpful and appreciated..