关于C#应用的问题 [英] Question about c# app

查看:76
本文介绍了关于C#应用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用带有SQL的C#应用​​程序工作.该项目是关于管理从特定设备接收的消息,将它们存储在GSM调制解调器的SIM卡和数据库的特殊表中.是我将收到的短信保存到数据库后无法立即删除它们吗?有人可以提供任何建议吗? 主要"形式的代码如下:


Hi,

I''m working in an app with c# an SQL.The project is about managing messages received from a certain device, storing them in the SIM card of a GSM modem and a spesific table of a database.The app works fine.The problem is that I can''t delete the incoming messages from the sim card right after they are saved in the database.Can anyone give any advise? The code of the "main" form is the following:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using GsmComm.GsmCommunication;
using GsmComm.PduConverter;
using System.Threading;
using System.Text.RegularExpressions;
using System.Data.SqlClient;



namespace SMS
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{

private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Label lbl_phone_status;
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.MenuItem mnu_send;
private System.Windows.Forms.MenuItem mnu_read;
private delegate void SetTextCallback(string text);
private CommSetting comm_settings=new CommSetting();
private System.Windows.Forms.MenuItem mnudelete;
private System.Windows.Forms.Timer timer1;
private IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnu_send = new System.Windows.Forms.MenuItem();
this.mnu_read = new System.Windows.Forms.MenuItem();
this.mnudelete = new System.Windows.Forms.MenuItem();
this.lbl_phone_status = new System.Windows.Forms.Label();
this.txtOutput = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnu_send,
this.mnu_read,
this.mnudelete});
this.menuItem1.Text = "Message";
//
// mnu_send
//
this.mnu_send.Index = 0;
this.mnu_send.Text = "Send";
this.mnu_send.Click += new System.EventHandler(this.mnu_send_click);
//
// mnu_read
//
this.mnu_read.Index = 1;
this.mnu_read.Text = "Read";
this.mnu_read.Click += new System.EventHandler(this.mnu_read_click);
//
// mnudelete
//
this.mnudelete.Index = 2;
this.mnudelete.Text = "Delete";
this.mnudelete.Click += new System.EventHandler(this.mnudelete_Click);
//
// lbl_phone_status
//
this.lbl_phone_status.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windo ws.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.lbl_phone_status.BackColor = System.Drawing.Color.White;
this.lbl_phone_status.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lbl_phone_status.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lbl_phone_status.ForeColor = System.Drawing.Color.Red;
this.lbl_phone_status.Location = new System.Drawing.Point(360, 368);
this.lbl_phone_status.Name = "lbl_phone_status";
this.lbl_phone_status.Size = new System.Drawing.Size(196, 23);
this.lbl_phone_status.TabIndex = 54;
this.lbl_phone_status.Text = "NO PHONE CONNECTED";
this.lbl_phone_status.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lbl_phone_status.UseMnemonic = false;
//
// txtOutput
//
this.txtOutput.Enabled = false;
this.txtOutput.Location = new System.Drawing.Point(88, 72);
this.txtOutput.Multiline = true;
this.txtOutput.Name = "txtOutput";
this.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtOutput.Size = new System.Drawing.Size(472, 272);
this.txtOutput.TabIndex = 55;
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(88, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 24);
this.label1.TabIndex = 56;
this.label1.Text = "Received SMS :";
//
// timer1
//
this.timer1.Interval = 15000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(656, 417);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.lbl_phone_status);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n;
this.Text = "SMS Server";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form 1_Closing);
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


private void Form1_Load(object sender, System.EventArgs e)
{
// Prompt user for connection settings
int port = GsmCommMain.DefaultPortNumber;
int baudRate = 9600; // We Set 9600 as our Default Baud Rate
int timeout = GsmCommMain.DefaultTimeout;

frmConnection dlg = new frmConnection();
dlg.StartPosition = FormStartPosition.CenterScreen;
dlg.SetData(port, baudRate, timeout);
timer1.Enabled = true;
if (dlg.ShowDialog(this) == DialogResult.OK)
{
dlg.GetData(out port, out baudRate, out timeout);
CommSetting.Comm_Port=port;
CommSetting.Comm_BaudRate=baudRate;
CommSetting.Comm_TimeOut=timeout;
}
else
{
Close();
return;
}

Cursor.Current = Cursors.WaitCursor;
CommSetting.comm = new GsmCommMain(port, baudRate, timeout);
Cursor.Current = Cursors.Default;
CommSetting.comm.PhoneConnected += new EventHandler(comm_PhoneConnected);
CommSetting.comm.MessageReceived+=new MessageReceivedEventHandler(comm_MessageReceived);

bool retry;
do
{
retry = false;
try
{
Cursor.Current = Cursors.WaitCursor;
CommSetting.comm.Open();
Cursor.Current = Cursors.Default;
}
catch(Exception)
{
Cursor.Current = Cursors.Default;
if (MessageBox.Show(this, "Unable to open the port.", "Error",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
retry = true;
else
{
Close();
return;
}
}
}
while(retry);

}


private delegate void ConnectedHandler(bool connected);

private void OnPhoneConnectionChange(bool connected)
{
lbl_phone_status.Text="CONNECTED";
}


private void comm_MessageReceived(object sender, GsmComm.GsmCommunication.MessageReceivedEventArgs e)
{
MessageReceived();
}

private void comm_PhoneConnected(object sender, EventArgs e)
{
this.Invoke(new ConnectedHandler(OnPhoneConnectionChange), new object[] { true });
}


private string GetMessageStorage()
{
string storage = string.Empty;
storage = PhoneStorageType.Sim;

if (storage.Length == 0)
throw new ApplicationException("Unknown message storage.");
else
return storage;
}


private void MessageReceived()
{
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();

DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.R eceivedUnread, storage);
foreach(DecodedShortMessage message in messages)
{
Output(string.Format("Message status = {0}, Location = {1}/{2}",
StatusToString(message.Status), message.Storage, message.Index));
//ShowMessage(message.Data);
Output("");
}

Output(string.Format("{0,9} messages read.", messages.Length.ToString()));
Output("");

}


private string StatusToString(PhoneMessageStatus status)
{
// Map a message status to a string
string ret;
switch(status)
{
case PhoneMessageStatus.All:
ret = "All";
break;
case PhoneMessageStatus.ReceivedRead:
ret = "Read";
break;
case PhoneMessageStatus.ReceivedUnread:
ret = "Unread";
break;
case PhoneMessageStatus.StoredSent:
ret = "Sent";
break;
case PhoneMessageStatus.StoredUnsent:
ret = "Unsent";
break;
default:
ret = "Unknown (" + status.ToString() + ")";
break;
}
return ret;
}


private void Output(string text)
{
if (this.txtOutput.InvokeRequired)
{
SetTextCallback stc = new SetTextCallback(Output);
this.Invoke(stc, new object[] { text });
}
else
{
txtOutput.AppendText(text);
txtOutput.AppendText("\r\n");
}
}


private void ShowMessage(SmsPdu pdu, object sender)
{
if (pdu is SmsSubmitPdu)
{
// Stored (sent/unsent) message
SmsSubmitPdu data = (SmsSubmitPdu)pdu;
Output("SENT/UNSENT MESSAGE");
Output("Recipient: " + data.DestinationAddress);
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");
return;
}
if (pdu is SmsDeliverPdu)
{
// Received message
SmsDeliverPdu data = (SmsDeliverPdu)pdu;
Output("RECEIVED MESSAGE");
Output("Sender: " + data.OriginatingAddress);
Output("Sent: " + data.SCTimestamp.ToString());
Output("Message text: " + data.UserDataText);
Output("-------------------------------------------------------------------");

// send to db2

data = (SmsDeliverPdu)pdu;
string s = data.UserDataText;
string p = data.SCTimestamp.ToString();
string g = data.OriginatingAddress.ToString();
string[] words = s.Split(';', ',');
foreach (string word in words)
{
SqlConnection myConnection = new SqlConnection(@"Network Library=DBMSSOCN;user id=sa;
password=******;server=CSOPER3\SQLEXPRESS;
Trusted_Connection=yes;
database=My_Test1;
connection timeout=30;");
try
{
myConnection.Open();

}
catch (SqlException e)
{
Console.WriteLine(e.Message);
throw e;

}

{
SqlCommand myCommand = new SqlCommand("INSERT INTO db2 (sender,time,ID,IMEI,LOCAL_TIME,LOCAL_DATE,BATTERY ,LAT,LON,ANGLE,SATELLITE,ALARM_ON,KEY_ID) " +
"values('" + g + "','" + p + "','" + words[0] + "','" + words[1] + "','" + words[2] + "','" + words[3] + "','" + words[4] + "','" + words[5] + "','" + words[6] + "','" + words[7] + "','" + words[8] + "','" + words[9] + "','" + words[10] + "')", myConnection);


myCommand.Connection = myConnection;

myCommand.ExecuteNonQuery();


try
{
myConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine(e.Message);

throw e;
}

}

}

if (pdu is SmsStatusReportPdu)
{
// Status report
SmsStatusReportPdu data = (SmsStatusReportPdu)pdu;
Output("STATUS REPORT");
Output("Recipient: " + data.RecipientAddress);
Output("Status: " + data.Status.ToString());
Output("Timestamp: " + data.DischargeTime.ToString());
Output("Message ref: " + data.MessageReference.ToString());
Output("-------------------------------------------------------------------");
return;
}
Output("Unknown message type: " + pdu.GetType().ToString());
}

private void mnu_send_click(object sender, System.EventArgs e)
{
Send send_sms=new Send();
send_sms.Show();
}


private void mnu_read_click(object sender, System.EventArgs e)
{
Receive receice_sms=new Receive();
receice_sms.Show();
}


private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Clean up comm object
if (CommSetting.comm != null)
{
// Unregister events
CommSetting.comm.PhoneConnected -= new EventHandler(comm_PhoneConnected);
CommSetting.comm.MessageReceived -= new MessageReceivedEventHandler(comm_MessageReceived);

// Close connection to phone
if (CommSetting.comm != null && CommSetting.comm.IsOpen())
CommSetting.comm.Close();

CommSetting.comm = null;
}
}

private void mnudelete_Click(object sender, System.EventArgs e)
{
Delete delete=new Delete();
delete.Show();
}

private void timer1_Tick(object sender, EventArgs e)
{
MessageReceived();
}

}
}




谢谢!




Thanks!

推荐答案

哇,转储了很多代码.

您是否已调试代码并尝试查找问题?如果有的话,您实际在哪一部分出现问题?另外,您在代码的某些部分中遇到错误还是产生了意外结果?
Wow, dumped a whole lot of code.

Have you debugged the code and tried to find the problem? If you have, in what part are you actually having the problem? Also are you getting errors or just having unexpected results in some part of the code?


这篇关于关于C#应用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆