0
How to ignore a exception
and insert the rest of the fields when I get this error in MYSQL using mysql . NET Connector? Mine try catch
is as follows:
try
{
conexao.Open();
MySqlDataReader reader = comm.ExecuteReader();
reader.Read();
return reader.GetString("ID");
}
catch (MySqlException e)
{
if (e.Number == 1062) //Duplicate entry
{
MySqlDataReader reader = comm.ExecuteReader();
reader.Read();
return reader.GetString("ID");
}
else
{
Debug.WriteLine(Query);
Debug.WriteLine(e);
return "Error";
}
}
catch (Exception e)
{
Debug.WriteLine(Query);
Debug.WriteLine(e);
return "Error";
}
finally
{
conexao.Close(); //Fechando a conexão
}
Of course when you have duplicate emails inside values
of my query I fell into the first catch (MySqlException e)
inside it I began to filter the error by if (e.Number == 1062
he checks and falls right into that if
, but when I will execute the command MySqlDataReader reader = comm.ExecuteReader();
MYSQL warns again of the error and code goes to the finally
and let’s say if I have 1 duplicated email out of 100 emails, I would like to insert 99 and ignore the email that already exists in the table.
Thought to make a select by checking if the email already exists?
– Gabriel Coletta
It’s a lot of emails, it’s an INSERT with N-type email numbers
– Leonardo Bonetti
calmai, are you using 1 Insert to add multiple emails? Your bank is normalized?
– Gabriel Coletta
@Gabrielcoletta yes, what happens is I analyze a folder of an email (in case the company’s email) and look for emails sent with error to clear my other email base, not to be ordering the bank all the time I give a
INSERT
on the tableEMAILS_ERROS
with several emails together(INSERT INTO table VALUES(),(),()
– Leonardo Bonetti
And it’s very well normalized.
– Leonardo Bonetti
I never used INSERT like this and I didn’t even know I had.
– Gabriel Coletta
https://dev.mysql.com/doc/refman/5.5/en/insert.html take a look at
– Leonardo Bonetti