26 Mayıs 2011 Perşembe

Razor render- MVC3 View Render to String

        public static string RazorRender(Controller context, string DefaultAction)

        {

            string Cache = string.Empty;

 

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            System.IO.TextWriter tw = new System.IO.StringWriter(sb);

 

            RazorView view_ = new RazorView(context.ControllerContext, DefaultAction, null, false, null);

            view_.Render(new ViewContext(context.ControllerContext, view_, new ViewDataDictionary(), new TempDataDictionary(), tw), tw);

 

            Cache = sb.ToString();

 

            return Cache;

        }

 

        public static string RenderRazorViewToString(string viewName, object model)

        {

            ViewData.Model = model;

            using (var sw = new StringWriter())

            {

                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);

                var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();

            }

        }

 

        public static class HtmlHelperExtensions

        {

            public static string RenderPartialToString(ControllerContext context, string partialViewName, ViewDataDictionary viewData, TempDataDictionary tempData)

            {

                ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);

 

                if (result.View != null)

                {

                    StringBuilder sb = new StringBuilder();

                    using (StringWriter sw = new StringWriter(sb))

                    {

                        using (HtmlTextWriter output = new HtmlTextWriter(sw))

                        {

                            ViewContext viewContext = new ViewContext(context, result.View, viewData, tempData, output);

                            result.View.Render(viewContext, output);

                        }

                    }

 

                    return sb.ToString();

                }

 

                return String.Empty;

            }

        }

 

9 Mayıs 2011 Pazartesi

difference between two date–iki tarih arasındaki zaman farkı

public string LoopAge(DateTime myDOB, DateTime FutureDate)

{

    int years = 0;

    int months = 0;

    int days = 0;

 

    DateTime tmpMyDOB = new DateTime(myDOB.Year, myDOB.Month, 1);

 

    DateTime tmpFutureDate = new DateTime(FutureDate.Year, FutureDate.Month, 1);

 

    while (tmpMyDOB.AddYears(years).AddMonths(months) < tmpFutureDate)

    {

        months++;

        if (months > 12)

        {

                years++;

                months = months - 12;

        }

    }

 

    if (FutureDate.Day >= myDOB.Day)

    {

        days = days + FutureDate.Day - myDOB.Day;

    }

    else

    {

        months--;

        if (months < 0)

        {

                years--;

                months = months + 12;

        }

        days = days + (DateTime.DaysInMonth(FutureDate.AddMonths(-1).Year, FutureDate.AddMonths(-1).Month) + FutureDate.Day) - myDOB.Day;

 

    }

 

    //add an extra day if the dob is a leap day

    if (DateTime.IsLeapYear(myDOB.Year) && myDOB.Month == 2 && myDOB.Day == 29)

    {

        //but only if the future date is less than 1st March

        if(FutureDate >= new DateTime(FutureDate.Year, 3,1))

                days++;

    }

 

    return "Years: " + years + " Months: " + months + " Days: " + days;

}

 

        public void LoopAge(DateTime myDOB, DateTime FutureDate)

        {

            int years = 0;

            int months = 0;

            int days = 0;

 

            DateTime tmpMyDOB = new DateTime(myDOB.Year, myDOB.Month, 1);

 

            DateTime tmpFutureDate = new DateTime(FutureDate.Year, FutureDate.Month, 1);

 

            while (tmpMyDOB.AddYears(years).AddMonths(months) < tmpFutureDate)

            {

                months++;

                if (months > 12)

                {

                    years++;

                    months = months - 12;

                }

            }

 

            if (FutureDate.Day >= myDOB.Day)

            {

                days = days + FutureDate.Day - myDOB.Day;

            }

            else

            {

                months--;

                if (months < 0)

                {

                    years--;

                    months = months + 12;

                }

                days +=

                    DateTime.DaysInMonth(

                        FutureDate.AddMonths(-1).Year, FutureDate.AddMonths(-1).Month

                    ) + FutureDate.Day - myDOB.Day;

 

            }

 

            //add an extra day if the dob is a leap day

            if (DateTime.IsLeapYear(myDOB.Year) && myDOB.Month == 2 && myDOB.Day == 29)

            {

                //but only if the future date is less than 1st March

                if (FutureDate >= new DateTime(FutureDate.Year, 3, 1))

                    days++;

            }

 

        }

 

C# TSQL Assembly Code

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

 

public partial class UserDefinedFunctions

{

    [Microsoft.SqlServer.Server.SqlFunction]

    public static SqlString datediff2()

    {

        // Put your code here

        return new SqlString("Hello");

    }

};

 

 

sp_configure 'clr enabled', 1

GO

RECONFIGURE

GO

ALTER DATABASE [AngelAdmin] SET TRUSTWORTHY ON

GO

 

CREATE Function datediff2() RETURNS NVARCHAR(1024)

EXTERNAL NAME TASQLHelper.UserDefinedFunctions.datediff2

 

SELECT dbo.datediff2()

20 Nisan 2011 Çarşamba

SQL JOIN TABLE VALUE TO STRING

SELECT STUFF((SELECT ',' + ISMI FROM MAGAZA_TANIM FOR XML PATH('')),1, 1, '')

ANKARA,ISTANBUL,IZMIR....

16 Nisan 2011 Cumartesi

Split string with parsename- sql virtual array

DECLARE @test nvarchar(256)= 'lorem.er.deneme.test'

 

SELECT PARSENAME(@test,1),PARSENAME(@test,3),PARSENAME(@test,2),PARSENAME(@test,4)

image

 

DECLARE @FullName NVARCHAR(500)

 

SET @FullName = N'Flintstone, Mr.Fred, Jr'

 

SELECT PARSENAME(FName, 3) AS "Title",

       PARSENAME(FName, 2) AS "FirstName",

       PARSENAME(FName, 4) AS "LastName",

       PARSENAME(FName, 1) AS "Prefix"

  FROM ( SELECT REPLACE(@FullName, ', ', '.')) D(FName)