读书人

定时气营一个windows service根据数

发布时间: 2012-09-10 11:02:33 作者: rapoo

定时运营一个windows service,根据数据库1中特定数据查找数据库2的记录
公司A系统,订单输入,包括客户信息,订单信息。输入完成后订单有一个状态“输入完成”。然后向公司B系统传递订单信息。

公司B系统,订单处理,处理完成也有一个状态“订单完成”。

问题来了,公司A每隔一段时间需要知道最近录入的新订单的最新状态,可是这个2个不同的系统和数据库(数据库在一个网中),要命的是,不共享订单ID。

所以,设计了一个windows service,
每隔一分钟在公司A数据库中找出状态是“输入完成" 的订单
然后循环这些订单,根据每一个订单的用户信息(名字),找出公司B数据库中状态是“订单完成”和订单用户名字一致的数据
然后,根据一些条件,用户姓名,性别,生日来判断找出的公司A和公司B的订单数据是描述的同一个订单
最后,根据公司B的订单状态,更新公司A订单的一些数据,比如状态更新成“即将发货”,还有发货时间等

下面是我的code,运行了一段时间,倒是没什么问题,就是变慢了一些,比如,订单001输入完成时间是1:00AM,然后“订单完成”是2:00AM,那么因为Service是每隔一分钟运行一次,公司A订单001状态改变成“即将发货”的时间应该是2:01AM,

可是,现在变成了2:04,还有延长的趋势。

会是哪里的问题呢?




namespace OrderStatusUpdate
{
public partial class OrderStatusUpdate: ServiceBase
{
private static System.Timers.Timer aTimer;
private string sSource = "OrderStatusUpdateService";
private string sLog = "Application";

public OrderStatusUpdate()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
System.Threading.Thread.Sleep(10000);

if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource, sLog);

aTimer = new System.Timers.Timer(10000);

aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

int interval = Convert.ToInt32(ConfigurationManager.AppSettings["Time"]);

aTimer.Interval = 1000 * 60 * interval;
aTimer.Enabled = true;

GC.KeepAlive(aTimer);
}

protected override void OnStop()
{
aTimer.Dispose();
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
SqlConnection connection = new SqlConnection();

try
{
connection.ConnectionString = connections["OrderConnectionString"].ConnectionString;
connection.Open();

string now = DateTime.Now.ToString("yyyy'-'MM'-'dd HH:mm:ss");

string queryStr1 = "..." // 在数据库1根据订单状态选择刚生成的订单

DataTable _order1 = new DataTable();
using (SqlCommand myCommand = new SqlCommand(queryStr1, connection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
_order .Load(myReader);
}
}

foreach (DataRow row in _order1.Rows)
{
DateTime _execTime = Convert.ToDateTime(row["CompleteTime"].ToString());
string queryStr2 = ""; //在数据库2中选择已完成的订单

using (SqlCommand command = new SqlCommand(queryStr2 , connection))
{
using (SqlDataReader myReader = command.ExecuteReader())
{
DataTable _order2 = new DataTable();
_order2.Load(myReader);

if (_order2.Rows.Count == 0)//数据库2中没有结果,查找下一个
continue;

if (_order2.Rows.Count > 1)


{
if (!selectFirstOne)//数据库2中发现多个不更新
continue;
}

if (matchName)
{
//客户名字不一样不更新
}

if (matchSex)
{
客户名字不一样不更新
}

if (matchBirthday)
{
生日不一样不更新
}

//程序到这里意味找到匹配,更新状态数据库1中订单状态。
command.CommandText =""

command.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry("OrderStatusUpdateService", ex.Message, EventLogEntryType.Error);
}
finally
{
connection.Close();
}
}
}
}


[解决办法]
string now = DateTime.Now.ToString("yyyy'-'MM'-'dd HH:mm:ss");

网络会不会有延时啊,

为什么你不直接给“即将发货”的时间”加上一分钟呢

[解决办法]
太正常了吧,你以为你设置一分钟,就精确到一分钟吗,网络状况也会引起执行的延时,包括数据库的数据量也会,这有什么奇怪的

读书人网 >C#

热点推荐