StatCounter

View My Stats

Tuesday, February 5, 2008

C#.NET Coding Performance

While Converting string to Integer Consider the Following things.

For Example u r reading a data from SQL datareader and convert it into integer format


while(SQldr.Read())
{
int a=Int.parse(SQLdr[0].toString());
int b=(int)SQLdr[0];
}


For Example Considering the above coding the later one gave a good performance.
int b=(int)SQLdr[0];
By comparing it into the first one the first one first convert the SQL datareader data
into String.And then only the the string is converted into integer.But the second one is not like that.
So the second one increase the performance

Dont use SP_ Prefix while creating the StoredProcedure-SQL Server

Reason:

Dont use sp_ prefix while creating the Storedprocedure.Because it degrades the system performance.while creating like this it first scan the master database...After that only it returns to check our concern database.

To Select IDENTITY column From Table-SQL Server

The query as Follows:
SELECT IDENTITYCOL FROM TableName

Purpose of SET NO COUNT ON inside the stored Procedure-SQL Server

If u dont use SET NO COUNT ON inside the stored procedure the DONE_IN_PROC Message each time send to the caller..For the Concern insert,delete,update operation taken.

This is the Performance issue.In order to overcome the use
SET NO COUNT ON inside the Stored Procedure

Create Transaction Inside the Stored Procedure-SQL

The Query as Follows:

CREATE TABLE A(ID1 INT IDENTITY,VALUE INT CONSTRAINT PK_A PRIMARY KEY NOT NULL)

CREATE TABLE B(ID2 INT IDENTITY,VALUE2 INT CONSTRAINT PK_B PRIMARY KEY NOT NULL)

CREATE TABLE C(ID3 INT IDENTITY,VALUE3 INT CONSTRAINT PK_C PRIMARY KEY NOT NULL)

Procedure:
CREATE PROCEDURE VALUE_INSERT
(
@FIRST INT=NULL,
@SECOND INT=NULL,
@THIRD INT=NULL)
AS
SET NOCOUNT ON
BEGIN TRAN T1
INSERT INTO A VALUES(@FIRST)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
INSERT INTO B VALUES(@SECOND)

IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
INSERT INTO C VALUES(@THIRD)

IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
COMMIT TRAN T1
For the above example I am creating 3 tables. If anywhere the primary key violation occurs then the whole transaction will be Roll backed.

Executing the Procedure:
VALUE_INSERT 1,1,1

VALUE_INSERT-Procedure Name


For the above sp the identity state changed.

In order to create a sequential identity insert u have manually insert the identity value by setting the identity_insert on to the concern table

CREATE PROCEDURE INSERT_VALUE
(
@FIRST INT=NULL,
@SECOND INT=NULL,
@THIRD INT=NULL)
AS
SET NOCOUNT ON
DECLARE @ID1 INT
DECLARE @ID2 INT
DECLARE @ID3 INT
SELECT @ID1=COUNT_BIG(ID1)+1 FROM A
SELECT @ID2=COUNT_BIG(ID2)+1 FROM B
SELECT @ID3=COUNT_BIG(ID3)+1 FROM C BEGIN TRAN T1 BEGIN SET IDENTITY_INSERT A ON
INSERT INTO A(ID1,VALUE) VALUES(@ID1,@FIRST)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT A OFF
END

BEGIN
SET IDENTITY_INSERT B ON
INSERT INTO B(ID2,VALUE2) VALUES(@ID2,@SECOND)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT B OFF
END
BEGIN
SET IDENTITY_INSERT C ON
INSERT INTO C(ID3,VALUE3) VALUES(@ID3,@THIRD)
IF(@@ERROR!=0)
BEGIN
ROLLBACK TRAN T1
RETURN
END
SET IDENTITY_INSERT C OFF
END
COMMIT TRAN T1

Types Of Cursor-SQL Server

1.Insensitive Cursor
2.Scroll Cursor
3.Forward-Only
4.Static Cursor
5.Keyset Cursor
6.Dynamic Cursor


1.Insensitive Cursor:
Insensitive cursor Example without using the looping structure

select * from table1

declare cursor_table1 insensitive cursor for select name1,salary from table1//Cursor declaration

create procedure p_table1 as
declare @name1 varchar(50)declare @salary integer
open cursor_table1 fetch next from cursor_table1 into @name1,@salary print @name1 print @salaryclose cursor_table1


exec p_table1

2.Scroll Cursor:

In This scroll cursor we can have more options method compared to insensitive cursor.
AN insensitive cursor have just a
1.Fetch next statement

But for Considering the scroll cursor have more as indicated below
1.fetch next
2.fetch first
3.fetch last
4.fetch Relative
5.fetch absolute

One important things is we cant able to update the cursor


simple example if the scroll cursor declaration as given below


declare cursor_table3 scroll cursor for select name1,salary from table1


3.Forward only cursor:

This forword only cursor is nothing but the fetching of records are moved from
first to the last.

It support fetch next function

Each time the fetch status is evaluated is (Under this type of situation we are using
Static and keyset cursor)


Delete a Cursor:

Deallocate CursorName

Inner,Outer,Cross Join-SQL Server

create table table11(id integer identity,name varchar(50),address varchar(50))

insert into table11 values('d','coimbatore')
select * from table11

create table table22(id integer identity,name varchar(50),salary integer)
insert into table22 values('q',3000)

select * from table22

outer join three types they are left,right,full outer join

Left Join:
select table11.name,table22.name from table11 left join table22 on table11.name=table22.name

Right join:
select table11.name,table11.address ,table22.name from table11 right join table22 on table11.name=table22.name

Full Outer join:
select table11.name,table11.address,table22.name,table22.salary from table11 full outer join table22 on table11.name=table22.name

// inner join it display only matched items
select table11.name ,table22.name from table11 inner join table22 on table11.name=table22.name

//cross join 0,0 0,1 1,0 1,1

select table11.name ,table22.name ,table11.id,table22.id,table11.address,table22.salary from table11 cross join table22