Saturday, January 3, 2009

Array


Determine if a string in an array
string[] x = new string[] {"apple", "banana", "pear"};

if ( ((IList)x).Contains ("apple"))
{

}

To find the index of a given element in an array
int index;
string[] theList = { "lion", "turtle", "ostrich" };
index = Array.IndexOf(theList, "turtle");

Monday, December 29, 2008

Truncate all table in a database

/*Disable Constraints & Triggers*/

exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'

exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'


/*Perform delete operation on all table for cleanup*/

exec sp_MSforeachtable 'DELETE ?'


/*Enable Constraints & Triggers again*/

exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'

/*Reset Identity on tables with identity column*/

exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''),

''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'

Call Validators form javascript


We can able to call the Validator controls form the javascript function
if needed

ValidatorEnable(document.getElementById ('reqItem'), true);

reqItem is the ID of the validator control.

www.codeproject.com

Wednesday, December 24, 2008

lord-krishna

http://www.authorstream.com/presentation/ginga-116274-lord-krishna-190-pictures-education-ppt-powerpoint/

Friday, December 19, 2008

CTE (Common Table Expressions)

A common table expression (CTE) is a temporary named result set that can be referred to by a defining statement.

; WITH ()
AS
(

)

If the WITH clause for a CTE is not the first statement in the batch, you should delimit it from the preceding statement by placing a semicolon (;) in front of it.

Otherwise some time it will produce an error.

Msg 319, Level 15, State 1, Line 228
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
;WITH cte AS
(
SELECT StationID,
Station,
Line1,
Line2,
Line3,
Line4,
Line5
FROM City1
)

--If we are having only CTE and no other statements
--it will produce an error.

--Msg 102, Level 15, State 1, Line 29
--Incorrect syntax near ')'.

;WITH cte AS
(
SELECT StationID,
Station,
Line1,
Line2,
Line3,
Line4,
Line5
FROM City1
)
--SELECT * FROM City1
--SELECT * FROM cte

--A Common Table Expression must be called immediately after stating it.
--If not it will fail
;WITH cte (StationID, Station) AS
(

SELECT StationID,
Station,
Line1,
Line2,
Line3,
Line4,
Line5
FROM City1
)

SELECT * FROM cte
--All columns in the CTE must be in the Column List.
--Otherwise it will produce the error

--Msg 8158, Level 16, State 1, Line 1
--'cte' has more columns than were specified in the column list.

;WITH cte (A, B, C, D, E) AS
(
SELECT StationID,
Station,
Line1,
Line2,
Line3,
Line4,
Line5
FROM City1
)

SELECT * FROM cte
--But we can give new name for each column in CTE in Column List

Recurisve CTE
When a CTE refers to itself, it is considered to be recursive.
This is mainly used when hierarchical data is stored in the table.

; WITH cteName (column list(optional)) AS
(
-- Anchor Query or Anchor Member(AM)
UNION ALL --(This is the only thing allowed)
-- Recursive Query or Recursive Member (RM).
)
Defined number of loops

Default MAXRECURSION is 1000.

SELECT * FROM cteName OPTION (MAXRECURSION 4)

MAXRECURSION OPTION allowed maximum is 32767.

Reference Link

http://jonathanparker.com.au/

http://www.databasejournal.com/

http://www.mssqltips.com/

http://omnibuzz-sql.blogspot.com

Search for the below text in the below link and it is explained well.

Recursive Queries and Common Table Expressions

Thursday, December 18, 2008

DataReader to DataTable


public class DataUtils: System.Data.Common.DataAdapter
{
public DataUtils()
{
//
// TODO: Add constructor logic here
//
}

public int FillFromReader( DataTable dataTable, IDataReader dataReader )
{
return this.Fill( dataTable, dataReader );
}
}



How to call


DataUtils _objDataUtils = new DataUtils();

objDataUtils.FillFromReader( dataTable, dr ); //converts a datareader into a datatable

Wednesday, December 17, 2008

SqlParameter

Case 1
SqlParameter[] osqlParam = new SqlParameter[2];

osqlParam[0] = new SqlParameter("@FromCity", SqlDbType.VarChar);
osqlParam[0].Value = _objTubeMap.fromStation;

osqlParam[1] = new SqlParameter("@ToCity", SqlDbType.VarChar);
osqlParam[1].Value = _objTubeMap.toStation;

return SQLHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "GetStations", osqlParam);


Case2



string[] paramsNames = new string[2] { "@PostTitle", "@PostBody" };
string[] values = new string[2] { TextBox1.Text, Body };
SqlParameter[] parameters = new SqlParameter[2];

for (int i = 0; i < paramsNames.Length; i++)
{
parameters[i] = new SqlParameter();
parameters[i].ParameterName = paramsNames[i];
parameters[i].Value = values[i];
}