Skip to content

Instantly share code, notes, and snippets.

View juanonsoftware's full-sized avatar

Juan on Software juanonsoftware

  • Rabbit Software
  • Vietnam
View GitHub Profile
@juanonsoftware
juanonsoftware / gist:7067ce53813201abbdae
Last active August 29, 2015 14:23
C# Convert null property to Empty JSON object by property's type
namespace JsonConvertNullToEmpty
{
public class Main
{
public Child First
{
get;
set;
}
@juanonsoftware
juanonsoftware / gist:b55bcf15923f3f9cd2f4
Created June 29, 2015 09:40
C# LINQ with join on multiple properties
namespace ConsoleApplication1
{
public class Context
{
public IList<Product> Products { get; set; }
public IList<Company> Companies { get; set; }
}
public class Product
{
@juanonsoftware
juanonsoftware / gist:103fec4fa9c5b9e2ab14
Last active January 26, 2022 17:02
Displaying image from webapi using javascript base64
WebAPI
// GET api/values
public dynamic Get()
{
var root = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
var path = Path.Combine(root, "App_Data/Koala.jpg");
var bytes = File.ReadAllBytes(path);
var base64 = Convert.ToBase64String(bytes);
@juanonsoftware
juanonsoftware / gist:f4b3e85415ccdcab2161
Created July 3, 2015 16:04
NHiberate - Code to make SchemaExport generate database for entities mapping class
using System;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Tool.hbm2ddl;
namespace ConsoleApplication1
{
@juanonsoftware
juanonsoftware / sql-get-all-locks.sql
Last active January 26, 2022 17:33
A script to list all current locks in SQL server
-- With SQL
select handle.text as sql, * from
(
select DB_NAME(dbid) as dbname, *
from master.dbo.sysprocesses
where blocked <> 0
union
@juanonsoftware
juanonsoftware / Kill-sql-locks
Created April 12, 2018 09:43
Kill blocking processes on a database
--- Kill blocked processes on a database
DECLARE @databasename nvarchar(50)
SET @databasename = N'Datbase_Name'
-- SET @databasename = DB_NAME()
DECLARE @Sql varchar(max)
SET @Sql = ''
SELECT @Sql = @Sql + 'Kill ' + Convert(varchar, SPId) + ';'
FROM master.dbo.sysprocesses
@juanonsoftware
juanonsoftware / sql-backup-database-diff
Last active April 12, 2018 09:52
Script to backup an SQL Server database
-- NOTE
-- Must update dbName & Folder
declare @dbName nvarchar(150);
declare @folder nvarchar(100);
set @dbName = 'TestDB';
set @folder = 'D:\Mega\Wip\SQL\TestDB\Backup\';
declare @path nvarchar(255);
@juanonsoftware
juanonsoftware / sql-check-index-fragmentation.sql
Last active January 26, 2022 17:32
Check Fragmentation of ALL Indexes in a Database
-- https://myadventuresincoding.wordpress.com/2013/05/27/sql-server-check-index-fragmentation-on-all-indexes-in-a-database/
SELECT dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.alloc_unit_type_desc,
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
@juanonsoftware
juanonsoftware / sql-get-running-jobs.sql
Last active April 12, 2018 10:00
To get all jobs that are running on an instance
SELECT
ja.job_id,
j.name AS job_name,
ja.start_execution_date,
ISNULL(last_executed_step_id,0)+1 AS current_executed_step_id,
Js.step_name
FROM msdb.dbo.sysjobactivity ja
LEFT JOIN msdb.dbo.sysjobhistory jh
ON ja.job_history_id = jh.instance_id
JOIN msdb.dbo.sysjobs j
@juanonsoftware
juanonsoftware / sql-get-db-backups.sql
Last active April 12, 2018 10:00
Script to get all backup of a SQL Server database
declare @dbName varchar(255);
set @dbName = 'TestDB';
SELECT m.physical_device_name,
b.backup_start_date,
b.backup_finish_date,
b.type,
b.backup_size/1024/1024 AS BackupSizeMB,
b.compressed_backup_size/1024/1024 as CompressedBackupSizeMB