Handy C#, VB.Net & SQL Code Snippets

Date time – how to initialize a date


DateTime value = new DateTime(2017, 1, 18);

Concatenate string list into comma separated string

 List<string> message = new List<string>();
      message.Add("Record exists");
      message.Add("Invalid data format");
      message.Add("Duplicated record");
      string output = String.Join(", ", message.Where( m=>
                      (m.Length != 0)));
      Console.WriteLine(output);  

Initialize a string list


using System;
using System.Collections.Generic;

 List<string> message = new List<string>()
     {
         "Record exists",
         "Invalid data format",
         "Duplicated record"
     };    

Save text to a file

Private Function SafeFile(text As String,folderPath as string) As Boolean

     Dim fileName as String = System.IO.Path.Combine(folderPath ,FileName)
     if System.IO.File.Exists(fileName) then return False
     file = My.Computer.FileSystem.OpenTextFileWriter(fileName, True)
     file.WriteLine(text )
     file.Close()
     Return True

End Function

What was the last thing I created in that database?

Open the database using Microsoft SQL Server Management Studio, create a query for the database and a

SELECT name, create_date, modify_date
FROM sys.objects
order by Create_date desc

You can filter by type:

AF = Aggregate function (CLR)
C = CHECK constraint
D = DEFAULT (constraint or stand-alone)
F = FOREIGN KEY constraint
FN = SQL scalar function
FS = Assembly (CLR) scalar-function
FT = Assembly (CLR) table-valued function
IF = SQL inline table-valued function
IT = Internal table
P = SQL Stored Procedure
PC = Assembly (CLR) stored-procedure
PG = Plan guide
PK = PRIMARY KEY constraint
R = Rule (old-style, stand-alone)
RF = Replication-filter-procedure
S = System base table
SN = Synonym
SO = Sequence object

Applies to: SQL Server 2012 through SQL Server 2016.

SQ = Service queue
TA = Assembly (CLR) DML trigger
TF = SQL table-valued-function
TR = SQL DML trigger
TT = Table type
U = Table (user-defined)
UQ = UNIQUE constraint
V = View
X = Extended stored procedure

Applies to: SQL Server 2016 through SQL Server 2016, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse.

ET = External Table

source https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-objects-transact-sql

SQL Server, Where is that column located?

   SELECT      COLUMN_NAME as ThisColumn,TABLE_NAME as TheTableName,*
   FROM        INFORMATION_SCHEMA.COLUMNS
   WHERE       COLUMN_NAME LIKE 'ColumnYouAreSearchingFor'

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s