Most of the times we wouldn't want the time part of the dates to be stored or retrieved when dealing with only days. But the data type "datetime" in sql always includes the time even though you don't want it. There is no other data type in sql to store or retrieve the date without time. In this case we need to strip the time part from the date and store the remaining in database. To do this favor, I have come up with the below sql code which strips the time part from the date time.
declare @date datetime;
set @date = getdate();
select cast(convert(varchar, @date, 101) as datetime) --Strips time element from the date time
The above code will work like this:
If you have the date time 25/09/2009 12:36:40:654 then this will be converted as 25/09/2009 00:00:00:000
Friday, September 25, 2009
Tuesday, September 22, 2009
Find common elements in c# generic list
The following code demonstrates how to get the common elements in
given 2 lists using Intersect method of System.Collections.Generic.List.
using System;
using System.Linq;
namespace ConsoleApplication1
{
static class Program
{
static void Main(string[] args)
{
int[] elementSet1 = { 5, 1, 6, 3, 8 };
int[] elementSet2 = { 3, 7, 8, 6, 5 };
foreach (int element in elementSet1.Intersect(
elementSet2))
{
Console.WriteLine(element);
}
Console.Read();
}
}
}
Labels:
c# generic list,
common elements,
Dot Net 3.5,
intersect,
LINQ
How to find an element in c# Generic List
The following sample code demonstrates you how to find a matching element in the object list.
Let's say we have an object called Person with public properties PersonID, Name, Age, Gender as defined below.
using System.Collections.Generic.List;
public class Person
{
public int PersonID {get; set;}
public string Name {get; set;}
public int Age {get; set;}
public bool Gender {get; set;}
}
class program
{
public static void Main(string[] args)
{
// Let's say we have a method defined as dbAccess.GetPersons() to
//return all the records from Person table and populates the
//list object "persons" as shown below.
List persons = dbAccess.GetPersons();
int personIDToFind = 1234;
//Find person in persons list by PersonID using predicate
//"p=>p.PersonID == personIDToFind"
Person personByFindOperation = persons.Find( p=>p.PersonID == personIDToFind );
string personName = "Kiran"
//Find person in persons list by Name using predicate
//"p=>p.Name == personName"
Person personByFindOperation2 = persons.Find( p=>p.Name == personName );
}
}
Note: The Find method of List returns the first occurrence of the exact match within the entire System.Collections.Generic.List.
Let's say we have an object called Person with public properties PersonID, Name, Age, Gender as defined below.
using System.Collections.Generic.List;
public class Person
{
public int PersonID {get; set;}
public string Name {get; set;}
public int Age {get; set;}
public bool Gender {get; set;}
}
class program
{
public static void Main(string[] args)
{
// Let's say we have a method defined as dbAccess.GetPersons() to
//return all the records from Person table and populates the
//list object "persons" as shown below.
List
int personIDToFind = 1234;
//Find person in persons list by PersonID using predicate
//"p=>p.PersonID == personIDToFind"
Person personByFindOperation = persons.Find( p=>p.PersonID == personIDToFind );
string personName = "Kiran"
//Find person in persons list by Name using predicate
//"p=>p.Name == personName"
Person personByFindOperation2 = persons.Find( p=>p.Name == personName );
}
}
Note: The Find method of List returns the first occurrence of the exact match within the entire System.Collections.Generic.List.
Labels:
c#,
Dot Net,
element,
Find,
Generic List,
Object,
sort items
Thursday, September 10, 2009
How to create a windows service in dot net framework
Windows service is an application which always runs in background. We can view all the installed windows services in ControlPanel/Administrativetools/Services. Sql server, Oracle, ... are the good examples of windows service.
Using visual studio it is very easy to create windows service. Please follow the below steps to create a windows service.
Using visual studio it is very easy to create windows service. Please follow the below steps to create a windows service.
- Open visual studio and create a new project of template "Windows Service" as shown below
- After you click OK in the above step you will see a new solution named "WindowsService1" created as shown below.
- Right click on the above screen and click "Add Installer". This will create an installer component which helps us in installing this service.
- You can notice a new component "ProjectInstaller.cs" added and opened as shown below. It has 2 controls "ServiceProcessInstaller1" and "ServiceInstaller1".
- You can set whether this service is manual, automatic, or disabled by changing the StartType property of "ServiceInstaller1" (Right click -> Properties).
- Change the "ServiceProcessInstaller1"'s property "Account" to "LocalSystem"
- Right click on Service1.cs in solution explorer and click "View Code"
- In code you will see the service event handlers "OnStart", "OnStop".
- Write code that initiates a thread in OnStart event handler.
- Write code that stops execution of the thread in OnStop event handler.
- Please see below for sample code
- Build the solution. You have the windows service built ready.
- But until we install this service we cannot see it is running or not.
- To install this service you need to open the Visual Studio 2005/2008 Command prompt in Start->Visual Studio 2005/2008->Visual Studio Tools
- Run the command : InstallUtil.exe Windows_Service_EXE_Full_Path
- Here Windows_Service_EXE_Full_Path is the exe generated for the WindowsService1 project. Let's say the full path of the exe is C:/WindowsService1/Bin/Debug/WindowsService1.exe then our command to install it would be like this.
- InstallUtil.exe C:/WindowsService1/Bin/Debug/WindowsService1.exe
- If the above command is successful then you will notice a new service named "Service1" is shown in Control Panel->Administrative Tools/Services
- To uninstall a service, InstallUtil.exe -u C:/WindowsService1/Bin/Debug/WindowsService1.exe
- To make the life easier to install/uninstall windows services we can create a setup project which will let people easily install/uninstall with a wizard Please read this link. http://cherupally.blogspot.com/2009/09/how-to-create-setup-project-to-install.html
Labels:
c#,
create,
Dot Net,
installutil,
project,
project installer,
service,
visual studio,
win 32 service,
windows service
How to create a setup project to install windows services
This post assumes that you have knowledge on windows services and how to create a windows service using visual studio. People who don't know how to create windows service please learn here.
Please follow below steps to create a setup project
1. Create a visual studio solution with a project of type Windows Service as explained in http://cherupally.blogspot.com/2009/09/how-to-create-windows-service-in-dot.html
2. Add a new project of type "Setup Project" to the solution as shown below
2. Once you click on OK button in the above step, you will see the screen below. There you notice a new project named "Setup1" is added.
3. Now right click on Setup1 project in the solution and click "Add Project Output" as shown below.
4. In "Add Project Output Group" dialog box shown below, select the windows service project in the drop down and click OK.
5. The above 4 steps are only to copy all the assemblies to specifies installation folder. Setup will not install the services in the system. To tell the setup to install services we must add custom actions. To open the custom actions window right click "Setup1" project in solution explorer and click View->Custom Actions as shown below.
6. You will see the Custom Actions tab opened like this.
7. Right click on "Custom Actions" and click "Add Custom Action".
8. That will open a dialog box which lets you choose the items from "Application Folder". Select "Primary Output From WindowsService1" in "ApplicationFolder" and click OK.
9. The above step will add this custom action to all sub folders "Install", "Commit", "Rollback", "Uninstall". Finally you will see the screen like this.
10. Build the "Setup1" project. Now you are ready to use this setup project to install/uninstall windows services. Take the setup build from WindowsService1\Setup1\Debug and use it to install/uninstall when needed.
Please follow below steps to create a setup project
1. Create a visual studio solution with a project of type Windows Service as explained in http://cherupally.blogspot.com/2009/09/how-to-create-windows-service-in-dot.html
2. Add a new project of type "Setup Project" to the solution as shown below
2. Once you click on OK button in the above step, you will see the screen below. There you notice a new project named "Setup1" is added.
3. Now right click on Setup1 project in the solution and click "Add Project Output" as shown below.
4. In "Add Project Output Group" dialog box shown below, select the windows service project in the drop down and click OK.
5. The above 4 steps are only to copy all the assemblies to specifies installation folder. Setup will not install the services in the system. To tell the setup to install services we must add custom actions. To open the custom actions window right click "Setup1" project in solution explorer and click View->Custom Actions as shown below.
6. You will see the Custom Actions tab opened like this.
7. Right click on "Custom Actions" and click "Add Custom Action".
8. That will open a dialog box which lets you choose the items from "Application Folder". Select "Primary Output From WindowsService1" in "ApplicationFolder" and click OK.
9. The above step will add this custom action to all sub folders "Install", "Commit", "Rollback", "Uninstall". Finally you will see the screen like this.
10. Build the "Setup1" project. Now you are ready to use this setup project to install/uninstall windows services. Take the setup build from WindowsService1\Setup1\Debug and use it to install/uninstall when needed.
Labels:
configure,
deployment,
install,
installutil,
project,
service,
setup,
visual studio,
win 32 service,
windows service
Subscribe to:
Posts (Atom)