Problems with try & catch

  • Thread starter Thread starter Carlo Goretti
  • Start date Start date
C

Carlo Goretti

Guest
Hey!

Since i have started to use the Server class instead of SQL class for executing T-SQL scripts then the try and catch no longer works..

What am i missing here?

try
{


connetionString = "Data Source = " + ServerComboBox.SelectedItem.ToString() + "; Initial Catalog=master; integrated security = true";
queryString = "SELECT NAME FROM sys.databases ";

Microsoft.Data.SqlClient.SqlConnection sqlConnection = new Microsoft.Data.SqlClient.SqlConnection(connetionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);
reader = server.ConnectionContext.ExecuteReader(queryString);

while (reader.Read())
{
MomentumComboBox.Items.Add(reader[0]);
}
reader.Close();

}
catch (SqlException sql)
{
MessageBox.Show("Följande fel: \n\n" + sql.Message);
}
Here is another example:

TidsåtgångLbl.Visible = true;
TidLbl.Visible = true;
connetionString = "Data Source = " + ServerComboBox.SelectedItem.ToString() + "; Initial Catalog=master; integrated security = true";
Microsoft.Data.SqlClient.SqlConnection sqlConnection = new Microsoft.Data.SqlClient.SqlConnection(connetionString);
ServerConnection svrConnection = new ServerConnection(sqlConnection);
Server server = new Server(svrConnection);

for (int i = 0; i < lstChckBox.Count(); i++)
{
//Här kollar den vilket av skripten som ska köras.
if (lstChckBox.Checked == true)
{
testTick = 0;
TidPerSkriptTimer.Start();
try
{
queryString = File.ReadAllText(files, Encoding.Default);
server.ConnectionContext.ExecuteNonQuery(queryString);

lstChckBox.ForeColor = Color.Green;

}
catch (SqlException sql)
{
lstChckBox.ForeColor = Color.Red;
MessageBox.Show("Detta skript gick inte att köra: " + Path.GetFileName(files).ToString() + "\n Följande fel: \n\n" + sql.Message);
//Ifall skriptet inte kunde köras blir texten Röd.
return;
}
//TidPerSkriptTimer.Stop();
TidPerSkriptTimer.Stop();
fi = new FileInfo(files);
MessageBox.Show("Nu är " + fi.Name + " Kört!");
}

}

Best Regards..

Continue reading...
 
Back
Top