Skip to content

Instantly share code, notes, and snippets.

@vendettamit
vendettamit / UpdateBulk.cs
Created December 4, 2019 22:14
Update All fields of a document in MongoDB using BuildWrite
public async Task<long> UpdateBulk<T>(IMongoCollection<T> _collection, IEnumerable<T> items, FilterDefinition<T> filter, params string[] propertiesToSkip)
{
if (!items.Any())
{
return 0;
}
var updates = new List<WriteModel<T>>();
var filterBuilder = Builders<T>.Filter;
var updater = Builders<T>.Update;
@vendettamit
vendettamit / StringOrBindaryDataTruncated_Linqpad.cs
Created December 3, 2019 19:08
Utility method to detect long string and targeted column that's causing exception "String or binary data would be truncated"
public static void FindLongStrings(object testObject)
{
foreach (FieldInfo propInfo in testObject.GetType().GetFields())
{
foreach (ColumnAttribute attribute in propInfo.GetCustomAttributes(typeof(ColumnAttribute), true))
{
if (attribute.DbType.ToLower().Contains("varchar"))
{
string dbType = attribute.DbType.ToLower();
int numberStartIndex = dbType.IndexOf("varchar(") + 8;
//Escape special characters in a string to use in batch file
var arg = "^hU$xgjX4ku*Hc0p%#F^UH";
arg.replace(/([\(\)%!\^<>|;, ])/g,'^$1').replace(/&/g, '&&');
console.log(arg);
@vendettamit
vendettamit / GetProcResultInTable.sql
Last active June 13, 2019 17:57
Get any procedure result in temp table without using OPENROWSET or OPENQUERY
create table #d
(is_hidden bit NULL, column_ordinal int NULL, name sysname NULL, is_nullable bit NULL, system_type_id int NULL, system_type_name nvarchar(256) NULL,
max_length smallint NULL, precision tinyint NULL, scale tinyint NULL, collation_name sysname NULL, user_type_id int NULL, user_type_database sysname NULL,
user_type_schema sysname NULL,user_type_name sysname NULL,assembly_qualified_type_name nvarchar(4000),xml_collection_id int NULL,xml_collection_database sysname NULL,
xml_collection_schema sysname NULL,xml_collection_name sysname NULL,is_xml_document bit NULL,is_case_sensitive bit NULL,is_fixed_length_clr_type bit NULL,
source_server sysname NULL,source_database sysname NULL,source_schema sysname NULL,source_table sysname NULL,source_column sysname NULL,is_identity_column bit NULL,
is_part_of_unique_key bit NULL,is_updateable bit NULL,is_computed_column bit NULL,is_sparse_column_set bit NULL,ordinal_in_order_by_list smallint NULL,
order_by_list_length smallint NULL,order_by_is_desce
@vendettamit
vendettamit / SqlfunctionSplitIndexed.Sql
Created January 3, 2019 17:24
Split string with Index number
CREATE FUNCTION [dbo].[fnSplitIndexed](
@sInputList VARCHAR(8000) -- List of delimited items
, @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (idx integer, item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
Declare @idx integer = 0
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
public interface IUnitOfWork<TContext> where TContext: DbContext, new()
{
void Commit();
}
public interface IDbFactory<TContext> : IDisposable where TContext: DbContext, new()
{
TContext Init();
}
@vendettamit
vendettamit / min_max.cs
Created August 15, 2017 17:21
Get minimum or maximum of two number without using conditional statement. Max(a,b) MIN(a,b) implementation in CSharp/C#
int max(int a, int b)
{
/* max(a,b) = 1/2(a + b + |a-b|) */
Func<int, int> modValue = x => (int)Math.Sqrt(Math.Pow(x, 2));
return (int)Math.Ceiling((double)(a + b)/2 + ((double)modValue(a - b))/2);
}
int min(int a, int b)
{
@vendettamit
vendettamit / OnEventCustomTask.ps1
Created January 13, 2017 22:13
Powershell script to run when an event occurs in Windows event log
# To test this script you can use Powershell to write your own test error log entry in the following way:
# -------------------------------------
# New-EventLog –LogName Application –Source "Test"
# Write-EventLog –LogName Application –Source "Test" –EntryType Error –EventID 1 –Message "This is a test message."
# Name to filter the event uses wild card pattern matching
$a = "*LocalReport*Application*"
#Write-EventLog –LogName Application -Source "Test" –EntryType Error –EventID 1046 –Message "This is a test message for $($a)."
@vendettamit
vendettamit / CreateEventTask.ps1
Last active January 13, 2017 22:09
Create Event based task to run a custom powershell script on Windows Event
# Create a task to run powershell script on event
$cred = Get-Credential
$password = $cred.GetNetworkCredential().Password
$taskName = "EventMonitoringService"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden "C:\CourtAlert\Scripts\TakeAction.ps1""'
#Remove existing tasks
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
@vendettamit
vendettamit / CheckStatusOfWinServices.ps1
Created January 13, 2017 22:07
Check windows service status and email if stopped using power shell script
$client = "Test"
$service=""
$EmailBody = ""
$EmailSubject = ""
$SMTPServer = "relay2"
$PCName = $env:COMPUTERNAME
$EmailFrom = "$client <Test@cshandler.com>"
$EmailTo = "achoudhary@courtalert.com"