U
U
UrbanRider2017-01-25 13:00:42
Programming
UrbanRider, 2017-01-25 13:00:42

How to avoid brakes when try catch is triggered?

Good afternoon, dear colleagues.
There is a table with a bunch of records in which the Subscriber field is represented as a phone number and there is a table of correspondence between the phone number and the full name (Subscriber field and FIO). The point is that not all subscribers have full name designations, as a result of which exceptions (Exceptions) will necessarily fall out, which I process and leave there just a phone number.
I am fetching from SQL like this:

SELECT TOP 100 Calls.ID, Calls.DateTime, Calls.Trunk, Calls.Subscriber, Subscribers.FIO, Calls.ExtNum, Calls.Duration, Calls.Direction, Calls.ExitCode FROM Calls LEFT OUTER JOIN Subscribers ON Calls.Subscriber = Subscribers.Subscriber ORDER BY ID DESC

I read further:
while (reader.Read())
                    {
                        rec = new CDRRecord();
                        rec.ID = reader.GetInt64(0);
                        rec.DateTime = reader.GetDateTime(1);
                        rec.Trunk = reader.GetString(2);

                        //Если значение из Subscribers.FIO не Null, присваиваем его записи, 
                        //если null - будет Exception
                        try
                        {
                            rec.Subscriber = reader.GetString(4);
                        }
                        //тогда присваиваем записи значение Calls.Subscriber
                        catch
                        {
                            rec.Subscriber = reader.GetString(3);
                        }

                        rec.ExternalNumber = reader.GetString(5);
                        rec.Duration = TimeSpan.FromTicks(reader.GetInt64(6));
                        rec.Direction = reader.GetString(7);
                        rec.ExitCode = reader.GetString(8);
                        records.Add(rec);
                        
                    }

This part works fine, except that it is very slow. If you make a selection of 100 records, then it hangs for 1 second - not particularly critical, if 1000 can hang up to 5 seconds, and without this processing there will be no brakes, but I would like it to be where there is information about the subscriber, and where it is not it would just show the phone number.
Brakes are inversely proportional to the amount of data in the Subscribers table, which is logical - the more exceptions are thrown, the more brakes.
I ask you to help either change the algorithm to avoid exceptions, or suggest how to avoid brakes in such logic.
I understand that I'm doing something wrong, but here's where ...
PS I tried to specify a specific exception in catch:
catch(SqlNullValueException)
no effect.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Melkij, 2017-01-25
@UrbanRider

Why do you need not only exceptions, but also logic in the application in general? This behavior is elementarily specified in the request.

SELECT TOP 100 Calls.ID, Calls.DateTime, Calls.Trunk, 
coalesce(Subscribers.FIO, Calls.Subscriber) as fieldname, -- если FIO NULL, тогда использовать значение поля Subscriber
...

F
Fat Lorrie, 2017-01-25
@Free_ze

Call IDataRecord.IsDBNull(int) instead of try-catch before calling .

D
Denis Zagaevsky, 2017-01-25
@zagayevskiy

Do not use exceptions to handle the flow of program execution. Exceptions are expensive. Do a normal check instead.

D
d-stream, 2017-01-25
@d-stream

In my opinion, here it would be necessary to change the ideology from the sql query,
what prevents wrapping it in isnull Subscribers.FIO and throwing out the id or something else in this case?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question