Skip to content

Instantly share code, notes, and snippets.

@RickyLin
RickyLin / spGenerateEntity.sql
Last active February 28, 2024 06:51
spGenerateEntity
ALTER PROCEDURE [dbo].[spGenerateEntity]
@TableName AS NVARCHAR(128),
@NewDateTypes AS BIT = 0
AS
SELECT CASE WHEN COLUMN_NAME = TABLE_NAME + 'Id' THEN '[Column("' + COLUMN_NAME + '")]
public ' ELSE 'public ' END
+ CASE DATA_TYPE WHEN 'int' THEN 'int'
WHEN 'money' THEN 'decimal'
WHEN 'varchar' THEN 'string'
WHEN 'datetime' THEN 'DateTime'
@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 / StopAppPool.ps1
Created January 28, 2018 11:18
Powershell script to stop an IIS Application Pool
# usage: StopAppPool.ps1 -AppPoolName DefaultAppPool
# set-executionpolicy remotesigned
# Document of WebAdministration:
# https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/ee790599(v%3dtechnet.10)
# multiple parameters sample: param([string]$paramA,[string]$paramB)
param([string]$AppPoolName)
if ([string]::IsNullOrEmpty($AppPoolName))
{
@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 / 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 / IQueryableExtension.cs
Last active June 28, 2022 07:25
Extension methods on Queryable/IQueryable
using System;
using System.Linq.Expressions;
using System.Linq;
using System.Collections.Generic;
namespace Rvc.Utilities
{
// refer to System.Web.Query.Dynamic namespace
public static class IQueryableExtensions
{
@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;');