Sunday, 20 July 2014

Show animated wait window ,while processing request

Its a major problem faced while processing DB request else functions etc .

The problem is when we perform command on database it takes time of few seconds, at that time my applications main window becomes halted.
when i performs command on database execution returns on main application when whole command is executed on database.
Just Like "NonQuery()"
Lets assume we are working on asp.net 4.0 and sending request to view.  i am showing a form before it and closing that form after the command, but problem is i am putting a GIF image on the form, because exection is halted on main application, the gif don't shows animation until the command is executed.
so that i putted it on a BackGroundWorker, but main thread is halted so its child tread also become halt.
we can not put database transaction in background worker, becuase sometime working is depended on result retrieved from database, and we want to halt application untill data is not fetched, but in this case also we want that on wait form, the gif image should show its animation.
What to do in this case, and how to do ?
For that no need of large block of code.
<asp:UpdateProgress ID="updateProgress1" runat="server">'
 <ProgressTemplate>
       <div id="progressBackgroundFilter">
                 </div>
           <div id="processMessage" style="text-align: center">
            <asp:Image ID="imgLoader" runat="server" ImageUrl="~/Images/loader.gif" />    <br />
           <b>Loading...</b>
  </div>
</ProgressTemplate>
Same problem I faced while processing request from Windows application.
That time , we normally used Thread class and its methods
Thread query = new Thread(() => Method("Method", "Parameters", Blah));
  query.Start();    //start your animation
  while (query.IsAlive)   {
    Application.DoEvents();
    Thread.Sleep(10);    }
I don't have any idea about  BackgroundWorker  in windows form. But you can try with it.

Saturday, 12 July 2014

Dynamic query in SQL

Dynamic query which nothing but conditional query which help to optimize query and improve performance of query.

Currently using  [AdventureWorksLT2008R2] datbase as following :

Try first

EXEC ('SELECT * FROM SalesLT.Product')

You can try following just for idea

DECLARE @SQL NVARCHAR(max), @ParmDefinition NVARCHAR(1024)
DECLARE @ListPrice money = 2000.0, @LastProduct varchar(64)
SET @SQL =       N'SELECT @pLastProduct = max(Name)
                   FROM SalesLT.Product
                   WHERE ListPrice >= @param1'
SET @ParmDefinition = N'@param1 money,
                        @pLastProduct varchar(64) OUTPUT'
EXECUTE sp_executeSQL      -- Dynamic T-SQL
            @SQL,
            @ParmDefinition,
            @param1 = @ListPrice,
            @pLastProduct=@LastProduct OUTPUT
            
SELECT [ListPrice >=]=@ListPrice, LastProduct=@LastProduct

Tips :

If you tried it, then also tried for Temp Table and Table variable for knowledge.

Query :
1)  DECLARE @T AS MyTable;

     INSERT INTO @T VALUES (1,2), (2,3)

     SELECT * FROM @T

     EXEC sp_executesql   N'SELECT *  FROM @T'

Error :  Declare Scalar variable @T

2)   Use  TYPE feature of SQL Server  for Table variable . Refer following query to execute sql statement with table variable

CREATE TYPE MyTable AS TABLE 
Foo int,
Bar int
);
GO

DECLARE @T AS MyTable;

INSERT INTO @T VALUES (1,2), (2,3)

SELECT *  FROM @T

EXEC sp_executesql
  N'SELECT *  FROM @T',
 N'@T MyTable READONLY',
  @T=@T 

3)  


Saturday, 5 July 2014

Paging in SQL Server

Paging is one the functionality which we can achieved in many ways. So many plugins,technologies used for development.

My sir already used in 2008, I think over to tried it in by different way. It found that Microsoft provides pagination feature with SQL Server 2012. We can acheived it by row_number() and minimum ids etc But from SQL Server 2012 we can do pagination by using FETCH and OFFSET.



From above records, I need to split next 6 records after 32 Address ID.
So doing pagination is a two-step process: -
  • First mark the start of the row by using “OFFSET” command.
  • Second specify how many rows you want to fetch by using “FETCH” command.

Query : 
1. Fetching records from 0 to above

SELECT * FROM [Address] 
order by AddressID
offset 0 rows – start from zero
Fetch Next 6 Rows only

2. Fetching records from 32 to next 6 records paging.

SELECT * FROM [Address] 
order by AddressID
offset 6 rows 
Fetch Next 6 Rows only

[Still reading regarding same... but you can start reading regarding FETCH and OFFSET ]



NIHAR'S BLOG: Sequence : Auto Generated ID's in SQL Server 2012

NIHAR'S BLOG: Sequence : Auto Generated ID's in SQL Server 2012: Hi friends , after long time come back with new excellent features of SQL Server 2012. Don't worry , I am not posting all features bec...

Reading text file in SQL Server

There are many option to read text file data and store it into database. We may have following options available for reading/storing/updating data into database
1.  Import and Export wizard of SQL Serve
2. SQL Server Integration Services
3. Using C# with SQL CLR will insert data into tables
But , using few lines code of query need to execute in SSMS.
SQL Server provides "BULK INSERT" Operation to read data from local system and store it into database tables.
Steps :
1. First create text file with some name "Abc.txt" and enter values as
nihar,
Vishal,
Vivek,
Save and Copy path of file.
2. Open SSMS and create temp table for data insertion
Create table temp_Data
(
name nvarchar(20)
)
3.  Use following query to view operation
BULKINSERT Temp_dataFROM  'D:\Abc.txt'WITH(
FIELDTERMINATOR 
=',',     -- Here you can use any delimiter for seperation
ROWTERMINATOR 
='\n'  );
SELECT * FROM Temp_data
o/P :  3 Rows Affected
Here some arguments commonly used in BULK INSERT
-  FIELDTERMINATOR
-  ROWTERMINATOR