Memorable code
Join the DZone community and get the full member experience.
Join For Freepublic class Program { static List<Thread> list = new List<Thread>(); private static void Main(string[] args) { var lines = File.ReadAllLines(args[0]); foreach (var line in lines) { var t = new Thread(Upsert) { Priority = ThreadPriority.Highest, IsBackground = true }; list.Add(t); t.Start(line); } foreach (var thread in list) { thread.Join(); } } private static void Upsert(object o) { var args = o.ToString().Split(','); try { using(var con = new SqlConnection(Environment.CommandLine.Split(' ')[1])) { var cmd = new SqlCommand { Connection = con, CommandText = "INSERT INTO Accounts VALUES(@p1, @p2, @p3, @p4,@p5)" }; for (var index = 0; index < args.Length; index++) { cmd.Parameters.AddWithValue(@"@p" + (index + 1), args[index]); } try { cmd.ExecuteNonQuery(); } catch (SqlException e) { if(e.Number == 2627 ) { cmd.CommandText = "UPDATE Accounts SET Name = @p2, Email = @p3, Active = @p4, Birthday = @p5 WHERE ID = @p1"; cmd.ExecuteNonQuery(); } } } } catch (SqlException e) { if(e.Number == 1205) { var t = new Thread(Upsert) { Priority = ThreadPriority.Highest, IsBackground = true }; list.Add(t); t.Start(o); } } } }
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Does the OCP Exam Still Make Sense?
-
RBAC With API Gateway and Open Policy Agent (OPA)
-
Multi-Stream Joins With SQL
-
Developers Are Scaling Faster Than Ever: Here’s How Security Can Keep Up
Comments