Skip to main content

Posts

Showing posts from 2020

How to Floor a number to 0.05 increments in T-SQL

Say you've got a number like 72.26 and you want to round it or floor to the nearest 0.05 The first thing to do is divide it by the round number.   For example, if you want 72.26 rounded to the nearest 0.05, this will show how many 0.05's there are in the number                     72.26 / 0.05 = 1,445.2                     From there, round or floor as needed               eg,                 SELECT ROUND( 72.26 / 0.05  , 0 )   or  SELECT FLOOR( 72.26 / 0.05  , 0 )                Results in 1445.0 Then, multiply it by the round number  This returns you to the original value, less then remainder (or to the next increment if it rounded up)           eg  SELECT 0.05 * ROUND( 72.26 / 0.05  , 0 )   Results in 72.25 Demonstration Code  DECLARE @SourceValues TABLE (     [TestValue] DECIMAL(4, 2) ); DECLARE @Increment DECIMAL(4, 2) = 0.0; WHILE @Increment <= 0.1 BEGIN     INSERT INTO @SourceValues     VALUES     (72 + @Increment);     SET @Increment = @Increment + 0.01; END; SELECT

Can't Install Powershell Modules

 When trying to install a PowerShell module I was getting this error... WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2/'. PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'XXX'. Turns out it was a problem with the SecurityProtocol, fixed by entering  [System.Net.ServicePointManager]::SecurityProtocol =                     [System.Net.SecurityProtocolType]::Tls12;  SecurityProtocols I had in place before entering this command were... PS C:\WINDOWS\system32> [System.Net.ServicePointManager]::SecurityProtocol Ssl3, Tls Afterwards, I had  PS C:\WINDOWS\system32> [System.Net.ServicePointManager]::SecurityProtocol Tls12  

Backup and Restore Database Commands

  BACKUP   DATABASE   [DB_Name]         TO   DISK   =   N'\\server\directory\backupfile.bak'         WITH   NOFORMAT ,   INIT ,   NAME = N'DBNAME' ,   SKIP ,      NOREWIND ,   NOUNLOAD ,   STATS = 10 ;   RESTORE   FILELISTONLY                FROM   DISK =   N '\\server\directory\backupfile.bak'   DROP   DATABASE   [DB_Name]   ;   RESTORE   DATABASE   [DB_Name]             FROM   DISK = ' '\\server\directory\backupfile.bak'         WITH         MOVE   'DB_logicalName'   TO                'D:\Databases\data file name. mdf ' ,         MOVE   ' DB_Log Name _log'   TO                         'E:\Logs\log file name_log.ldf'    

Using GetEnumerator

  The tips are:  Seems to work better if you know the type you want back from GetEnumerator, notice the IEnumerator<Employee> Notice the use of Current

Good article on getting the right characters of a string in C#

Left, Right, SubString C#   https://kodify.net/csharp/strings/left-right-mid/#left-right-and-mid-string-segments-in-c Feature Description C# equivalent Left Get specific number of characters ( count ) from left part string.Substring(0, count) Right Get specific number of characters ( count ) from right side string.Substring(string.Length - count, count) Mid Get a specific number of characters ( count ) starting at a certain point ( index ) string.Substring(index, count) Mid Get all characters starting at a certain point ( index ) string.Substring(index)

Accessing localhost Web API from an android emulator on Windows

 There is a preconfigured IP address, for the emulator to access.  The IP Address is 10.0.2.2 ie: use something like https://10.0.2.2:5001/api/customers You'll end up with something like... private string baseurl = Device.RuntimePlatform == Device.Android ?     " https://10.0.2.2:5001/api" :     " https://localhost:5001/api" ; 

How to trust IIS Certificates for Development

 Type the following on a command line  > dotnet dev-certs https --trust If you're accessing the dev site from another computer or device, via an application, you can disable ssl certificate checking in the client app with... HttpClientHandler handler = new HttpClientHandler(); handler.ServerCertificateCustomValidationCallback = ( message, cert, chain, errors ) =>  { if(cert.Issuer.Equals("CN=localhost"))                    { return true;                     } return errors == System.Net.Security.SslPolicyErrors.None; }; Notes about Android and Xamarin Requires TLS 1.2 The default AndroidClientHandler supports TLS 1.2, but only for Android versions greater than 4.1 If an Android version is needed that is lower than 4.1, then we'll need to use the HttpClientHandler which supports TLS 1.0.  It is slower and increases the package size. Notes about iOS and Xamarin Requires TLS 1.2 The HttpClient uses NSUrlSession, this is the default and supports TL

Managing Windows Credentials with Powershell

1) Download Credman from  https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde It uses Advapi32.dll   2)  To add a Cred, the syntax is  .\CredMan.ps1 -AddCred -Target 'SERVER' -User 'domain\xxxxxx' -Pass 'xxxxxxxx' -CredType 'DOMAIN_PASSWORD' Notes 'SERVER' can include the single hostname or FQDN plus the desired port   

Regex - Dealing with Groups, C#

Just say you have a string like this one.... Job Posting: 10 Jul 2020 - Closing Date: 26 Jul 2020 Land Administration Legal and Justice Regulatory and Compliance Central &amp; North Coasts Job Title: Senior Analyst Native Title Job Grade / Classification: Clerk Grade 9/10 Employment Type: Temporary role for 24 months Location: Negotia...   and you want to insert a new line after the date, 26 Jul 2020.  Details = " Job Posting: 10 Jul 2020 - Closing Date: 26 Jul 2020 Land Administration Legal and Justice Regulatory and Compliance Central &amp; North Coasts Job Title: Senior Analyst Native Title Job Grade / Classification: Clerk Grade 9/10 Employment Type: Temporary role for 24 months Location: Negotia...   ";    Regex r1 = new Regex( @"(.*Closing Date: \d\d [A-Za-z]* \d\d\d\d)(.*)" ); Match match = r1.Match(Details); if (match.Success && match.Groups.Count == 3 ) { j.JobDates = match.Groups[ 1 ].Value.Trim(); j.Details = match.Gro

Ports Used by MSSQL

https://docs.microsoft.com/en-us/sql/sql-server/install/configure-the-windows-firewall-to-allow-sql-server-access?view=sql-server-ver15 Ports Used By the Database Engine By default, the typical ports used by SQL Server and associated database engine services are: TCP  1433 ,  4022 ,  135 ,  1434 , UDP  1434 Ports Used By Integration Services The Integration Services service uses DCOM on port 135 Ports Used By Analysis Services By default, the typical ports used by SQL Server Analysis Services and associated services are: TCP  2382 ,  2383 ,  80 ,  443 Ports Used By Reporting Services By default, the typical ports used by SQL Server Reporting SErvices and associated services are: TCP  80 ,  443

SQL Merge

When writing an SQL Merge Statement, I wanted to avoid updating rows that hadn't changed. The trick is to use EXISTS and EXCEPT The page at http://www.made2mentor.com/2013/05/writing-t-sql-merge-statements-the-right-way/ explains this well There's also a reference to Paul White's page https://www.sql.kiwi/ MERGE #Customer_New AS Target USING #Customer_Orig AS Source ON Target. CustomerNum = Source. CustomerNum WHEN MATCHED AND EXISTS ( SELECT Source. CustomerName , Source. Planet EXCEPT SELECT Target. CustomerName , Target. Planet ) THEN UPDATE SET Target. CustomerName = Source. CustomerName ,Target. Planet = Source. Planet WHEN NOT MATCHED BY TARGET THEN INSERT ( CustomerNum, CustomerName, Planet ) VALUES ( CustomerNum, Source. CustomerName , Source. Planet ) WHEN NOT MATCHED BY SOURCE THEN DELETE ;

Restrict Time Machine Backup to a certain size

From  https://www.defaults-write.com/setup-a-size-limit-for-time-machine-backup-volumes/ Setup a size limit for Time Machine backup volumes October 22, 2012   by  Mr.R Time Machine will standard use all the available disk space on an external drive or a external server volume. You can setup a limit how much disk space Time Machine may use. Use this defaults write Terminal command: defaults write /Library/Preferences/com.apple.TimeMachine MaxSize 405600 Where MaxSize 405600 is the MB equivalent of 400GB (400 * 1024) To remove the limit: defaults delete /Library/Preferences/com.apple.TimeMachine MaxSize

How to Modify With of Solution Configurations Window in Visual Studio

Select the Standard Toolbar options pulldown and select "Add or Remove Buttons" Select Customize On the Customise Dialog box, select the Commands tab Select the Solutions Configurations item, the one you want to change the width of Select "Modify Selection" Enter the new width, and select OK

Implementing Impersonation in C#

How to implement impersonation in an ASP.NET application https://support.microsoft.com/en-us/help/306158/how-to-implement-impersonation-in-an-asp-net-application ASP.Net Security Overview https://support.microsoft.com/en-us/help/306590/info-asp-net-security-overview

Starting an async task in C# 6 from Main

To start an async method from Main, do something like static int Main ( ) { return DoAsyncWork().GetAwaiter().GetResult(); } As of C# 7.1, can you also write static async Task< int > Main ( ) { // This could also be replaced with the body     // DoAsyncWork, including its await expressions:     return await DoAsyncWork(); } or the following if Main returns void static async Task Main ( ) { await SomeAsyncMethod(); }

POCO and Entity Framework

https://www.red-gate.com/simple-talk/dotnet/net-framework/using-entity-framework-with-an-existing-database-data-access/ https://www.red-gate.com/simple-talk/dotnet/net-framework/using-entity-framework-with-an-existing-database-data-access/