Skip to content

Instantly share code, notes, and snippets.

@RickyLin
RickyLin / Program.cs
Last active March 27, 2023 03:24
SimpleProgramForTryDotNet
using System;
using System.Linq;
namespace SampleTest
{
public class Program
{
public static void Main()
{
@RickyLin
RickyLin / IPAddressesOfLoginFailure.sql
Last active January 30, 2024 01:09
Get IP addresses that lead to SQL Server login failure.
DECLARE @LogIndex INT
DECLARE @LastCheckDate DATETIME
SET @LogIndex = 0
SET @LastCheckDate = '2023-12-21'
DECLARE @Logs TABLE
(
LogDate DATETIME,
ProcessInfo NVARCHAR(4000),
@RickyLin
RickyLin / ReplaceDiacritics.cs
Created February 7, 2023 03:07
Converts characters above ASCII to their ASCII equivalents. For example, accents are removed from accented characters.
/// <summary>
/// Converts characters above ASCII to their ASCII equivalents. For example, accents are removed from accented characters.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ReplaceDiacritics(this string str)
{
/* based on code here:
* https://github.com/apache/lucenenet/blob/master/src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/ASCIIFoldingFilter.cs
*/
@RickyLin
RickyLin / READ_COMMITTED_SNAPSHOT.sql
Last active May 26, 2022 03:18
Set READ_COMMITTED_SNAPSHOT on for a database.
ALTER DATABASE DatabaseName SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE DatabaseName SET READ_COMMITTED_SNAPSHOT ON
ALTER DATABASE DatabaseName SET ONLINE
/* Generate sql scripts for multiple databases.
SELECT 'ALTER DATABASE ' + [name] + ' SET OFFLINE WITH ROLLBACK IMMEDIATE;
ALTER DATABASE ' + [name] + ' SET READ_COMMITTED_SNAPSHOT ON;
ALTER DATABASE ' + [name] + ' SET ONLINE;
'
FROM sys.databases
@RickyLin
RickyLin / EnrichExceptionDataDbCommandInterceptor.cs
Last active April 22, 2022 05:10
Append command text and parameters to Exception.Data property when exceptions are thrown from Entity Framework Core.
/*
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
*/
public class EnrichExceptionDataDbCommandInterceptor : DbCommandInterceptor
@RickyLin
RickyLin / CurrentThreadTaskScheduler.cs
Created March 18, 2022 14:55
CurrentThreadTaskScheduler
/* The CurrentThreadTaskScheduler code comes from
* https://github.com/dotnet/samples/blob/main/csharp/parallel/ParallelExtensionsExtras/TaskSchedulers/CurrentThreadTaskScheduler.cs
*/
/// <summary>
/// Provides a task scheduler that runs tasks on the current thread.
/// </summary>
public sealed class CurrentThreadTaskScheduler : TaskScheduler
{
/// <summary>Runs the provided Task synchronously on the current thread.</summary>
/// <param name="task">The task to be executed.</param>
@RickyLin
RickyLin / HtmlEncodeDecode.js
Created November 22, 2021 12:27
HtmlEncode and HtmlDecode in Javascript
/* source: https://stackoverflow.com/a/7124052/2753545 */
function htmlEscape(str) {
return str
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\//g, '&#x2F;');
@RickyLin
RickyLin / GetDefaultConstraintName.sql
Created August 30, 2021 08:19
Get default constraint name of a column
SELECT T.name AS TableName, A.name AS ColumnName, d.name AS ConstraintName
FROM sys.default_constraints D
INNER JOIN sys.all_columns A ON A.default_object_id = D.object_id
INNER JOIN sys.tables T ON T.object_id = A.object_id
WHERE T.name = @TableName AND A.name = @ColumnName
@RickyLin
RickyLin / spDuplicateEntryWithIdentityValue.sql
Created April 17, 2021 08:52
Generate and run "insert into ... select ... from" statement for a table.
CREATE PROCEDURE [dbo].[spDuplicateEntryWithIdentityValue]
@TableName NVARCHAR(128),
@IdentityColumnName NVARCHAR(128), -- the name of auto-increasing column
@CurrentIdentityValue INT,
@NewIdentityValue INT OUTPUT
AS
DECLARE @ColumnNames NVARCHAR(MAX)
SELECT @ColumnNames = STRING_AGG(N'[' + COLUMN_NAME + N']', N', ')
FROM INFORMATION_SCHEMA.COLUMNS
@RickyLin
RickyLin / fnGetDateRange.sql
Last active April 17, 2021 08:46
Get date range of a week, a month or a year that contains a date.
CREATE FUNCTION [dbo].[fnGetDateRange]
(
@Now DATETIME,
@FrequencyId INT
)
RETURNS @Result TABLE
(
StartDate DATE,
EndDate DATE
)