Archive for the ‘Database’ Category.

D LINQ : How to Map Columns Which Auto Generate Values At the Database

2009-05-11

I have being working on a software application made on .Net and recently my client asked me to use D LINQ instead of SQL.

D LINQ has great benefits loaded! As I started working with DLINQ I started to know that preventing SQL injection is not a headache anymore and misspelled SQL queries will not trouble agian at run time because DLINQ generates all necessary SQL inside the framework!

I chose to use annotations inside the class instead of using separate xml file. Following is a part of the first class I ported to D LINQ.

</p>

<table(Name:="tbl_Process")> _
Public Class Process
<column(Name:="ProcessID", DbType:="Int", isprimarykey:=True)> _
Public ProcessID As Integer
<column(Name:="BusinessProcessID", DbType:="varchar(50)")> _
Public BusinessProcessID As String
<column(Name:="ProcessText", DbType:="varchar(50)", isprimarykey:=True)> _
Public ProcessText As String
...
...

ProcessID column is the primary key of my database table tbl_Process.

Important Point: I use database to auto-generate values for the primary key column (integer value incremented by one).

So when I run the application, It gave me this unexpected error:

Cannot insert explicit value for identity column in table ‘tbl_Process’ when IDENTITY_INSERT is set to OFF.

Basically, the IDENTITY_INSERT when using the database to auto generate value for a field but when I ran a SQL insert statement at the database end it worked fine!

After some tough time I found out the solution for this problem!

When we use an auto-generate field in a data table we have to specifically mention it in the matching field in the appropriate class.

The code to state this is:

Syntax:

IsDbGenerated:=True

This has to be added to the annotation added for the specific column like:

</p>

<column(Name:="ProcessID", DbType:="Int", isprimarykey:=True, IsDbGenerated:=True)> _
Public ProcessID As Integer

Now D LINQ can understand that the field value is auto-generated by the database!

Was this post helpful to you? How can I improve? – Your comment is highly appreciated!

Cassian Menol Razeek

How to (Full and Transaction Log) backup and restore a database in Microsoft SQL Server 2005

2008-12-04

In this article I will discus how to backup a database as a FULL backup or with Transaction Log (Tail Log) backup and how to restore the database, how to recover transactions made after you taking the backup, etc.

Since it is not easy to use and organize images in the post itself I have uploaded a compiled pdf file of this article.

Please download the complete manual from following link:

How to backup and restore a database in Microsoft SQL Server 2005

Was this post helpful to you? How can I improve? – Your comment is highly appreciated!

Cassian Menol Razeek

Oracle Trunc Function

2008-11-24

Among various helpful functions provided by Oracle, Trunc function took my attention today because I had to fix a defect where some one has misunderstood and misused this function.

As the name itself suggests, the trunc function is capable of truncating a value. This function can be used either on a date type value or a numeric value.

Using trunc for numeric values

When it comes to numbers, trunc function can return a number truncated to a certain number of decimal places.

syntax:

trunc( number, [ decimal_places ] )

Example usage:

trunc(125.815) would return 125
trunc(125.815, 0) would return 125
trunc(125.815, 1) would return 125.8
trunc(125.815, 2) would return 125.81
trunc(125.815, 3) would return 125.815
trunc(-125.815, 2) would return -125.81
trunc(125.815, -1) would return 120
trunc(125.815, -2) would return 100
trunc(125.815, -3) would return 0

Menol

Note: The trunc function does not round the values like in round function. It simply truncates the number as instructed.

Using trunc for date values

The trunc function is capable to truncate a date value to a specific unit of measure.

Syntax:

trunc ( date, [ format ] )

Possible values for format parameter:

Unit Valid format parameters
Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y
ISO Year IYYY, IY, I
Quarter Q
Month MONTH, MON, MM, RM
Week WW
IW IW
W W
Day DDD, DD, J
Start day of the week DAY, DY, D
Hour HH, HH12, HH24
Minute MI

Menol

Example Usage:

trunc(to_date(‘22-AUG-03′), ‘YEAR’) would return ‘01-JAN-03′
trunc(to_date(‘22-AUG-03′), ‘Q’) would return ‘01-JUL-03′
trunc(to_date(‘22-AUG-03′), ‘MONTH’) would return ‘01-AUG-03′
trunc(to_date(‘22-AUG-03′), ‘DDD’) would return ‘22-AUG-03′
trunc(to_date(‘22-AUG-03′), ‘DAY’) would return ‘17-AUG-03′

Menol

Was this post helpful to you? How can I improve? – Your comment is highly appreciated!

Cassian Menol Razeek