Sunday, July 28, 2013

STATISTICS_NORECOMPUTE in Rebuild Index


It specifies whether distribution statistics are recomputed. The default is OFF.
ON
Out-of-date statistics are not automatically recomputed.
OFF
Automatic statistics updating are enabled.

Indeed, statistics are recomputed during the index rebuild.

Dont think that it stops SQL Server from updating the statistics at the time of a rebuild; that is NOT what it does. 
Statistics are ALWAYS updated at the time of a REBUILD; this cannot be turned off (nor would you want to).

Instead, STATISTICS_NORECOMPUTE stops the database-wide auto-updating from updating the specific statistics for an index (or column-level statistic) that was created (or rebuilt) with this option turned on.

STATISTICS_NORECOMPUTE – When would anyone want to use it?


HTH

Saturday, July 27, 2013

Delegates, Invoke, InvokeRequired


Question: What is InvokeRequired? Why this word is so difficult and what is it do? 

Answer: InvokeRequired is a property of a control which tells whether or not you can safely access the control. It is a boolean to check if you are on the UI thread before you try to update a form or control from a worker thread. 

If the property is True, then an invocation is required to access the control as the caller is on different thread than the one that owns the control's handle.

The invocation is performed by calling the control's Invoke or BeginInvoke method. 
You create a delegate, which is an object that contains the reference to a method. Then we pass this delegate to Invoke or BeginInvoke method which essentially call the reference method again but on thread that owns the control's handle.

Summary:
InvokeRequired asks 'Am I on the right thread?', if so carry on, else I need a delegate.

I got help and understanding of this from: 

Accessing Controls from Worker Threads


HTH.


Thursday, July 25, 2013

Insert Data in DataGridView

Hi,

Lets say we already have DataGridView control as 'dgView' defined on our form with our columns defined as  FirstName, LastName and Address.
All columns were added from Design view of the form.
Now in code behind, if we just to add row without binding it to some datasource, it would be:
 
dgView.Rows.Add(New String() {"John","Doe","123 Main St, City UK"})
 

If we want to bind it with dataset or datatable then it would be :
 
Dim dt As New DataTable
dt.Columns.Add("FirstName") dt.Columns.Add("LastName") dt.Rows.Add(New String() {"saqib", "mahmood"}) ' This property is set if we dont have predefined columns in DataGridView.
dgView.AutoGenerateColumns = True dgView.DataSource = dt


Happy Coding!

Saturday, July 20, 2013

Delegates vb.net

Delegates are function pointers that store function address.

There are 3 steps to understand Delegates:
  1. Defining Delegate
  2. Instantiating Delegate
  3. Assigning Address of function to Delegate

Nothing fancy; just memorize the above steps to absorb the whole idea.

'How to define?
Public Delegate Sub testfile(Byval filename As String)

'instantiating the delegate
Dim mytestfile As testfile

Private Sub DoWork()
    'now assign the address of function to the delegate
    mytestfile = AddressOf FileTester

    'it can be called as
    mytestfile("test.txt") 
    'or
    mytestfile.Invoke("test.txt")
End Sub
Public Sub FileTester(ByRef filename As String)
     MsgBox(filename)
End Sub


Now FileTester is really the function will be called which just shows the filename

Happy coding!

Wednesday, May 8, 2013

Script Trigger in SQL Server 2008

Here is the query to script out all Triggers in SQL Server 2008:


set nocount on

DECLARE name nvarchar(128)

DECLARE procCursor CURSOR
FOR
SELECT distinct Name FROM sysobjects o
Inner join syscomments c on OBJECTPROPERTY(o.id, N'IsTrigger') = 1 and c.id= o.id

OPEN procCursor
FETCH
NEXT FROM procCursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN

PRINT 'if exists (select * from dbo.sysobjects '
PRINT ' where id = object_id(N' + char(39) + '[dbo].[' + @Name + ']' + char(39) + ')'
PRINT ' and OBJECTPROPERTY(id, N' + char(39) + 'IsTrigger' + char(39) + ') = 1) '
PRINT '  DROP TRIGGER ' + @Name 
PRINT ' GO '
PRINT ' SET QUOTED_IDENTIFIER on '
PRINT ' GO '
PRINT ' SET ANSI_NULLS on '
PRINT ' GO'
exec sp_helptext @Name
PRINT ' GO '
FETCH NEXT FROM procCursor INTO @name
END
CLOSE procCursor
DEALLOCATE procCursor
GO


Wednesday, April 10, 2013

Generate Row number


This method will only work when you do "select into from "

Syntax is :
select identity(int,1,1) as RowNumber, *
into newtable
from oldtable