我的Windows服务有问题

时间:2014-04-08 09:50:21

标签: c#

我的Windows服务程序有问题 我尝试创建一个Windows服务来从数据库中读取数据并将它们插入到其他数据库中但是当我从第一个数据库中读取数据并将其复制到数据集中时我遇到了问题,我无法从数据中读取这些数据设置并将其插入到其他数据集中以将其插入到其他数据库中 这是我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Data.SqlClient;
using System.Collections;
using System.Data.Sql;

namespace WindowSeriveDemo
{
    public partial class Service1 : ServiceBase
    {
        string connection = @"Data Source=DEVELOPER-PC\DEVELOPER;Initial Catalog=NMSys;User ID=sa;Password=2649940931";
        string connection2 = @"Data Source=DEVELOPER-PC\DEVELOPER;Initial Catalog=Dpardazesh;User ID=sa;Password=2649940931";


        private Timer ServiceTimer = new Timer();

        private int inProcess = 0;
        public const string SP_dataRead = "usp_Read_Data_From_DpardazeshDB";
        public const string SP_insertData = "usp_Write_Data_Into_NMSysDB";
        private IEnumerable<DataRow> item;
        //Timer timer1 = new Timer();

        public Service1()
        {
            InitializeComponent();
            setupTimer();
        }

        protected override void OnStart(string[] args)
        {


            //ServiceTimer.Elapsed += new ElapsedEventHandler(timer1_Elapsed);

        }
        private void setupTimer()
        {
            ServiceTimer.Elapsed += new ElapsedEventHandler(ServiceTimer_Tick);
            ServiceTimer.Interval = 10000;
            ServiceTimer.Enabled = true;
            ServiceTimer.Start();
        }
        protected override void OnStop()
        {
            //timer1.Enabled = false;
        }

        private void SyncDatabases()
        {
            inProcess = 1;



            SqlConnection conn2 = new SqlConnection(connection2);

            try
            {
                conn2.Open();
                SqlCommand cmd = new SqlCommand(SP_dataRead);
                winservDS dataSet = new winservDS();
                cmd.CommandType = CommandType.StoredProcedure;
                //AddParameter(cmd);
                SqlDataAdapter adapter = new SqlDataAdapter(SP_dataRead, conn2);
                adapter.Fill(dataSet, dataSet.usp_Read_Data_From_DpardazeshDB.TableName);
                dataSet.AcceptChanges();

                return ;


            }
            catch (Exception ex)
            {
                string text = ex.Message;
            }


            inProcess = 0;
        }

         private static SqlCommand cmdGetIdentity;
         public winservDS InsertUsers(IDictionary ids)
         {
             inProcess = 1;


             SqlConnection conn = new SqlConnection(connection);
             SqlConnection conn2 = new SqlConnection(connection2);
             conn.Open();
             conn2.Open();
             winservDS updates = new winservDS();
             DataRow dr;
             dr = updates.usp_Read_Data_From_DpardazeshDB.NewRow();

             foreach (DictionaryEntry i in ids)
             {
                 if (i.Value == null)
                     dr[i.Key.ToString()] = DBNull.Value;
                 else
                     dr[i.Key.ToString()] = i.Value;
             }
             updates.usp_Read_Data_From_DpardazeshDB.Rows.Add(dr);
             try
             {
                 winservDS.usp_Read_Data_From_DpardazeshDBDataTable tbl = updates.usp_Read_Data_From_DpardazeshDB;
                 //Create the adapter initial
                 SqlDataAdapter dataAdapter = new SqlDataAdapter();
                 dataAdapter.InsertCommand = WriteDatabase(conn);

                 //Roll Back the changes if some one error have
                 dataAdapter.ContinueUpdateOnError = false;

                 cmdGetIdentity = new SqlCommand("SELECT @@IDENTITY", conn);
                 dataAdapter.RowUpdated += new SqlRowUpdatedEventHandler(HandleRowUpdated);

                 dataAdapter.Update(tbl.Select("", "", DataViewRowState.Added));
                 return updates;
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message, ex);
             }
             inProcess = 0;
         }

       private static void HandleRowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            if ((e.Status == UpdateStatus.Continue) && (e.StatementType == StatementType.Insert))
                {
                    e.Row["id"] = Convert.ToInt32 (cmdGetIdentity.ExecuteScalar());
                    e.Row.AcceptChanges();
                }
            }


       private static SqlCommand WriteDatabase(SqlConnection conn)
        {


                SqlCommand cmd = new SqlCommand(SP_insertData);
                cmd.Parameters.Clear();
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameterCollection pc = cmd.Parameters;

                pc.Add(CreateParameter("@fHitType", System.Data.SqlDbType.Int));
                pc.Add(CreateParameter("@DateOfHit", System.Data.SqlDbType.DateTime));
                pc.Add(CreateParameter("@TimeOfHit", System.Data.SqlDbType.Int));
                pc.Add(CreateParameter("@fEmpid", System.Data.SqlDbType.Int));
                cmd.ExecuteNonQuery();
                return cmd;

        }

        private static SqlParameter CreateParameter(string p, SqlDbType sqlDbType)
        {
            SqlParameter parameter = new SqlParameter("@" + p, sqlDbType);
            parameter.SourceColumn = p;
            return parameter;
        }

        private void ServiceTimer_Tick(object sender, EventArgs e)
        {
            if (inProcess == 0)
            {
                ServiceTimer.Stop();
                SyncDatabases();
                CopyData();
                //InsertUsers(ids);
                ServiceTimer.Start();
            }

        }


        private void CopyData()
        {
            DataSet ds1 = new winservDS();
            DataSet ds2 = new In_Out_RecordsDS();

            foreach (DataRow item in ds1.Tables[0].Rows)
            {

                ds2.Tables[0].Rows.Add(item);


            }


        } here

1 个答案:

答案 0 :(得分:0)

尝试将DataReader用于源记录集,将所有列读入变量,然后使用INSERT语句将它们插入到第二个数据库中。复制数千条记录时,请避免使用您的事务日志...