Thursday 22 August 2013

Split Date Range using SQL Server

Declare @StartDate DateTime, @EndDate DateTime, @FromDate DateTime, @ToDate DateTime, @CurrentCalYearStart DateTime, @CurrentCalYearEnd DateTime

Set @StartDate='06/25/1980'
Set @EndDate='06/26/1983'

SET @FromDate = @StartDate
SET @ToDate = @EndDate
WHILE (year(@FromDate) <= YEAR(@EndDate))
BEGIN
    SET @CurrentCalYearStart = DATEADD(MM,0,DATEADD(YY,DATEDIFF(YY,0,@FromDate),0))
    SET @CurrentCalYearEnd = DATEADD(MM,12,DATEADD(YY,DATEDIFF(YY,0,@FromDate),-1))
   
    IF (datediff(year,@StartDate,@CurrentCalYearStart) = 0 AND datediff(year,@EndDate,@CurrentCalYearEnd) = 0)
    BEGIN
        SET @FromDate = @StartDate
        SET @ToDate = @EndDate
    END
    ELSE IF (datediff(year,@StartDate,@CurrentCalYearStart) = 0 AND datediff(year,@EndDate,@CurrentCalYearEnd) <> 0)
    BEGIN
        SET @FromDate = @StartDate
        SET @ToDate = @CurrentCalYearEnd
    END
    ELSE IF (datediff(year,@StartDate,@CurrentCalYearStart) <> 0 AND datediff(year,@EndDate,@CurrentCalYearEnd) <> 0)
    BEGIN
        SET @FromDate = @CurrentCalYearStart
        SET @ToDate = @CurrentCalYearEnd
    END
    ELSE IF (datediff(year,@StartDate,@CurrentCalYearStart) <> 0 AND datediff(year,@EndDate,@CurrentCalYearEnd) = 0)
    BEGIN
        SET @FromDate = @CurrentCalYearStart
        SET @ToDate = @EndDate
    END

SELECT @FromDate,@ToDate

SET @FromDate = DATEADD(YEAR,1,@FromDate)

END

Friday 15 March 2013

Storing and Retrieving From App.config

 <?xml version="1.0"?>
<configuration>
  <configSections>

    <sectionGroup name="Country">
        <section name="CountryMapping" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>

<connectionStrings>
    <clear />
    <add name="XYZ" connectionString="Data Source=localhost;Initial Catalog=NorthWind; Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

<Country>
    <CountryMapping>

      <add key="United States of America" value="USA"/>
      <add key="Kingdom of Great Britain" value="UK"/>
      <add key="China" value="CH"/>

    </CountryMapping>
  </Country>

....
</configuration>

C# Code:
using System.Collections.Specialized;

 NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("Country/CountryMapping");
foreach (String valueCollection in section)
{
  if (dr["Country"].ToString() == valueCollection)
  {
     dr["Country"] = section[valueCollection];
  }


For more reference:
http://www.codeproject.com/Articles/21036/Working-with-Configuration-Files-app-config-in-C-C

Sunday 3 March 2013

Convert Datareader to Dataset C#


public static DataSet convertDataReaderToDataSet(SqlDataReader reader)
        {
            DataSet dataSet = new DataSet();
            do
            {
                // Create new data table

                DataTable schemaTable = reader.GetSchemaTable();
                DataTable dataTable = new DataTable();

                if (schemaTable != null)
                {
                    // A query returning records was executed

                    for (int i = 0; i < schemaTable.Rows.Count; i++)
                    {
                        DataRow dataRow = schemaTable.Rows[i];
                        // Create a column name that is unique in the data table
                        string columnName = (string)dataRow["ColumnName"]; //+ "<C" + i + "/>";
                        // Add the column definition to the data table
                        DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                        dataTable.Columns.Add(column);
                    }

                    dataSet.Tables.Add(dataTable);
                    // Fill the data table we just created
                    while (reader.Read())
                    {
                        DataRow dataRow = dataTable.NewRow();

                        for (int i = 0; i < reader.FieldCount; i++)
                            dataRow[i] = reader.GetValue(i);

                        dataTable.Rows.Add(dataRow);
                    }
                }
                else
                {
                    // No records were returned

                    DataColumn column = new DataColumn("RowsAffected");
                    dataTable.Columns.Add(column);
                    dataSet.Tables.Add(dataTable);
                    DataRow dataRow = dataTable.NewRow();
                    dataRow[0] = reader.RecordsAffected;
                    dataTable.Rows.Add(dataRow);
                }
            }
            while (reader.NextResult());
            return dataSet;
        }