这是我写的windows服务程序,怎么数据库中没有结果啊 ?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;//需要引用.Net命名空间
using System.Data.SqlClient;  // Use SQL Server data provider namespace
using System.Timers;
using System.Threading;namespace Service
{
    public partial class Service1 : ServiceBase
    {        SqlConnection con;
        SqlDataAdapter da;
        SqlCommandBuilder thisBuilder;
        DataSet ds;
        public Service1()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            Thread.Sleep(10000);
            this.timer.Enabled = true;
        }        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            this.timer.Enabled = false;
        }        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                this.con = new SqlConnection(
           @"Server=***;Integrated Security=True;
  Database=mybase");
                this.con.Open();
            }
            catch (Exception err)
            {
                throw new ApplicationException("连接数据库出错,系统信息:" + err.Message);
            }            this.da = new SqlDataAdapter("SELECT * FROM student ", this.con);
            this.thisBuilder = new SqlCommandBuilder(da);
            this.ds = new DataSet();
            this.da.Fill(ds, "student");
            //获得主机名
            string HostName = Dns.GetHostName();
            /* foreach (DataRow dr in dataset.Tables["student"].Rows)
             {
                 if (Convert.ToString(dr["Sno"]) == "11")
                 {
                     dr["Sname"] = HostName;
                     return;
                 }                 da.Update(dataset,"student");
                 con.Close();             }*/
            DataRow dr = this.ds.Tables["student"].NewRow();
            dr["Sno"] = 100;
            dr["Sname"] = HostName;
            dr["Sage"] = 20;
            this.ds.Tables["student"].Rows.Add(dr);
            this.da.Update(ds, "student");
            this.con.Close();
        }
    }
}
加了timer控件,每个30秒运行
怎么数据库中的不到想要的结果呢?

解决方案 »

  1.   

     有用Installutil安装服务吗? 安装后有启动吗? 
      

  2.   

    不要使用那个UI控件。        private System.Timers.Timer timerExecuter;        private ILog logger;        public AutoUploadService()
            {
                InitializeComponent();
            }        protected override void OnStart(string[] args)
            {
                logger = log4net.LogManager.GetLogger("logAll");            timerExecuter = new System.Timers.Timer();            timerExecuter.Enabled = true;            timerExecuter.Interval =Properties.Settings.Default.Interval * 60000 ; // setting number is minutes            timerExecuter.Elapsed += new System.Timers.ElapsedEventHandler(timerExecuter_Elapsed);            logger.Info("service start.");
                //logger.Info("first upload.");
                //BeginUpload();
            }        void timerExecuter_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                //check start time.
                DateTime time = Properties.Settings.Default.StartTime;            if (DateTime.Now.CompareTo(time) > 0) 
                {
                    
                    Properties.Settings.Default.StartTime = Properties.Settings.Default.StartTime.AddDays(1);
                    logger.Info("begin to upload" + DateTime.Now.ToString());
                   
                }
                else
                {
                    logger.Info("time is not arrive :" + time.ToString());
                    return;
                }
                BeginUpload();
               
            }
      

  3.   

    懒的看你的那么多代码。告诉你如何调试。创建一个控制台工程,引用你的windows服务工程,在解决方案上(点击鼠标右键)把这个控制台工程作为启动工程。然后让控制台工程去 new 那个对象实例并执行它的 OnStart。这样就可以在vs里测试或者调试了。
      

  4.   

    你用的那个Timer控件是UI控件,在服务里不起作用把服务选为“允许跟桌面交互”看行不行,或者把
    Timer控件改为我写的那样。
      

  5.   

    windows服务中是可以使用UI控件的。连windows Form也是可以有的。那些需要在windows的任务栏上显示一个任务图标的windows 服务,就是通过加载一个Form窗体,来加载.net的任务栏图标控件的。
      

  6.   

     Add a line: this.timer1.Start();
      

  7.   

    参考http://blog.csdn.net/Linux7985/archive/2010/01/10/5169375.aspx
      

  8.   

    有没有人在windows服务中可以正常操作数据的?我也是这样的问题,我操作不了我本的的sqlserver2000 也操作不了服务器上的sqlserver 2000 不知道是那错了!有会的人给我发个demo  
    邮箱:[email protected] 
    QQ:250110961 谢谢!
      

  9.   

    会的人给我发个demo   
    邮箱:[email protected]  
    QQ:250110961 谢谢!收到邮件测试正常后,我会在这个两个上给散发:60 分  谢谢大家支持!
    http://topic.csdn.net/u/20110830/10/45fb0123-9717-4e24-b1fd-41dc5ee8d1d0.html?seed=680969590&r=75234806#r_75234806
    http://topic.csdn.net/u/20110830/11/b77db9ba-fcaa-449c-809f-720d5d139750.html