C#Windows服务程序的快速开发

开发 后端
C#Windows服务程序的快速开发向你介绍了在很多应用中需要做windows服务来操作数据库等操作,希望对你了解C#Windows服务程序的开发有所帮助。

C#Windows服务程序的快速开发:在很多应用中需要做windows服务来操作数据库等操作,比如

(1)一些非常慢的数据库操作,不想一次性去做,想慢慢的通过服务定时去做,比如定时为数据库备份等

(2)在.net Remoting中利用windows服务来做Host

利用vs.net我们可以在几分钟之内建立其windows服务,非常简单

下面说一下C#Windows服务程序的快速开发的步骤

C#Windows服务程序的快速开发1. 新建一个项目

C#Windows服务程序的快速开发2. 从一个可用的项目模板列表当中选择Windows服务

C#Windows服务程序的快速开发3. 设计器会以设计模式打开

C#Windows服务程序的快速开发4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer)

C#Windows服务程序的快速开发5. 设置Timer属性,Interval属性200毫秒(1秒进行5次数据库操作)

C#Windows服务程序的快速开发6. 然后为这个服务填加功能

C#Windows服务程序的快速开发7.双击这个Timer,然后在里面写一些数据库操作的代码,比如

  1. SqlConnection conn=new SqlConnection(  
  2. server=127.0.0.1;database=test;uid=sa;pwd=275280");  
  3.   SqlCommand comm=-new SqlCommand(  
  4. insert into tb1 ('111',11)",conn);  
  5.   conn.Open();  
  6.   comm.ExecuteNonQuery();  
  7.   conn.Close(); 

C#Windows服务程序的快速开发8. 将这个服务程序切换到设计视图

C#Windows服务程序的快速开发9. 右击设计视图选择“添加安装程序”

C#Windows服务程序的快速开发10. 切换到刚被添加的ProjectInstaller的设计视图

C#Windows服务程序的快速开发11. 设置serviceInstaller1组件的属性:

1) ServiceName = My Sample Service

2) StartType = Automatic (开机自动运行)

C#Windows服务程序的快速开发12. 设置serviceProcessInstaller1组件的属性  Account = LocalSystem

C#Windows服务程序的快速开发13. 改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)

C#Windows服务程序的快速开发14. 执行命令“InstallUtil.exe MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。(InstallUtil这个程序在WINDOWS文件夹\Microsoft.NET\Framework\v1.1.4322下面)

C#Windows服务程序的快速开发15. 右击桌面上“我的电脑”,选择“管理”就可以打计算机管理控制台

C#Windows服务程序的快速开发16. 在“服务和应用程序”里面的“服务”部分里,你可以发现你的Windows服务已经包含在服务列表当中了

17. 右击你的服务选择启动就可以启动你的服务了

看看数据库是不是一秒多了5个记录啊

C#Windows服务程序的快速开发需要注意的是:

如果你修改了这个服务,路径没有变化的话是不需要重新注册服务的,如果路径发生了变化,需要先卸载这个服务InstallUtil.exe /u参数,然后再重新安装这个服务,不能直接安装。还有就是windows服务是没有界面的,不要企图用控制的输出方式来输出一些信息,你只能添加一个EventLog,通过WriteEntry()来写日志。

关于怎么用windows服务来做一个远程服务可以看一下http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT15.asp

C#Windows服务程序的快速开发的相关内容就向你介绍到这里,希望对你了解和学习C#Windows服务程序的快速开发有所帮助。

【编辑推荐】

  1. C#Windows服务之添加文件监视服务
  2. C#创建Windows服务学习的一点体会
  3. C#Windows服务程序之添加安装程序图解
  4. C#Windows服务程序开发实例浅析
  5. C#Windows服务程序开发浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-14 14:25:09

Windows服务程序

2009-08-14 15:54:50

Windows服务程序C#Windows服务

2009-08-14 15:19:38

Windows服务程序Windows服务

2009-08-14 14:17:16

C#Windows服务

2009-08-14 16:24:00

Windows服务程序

2009-08-14 15:06:08

Windows服务程序

2009-08-14 15:47:18

C#Windows服务

2009-08-14 16:48:39

C#Windows服务

2009-08-14 10:50:09

Windows服务介绍

2009-08-14 13:41:13

C#Windows服务

2009-08-14 14:53:55

WINDOWS服务交互

2009-08-14 17:27:30

C#Windows应用

2009-08-14 17:36:20

C#Windows应用

2009-08-14 17:43:20

C#Windows应用

2009-08-14 17:55:52

C#Windows应用

2009-08-14 11:15:19

文件监视C#Windows服务

2009-08-14 16:13:25

C#windows服务

2009-08-14 10:42:16

Timer控件的使用C#windows服务

2009-08-14 18:00:22

C#Windows应用

2009-08-14 18:04:59

C#Windows应用
点赞
收藏

51CTO技术栈公众号