我必须准备一个程序,每当计算机崩溃时,该程序就会发送SMS。 总体思路:
所以-我有一个侦听器,它检查是否在DBO中添加了任何新行。这非常好用:D(class:SqlDependencyEx)。每当发生更改时,我都会调用以下方法:
listener_andon_actions.TableChanged += (o, e) => SendToBuffer();
这是SendToBuffer()方法的一般概念:
SendToBuffer()
{
// connect to DB and put the data from query into DataTable. Then - put every row from this temporary DataTable to generalSMSBuffer
SqlConnection cnn;
cnn = new SqlConnection(connectionString);
string query = @"god_damn_long_query_here"
SqlCommand cmd = new SqlCommand(query, cnn);
// this last_id is used in query - to make sure I never grab for the same row twice :#
cmd.Parameters.AddWithValue("@last_id", lastID);
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
//put query result into tempDB
da.Fill(tempDB);
// add data from temporary DataTable (it will always be just one row, but I use foreach anyways...) to a generalSMSBuffer
foreach (var row in tempDB.Rows)
{
generalSMSBuffer.Rows.Add(tempDB.Rows[i]["phone_number"], tempDB.Rows[i]["name"], tempDB.Rows[i]["line_number"]);
// set lastID like an ID of last row added to generalBuffer (mentioned before)
lastID = int.Parse(tempDB.Rows[i]["id"].ToString());
i++;
}
tempDB.Clear();
}
好吧,我接下来要做的就是-每次将新行添加到generalSMSBuffer(这是一个DataTable)时,我都会引发一个事件:
protected void DataTable_RowChanged(object sender, DataRowChangeEventArgs e)
{
generalSMSBuffer = (DataTable)sender;
// only if a new row was ADDED
if (e.Action == DataRowAction.Add)
{
SendSMSesFromBuffer();
}
}
这是SendSMSesFromBuffer()方法:
public void SendSMSesFromBuffer()
{
foreach (DataRow row in generalSMSBuffer.Rows)
{
// sending SMS - with AT Commands through SerialPort
SendSMS(row["phone_number"].ToString(), row["name"].ToString(), row["line_number"].ToString());
}
}
这是一个问题:它可以工作(现在实际上正在工作),但是每次发生新的行故障时,都会向我发送generalSMSBuffer DataTable中的所有行。
我想我应该在发送完行后从generalSMSBuffer中删除行(我有机会从SMS发送器中设置诸如“忙碌标志”之类的东西)。
我该怎么做,以便仅在有缓冲区的情况下才发送SMS?我应该用计时器检查一下吗?
此方法SendSMSesFromBuffer()很糟糕...每次添加新行时我都会遍历每一行...想象一下当我发送SMS时添加20个新崩溃的情况。 ..
也许整个想法是错误的,如果没有多个线程[*]
很多东西,我想我的解释很复杂,但是任何提示都会帮助我...谢谢!