Posts

Demonstrating a SQL deadlock with .NET

var conn1 =new SqlConnection(cs); conn1.Open();var comm1 = conn1.CreateCommand(); comm1.CommandText ="Proc1;";new Thread(()=>{try{ Console.WriteLine("Executing comm1"); comm1.ExecuteNonQuery(); Console.WriteLine("comm1 done");}catch(Exception ex){ Console.WriteLine("comm1: "+ ex);}}).Start();var conn2 =new SqlConnection(cs); conn2.Open();var comm2 = conn2.CreateCommand(); comm2.CommandText ="Proc2";new Thread(()=>{try{ Console.WriteLine("Executing comm2"); comm2.ExecuteNonQuery(); Console.WriteLine("comm2 done");}catch(Exception ex){ Console.WriteLine("comm2: "+ ex);}}).Start();var conn3 =new SqlConnection(cs); conn3.Open();var comm3 = conn3.CreateCommand(); comm3.CommandText ="Proc3;";new Thread(()=>{try{ Console.WriteLine("Executing comm3"…

Check if two images are similar

I was trying to dedupe my photos, and found that identical photos didn't have identical file contents, because of the way they were stored in Google Photos, or iCloud, or Amazon Photos, or wherever else I had them stored. So this method will look at two photos, see if they have the same dimensions, then check to see if they are similar photos. It reduces both photos to 16x16 (regardless of original dimensions), then compares the RGB values of each pixel. If the photos are different enough, that number will be high. If the difference is close to zero, then they are likely the same photo, or at least very similar.I wouldn't use this if you aren't willing to accept a false positive once in awhile - maybe back up your photos to stupidly cheap cloud storage (AWS and Azure have 1$/TB/month now) before using this to dedupe.staticbool AreSimilar(string file1,string file2){ Console.WriteLine("Comparing:\r\n {0}\r\n {1}", file1, file2);using(var image1 =(Bitmap)Image.F…

Javascript arguments vs named parameters

// different behavior in ES5 non-strict vs ES5 strict and ES6function go(id, name ="jack"){++id; name ="bill";// the values of the variables (2, "bill") console.log(id); console.log(name);// the actual values passed in (1, "john")// but in ES5 non-strict, they are linked to the named ones, so (2, "jack") console.log(arguments[0]); console.log(arguments[1]);} go(1,"john");

Javascript rest parameters

// collects parameters and turns them into an array// must be the last parameter// no IE support// no Safari supportfunction writeArguments(name,...values){ console.log(`my name is ${name}`); console.log(JSON.stringify(values));// [1,2,3]} writeArguments("john doe",1,2,3);

SQL Table Valued Parameters in .NET

privatestatic SqlDataRecord CreatePersonRecord(int id,string name){var metaData =new[]{new SqlMetaData("Id", SqlDbType.Int),new SqlMetaData("Name", SqlDbType.VarChar,50)};var record =new SqlDataRecord(metaData); record.SetInt32(0, id); record.SetString(1, name);return record;}private SqlDataRecord[] GetPersonRecords(){ retun new[]{ CreatePersonRecord(1,"John Doe"), CreatePersonRecord(2,"Jane Doe")};}using(var conn =new SqlConnection(cs)){ conn.Open();using(var comm = conn.CreateCommand()){ comm.CommandText ="dbo.TestPerson"; comm.CommandType = CommandType.StoredProcedure;var peopleParmsValue = GetPersonRecords();if(!peopleParmsValue.Any()){ peopleParmsValue =null;// Don't set the value to an empty array} comm.Parameters.Add(new SqlParameter { ParameterName ="@people", Value = peopleParmsValue, SqlDbType = SqlDbType.Structured…

SQL Print output in ADO.NET

Thank you to this StackOverflow answer for showing how to retrieve PRINT result from SQL Server in C#:conn.InfoMessage +=(sender, e)=>{// e.Message contains the print output};

Async/await in console app

// PM> Install-Package Nito.AsyncExusing Nito.AsyncEx;class Program {staticvoid Main(string[] args){ AsyncContext.Run(()=> MainAsync(args));}static async void MainAsync(string[] args){// await Something...}}This was an answer on StackOverflow by async/await superstar and blogger Stephen Cleary, who wrote the referenced NuGet package.