Wednesday, August 20, 2014

Disposable Method in C#

Problem:
The memory would not release if you not call the dispose method.

Several Type need to disposable:
  • File handles
  • Network sockets
  • Database connections
  • Unmanaged memory
2 way to dispose object:

1.     Call Dispose method

Need to call dispose method in Try….Finally, because need to dispose after handling the exception. The code will look ugly because have quite Try… Finally method if you have several object need to dispose.

2.     Using

It’s included call dispose method in Try….Finally, so you no need to call few time of Try… Finally, and your code look clean and you would not forget to dispose it.

Conclusion:
You have to do call dispose method in every Idisposable object and Using is more recommended. 

http://coding.abel.nu/2011/12/idisposable-and-using-in-c/

SQL vs C# DataType

SQL Data Type
.NET Data Type
bit
bool
tinyint
byte
smallint
Int16 or short
int
int or Int32
bigint
long or Int64
decimal
decimal
numeric
decimal
datetime
Datetime
char, nchar
char
varchar, nvarchar, text, ntext
string
float
double
real
single
uniqueidentifier
Guid
Binary, varbinary
byte[]

Tuesday, August 12, 2014

Add a temproray table in SQL

SELECT *
INTO #TempTable

FROM Student

Way to do SQL without insert DB

DECLARE @NAME VARCHAR(50)
SET @NAME= 'abc01';

SELECT CHARINDEX('abc', @NAME)

SELECT * FROM Student

WHERE CHARINDEX(@NAME, Name) > 0

Tuesday, August 5, 2014

Clone another set Database

There are 2 method to clone another set Database in SQL Server :

  1. Back up and Restore ( Local )
  • Back up - Tasks (Backup) >  (Destination) Add , choose file or write file (.bak)
  • Restore - Tasks(Restore Database ) >  Device : Add File 

     2.  Script ( Server )
  • Tasks > generate script 
  •  better dont those db , choose table , no user
  •  Set Scripting Option - advanced (schema & data) , index - true

if database is so big , and just want few tables have data.

Import Data, choose source 1st, choose destination 2nd, choose table. It will create tables as well.
Run "Script", already generated will be ignore.

Detail Info
http://www.dispatchertimer.com/sql-server/how-to-generate-scripts-automatically-in-sql-server/#_Toc375325158