Chủ Nhật, 23 tháng 12, 2018

[C#] Mapping Network Drive

using IWshRuntimeLibrary;

string drive = ConfigurationManager.AppSettings["Drive"];
string folder = ConfigurationManager.AppSettings["Folder"];
string userName = ConfigurationManager.AppSettings["Username"];
string password = ConfigurationManager.AppSettings["Password"];

IWshNetwork_Class network = new IWshNetwork_Class();

try
{
 network.RemoveNetworkDrive(drive);
}
catch (Exception ex)
{

}

try
{
 network.MapNetworkDrive(drive, folder, Type.Missing, userName, password);
}
catch (Exception ex)
{

}

Thứ Sáu, 21 tháng 12, 2018

[C#] Open file without locking

var stream = File.Open(filePath, FileMode.Open,
 FileAccess.Read,
 FileShare.ReadWrite);

Thứ Năm, 4 tháng 10, 2018

[SQL] Explore Stored Procedures Cached Plans


SELECT OBJECT_NAME([ps].[object_id], [ps].[database_id]) 
            AS [ProcedureName]
 , [ps].[execution_count] AS [ProcedureExecutes]
 , [qs].[plan_generation_num] AS [VersionOfPlan]
 , [qs].[execution_count] AS [ExecutionsOfCurrentPlan]
 , SUBSTRING ([st].[text], 
  ([qs].[statement_start_offset] / 2) + 1, 
     ((CASE [statement_end_offset] 
   WHEN -1 THEN DATALENGTH ([st].[text]) 
      ELSE [qs].[statement_end_offset] END 
   - [qs].[statement_start_offset]) / 2) + 1) 
      AS [StatementText]
    , [qs].[statement_start_offset] AS [offset]
    , [qs].[statement_end_offset] AS [offset_end]
    , [qp].[query_plan] AS [Query Plan XML]
    , [qs].[query_hash] AS [Query Fingerprint]
    , [qs].[query_plan_hash] AS [Query Plan Fingerprint]
FROM [sys].[dm_exec_procedure_stats] AS [ps]
JOIN [sys].[dm_exec_query_stats] AS [qs] ON [ps].[plan_handle] = [qs].[plan_handle]
CROSS APPLY [sys].[dm_exec_query_plan] ([qs].[plan_handle]) AS [qp]
CROSS APPLY [sys].[dm_exec_sql_text] ([qs].[sql_handle]) AS [st]
WHERE [ps].[database_id] = DB_ID()
ORDER BY [ProcedureName], [qs].[statement_start_offset];

Thứ Hai, 17 tháng 9, 2018

[SQL] Search Columns By Data Types


select tb.name as table_name, cl.name as column_name
, type.name as type_name
, cl.max_length
from sys.columns cl
join sys.tables tb on cl.object_id = tb.object_id
join sys.types type on cl.user_type_id = type.user_type_id
where type.name in ('date', 'datetime', 'datetime2')
order by tb.name, cl.name

Thứ Bảy, 15 tháng 9, 2018

Thứ Sáu, 14 tháng 9, 2018

[SQL] Estimate Data Compression Savings

EXEC sp_estimate_data_compression_savings 
    @schema_name = 'dbo', 
    @object_name = 'TableName', 
    @index_id = NULL, 
    @partition_number = NULL, 
    @data_compression = 'ROW'
GO

EXEC sp_estimate_data_compression_savings 
    @schema_name = 'dbo', 
    @object_name = 'TableName', 
    @index_id = NULL, 
    @partition_number = NULL, 
    @data_compression = 'PAGE'
GO