Search This Blog

Wednesday, March 6, 2013

Last date of the month SQL Server

SQL Server > Built-In Functions > EOMONTH

EOMONTH returns the last day of the month that contains the specified date
 
SELECT EOMONTH(GETDATE())

For older editions ( < SQL Server 2012 )

SELECT CAST(DATEADD(d,-1,(DATEADD(mm,DATEDIFF(m,0,GETDATE())+1,0)))AS DATE)





Tuesday, March 5, 2013

ComVisibleAttribute C#

C# > InteropServices > ComVisible

ComVisible Attribute indicates that the managed type is visible to COM.
The default value is true.

Examples:


1. AssemblyInfo.cs
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
     [assembly: ComVisible(false)]

2.ComVisible in classes and methods
   [ComVisible(false)] //the class and its members are invisible to COM
   class ClassA
   {
        public ClassA()
        {
        }
        //if you derive ClassB from ClassA and export ClassB to COM, ClassA Method1 become visible to COM
        public int Method1()
        {
            return 0;
        }
        [ComVisible(false)] // Method2 is invisible to COM
        public int Method2()
        {
            return 1; 
        }
   }

 




DllImportAttribute C#

C# > InteropServices > DllImportAttribute

Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a static entry point.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        // Import Win32 MessageBox function.
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Call the MessageBox function with platform invoke.
            MessageBox(new IntPtr(0), "Text!", "Title", 0);
        }
    }
}




System.Runtime.InteropServices Namespace

System.Runtime.InteropServices Namespace

Provides a list of members that support COM interop and platform invoke services.

Monday, March 4, 2013

HttpUtility does not exist in the current context

Solution:

Add reference to System.Web;

Use:

using System.Web;

Relation Operators (C#)


== Equal

!= Not equal

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal





FOR XML clause (SQL Server)

SQL Server > XML Data > FOR XML

You can retrieve results of a SQL query as XML by specifying the FOR XML clause in the query.

Examples


Example

create table customer
(
       id                  int,
       name         nvarchar(max)
)
go
create table [order]
(
       id                  int,
       Date         date,
       amount       int,
       customer_id  int
)
set nocount on
go

insert into
       customer (id, name)
values
       (1,'Customer 1')
insert into
       customer (id, name)
values
       (2,'Customer 2')
insert into
        [order] (id, Date, amount, customer_id)
values
       (1,GETDATE(),100, 1)
insert into
       [order] (id, Date, amount, customer_id)
values
       (2,GETDATE(),100, 1)
insert into
       [order] (id, Date, amount, customer_id)
values
       (3,GETDATE(),200, 1)
insert into
       [order] (id, Date, amount, customer_id)
values
       (4,GETDATE(),300, 2)

--query returns results as a rowset
select
       customer.id,
       customer.name,
       ord.id order_id,
       ord.Date,
       ord.amount    
from
       customer
       join [order] ord on customer.id = ord.customer_id

Result:


id            name                  order_id  Date                    amount
1             Customer 1        1               2013-03-04         100
1             Customer 1        2               2013-03-04         100
1             Customer 1        3               2013-03-04         200
2             Customer 2        4               2013-03-04         300
                                                   


--query returns results as XML






AUTO mode returns query results as nested XML elements

select
       customer.id,
       customer.name,
       ord.id order_id,
       ord.Date,
       ord.amount    
from
       customer
       join [order] ord on customer.id = ord.customer_id
FOR XML auto

Result:

<customer id="1" name="Customer 1">
  <ord order_id="1" Date="2013-03-04" amount="100" />
  <ord order_id="2" Date="2013-03-04" amount="100" />
  <ord order_id="3" Date="2013-03-04" amount="200" />
</customer>
<customer id="2" name="Customer 2">
  <ord order_id="4" Date="2013-03-04" amount="300" />
</customer>






RAW mode transforms each row in the query result set into an XML element that has the generic identifier , or the optionally provided element name.


select
       customer.id,
       customer.name,
       ord.id order_id,
       ord.Date,
       ord.amount    
from
       customer
       join [order] ord on customer.id = ord.customer_id
FOR XML raw

Result:


<row id="1" name="Customer 1" order_id="1" Date="2013-03-04" amount="100" />
<row id="1" name="Customer 1" order_id="2" Date="2013-03-04" amount="100" />
<row id="1" name="Customer 1" order_id="3" Date="2013-03-04" amount="200" />
<row id="2" name="Customer 2" order_id="4" Date="2013-03-04" amount="300" />

PATH mode provides a simpler way to mix elements and attributes.

select
       customer.id,
       customer.name,
       ord.id order_id,
       ord.Date,
       ord.amount    
from
       customer
       join [order] ord on customer.id = ord.customer_id
FOR XML path

 Result:
<row>
  <id>1</id>
  <name>Customer 1</name>
  <order_id>1</order_id>
  <Date>2013-03-04</Date>
  <amount>100</amount>
</row>
<row>
  <id>1</id>
  <name>Customer 1</name>
  <order_id>2</order_id>
  <Date>2013-03-04</Date>
  <amount>100</amount>
</row>
<row>
  <id>1</id>
  <name>Customer 1</name>
  <order_id>3</order_id>
  <Date>2013-03-04</Date>
  <amount>200</amount>
</row>
<row>
  <id>2</id>
  <name>Customer 2</name>
  <order_id>4</order_id>
  <Date>2013-03-04</Date>
  <amount>300</amount>
</row>
drop table [order]
drop table customer