需求说明:
要求定时查询Doing表(待发短信)和短信猫中是否有数据,如果有信息则发送/接收。
表结构
Doing(ID MOBILID CONTEXT USERNAME DOTIME) --待发送的短信
Done(ID MOBILID CONTEXT SENDTIME USERNAME )--已发送的短信
Receive(ID MOBILID CONTEXT RECEIVETIME)--接受道的短信
GSMMultiPort.dll--控制猫的封装包(为提供)
里面有:
?‘初始化’,
?‘获取短信猫标识号码’,
?‘获取设备的连接状态’,
?‘断开连接并释放内存空间’,
?‘取得错误信息’,
?‘发送短信息’,
?‘接收短信息’等方法。
Service.cs代码
<----------------------------------------------------------------------------------------------->
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlTypes;
using System.Collections;
[WebService(Namespace = "此处为自己设定的XML命名空间")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
??? public string myConnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("lmtof.mdb");
??? protected OleDbConnection myconn;
??? protected OleDbCommand mycommand;
??? protected OleDbDataAdapter myda;
??? protected OleDbTransaction mytran;
??? protected DataSet ds;
??? public Service ()
??? {
??????? myconn = new OleDbConnection(myConnstring);
??????? //如果使用设计的组件,请取消注释以下行
??????? //InitializeComponent();????
??? }
??? #region //发信息
??? [WebMethod/*是否有数据要发送*/]
??? public string checkSMS()
??? {
??????? string selectsql = "SELECT * FROM doing";
??????? mycommand = new OleDbCommand(selectsql,myconn);
??????? try
??????? {
??????????? myconn.Open();
??????????? if (Convert.ToInt32(mycommand.ExecuteScalar()) > 0)
??????????? {
??????????????? return "true";
??????????? }
??????????? else
??????????? { return "false"; }
??????? }
??????? catch (Exception ee)
??????? {
??????????? return "ErrorWebService:"+ee.ToString();
??????? }
??????? finally { myconn.Close(); }
??? }
??? [WebMethod]
??? public DataSet getSMS(out string error)
??? {
??????? error = "";
??????? DataSet info=new DataSet();
??????? string selectsql = "SELECT CStr(id) + '|' + mobilid + '|'+context+'|'+username+'|'+cstr(dotime) FROM doing";
??????? try
??????? {
??????????? myda = new OleDbDataAdapter(selectsql,myconn);
??????????? myda.Fill(info);
??????????? return info;
??????? }
??????? catch (Exception ee)
??????? {
??????????? error = "ErrorWebService:" + ee.ToString();
??????????? return info;?????????
??????? }
??? }
??? [WebMethod]
??? //winform发送信息后,删除信息
??? //错误编码为‘ErrorWebService:’+ 错误信息
??? public string sendSMS(string uid,string id,string mobilid,string context,string sendtime,string username )
??? {
??????? if (uid != "oa.ftwsjd.gov.cn")
??????? {
??????????? return "access denied";
??????? }
??????? try
??????? {
??????????? //事务处理
??????????? //myconn.Open();
??????????? //mycommand = myconn.CreateCommand();
??????????? //mytran = myconn.BeginTransaction();
??????????? //mycommand.Transaction = mytran;
??????????? //mycommand.CommandText = "INSERT INTO done(mobilid,context,username,sendtime) VALUES ('" + mobilid + "','" + context + "','" + username + "','" + sendtime + "')";
??????????? //mycommand.ExecuteNonQuery();
??????????? //mycommand.CommandText = "DELETE FROM doing WHERE id=" + id;
??????????? //mycommand.ExecuteNonQuery();
??????????? //mytran.Commit();
??????????? return "true";
??????? }
??????? catch (Exception ee)
??????? {
??????????? return "ErrorWebService:" + ee.ToString();
??????? }
??????? finally
??????? {
??????????? myconn.Close();
??????? }
??? }
??? #endregion
??? #region//收信息??? [WebMethod]??? //接收 SMS 插入Receive表中??? //错误编码为‘ErrorWebService:’+ 错误信息??? public string ReceiveSMS(string MOBILID,string CONTEXT,DateTime RECEIVETIME)??? {??????? try??????? {??????????? myconn.Open();??????????? mycommand = myconn.CreateCommand();?????????? ??????????? mytran = myconn.BeginTransaction();??????????? mycommand.Transaction = mytran;??????????? mycommand.CommandText = "INSERT INTO receive(MOBILID,CONTEXT,RECEIVETIME) VALUES ('" + MOBILID + "','" + CONTEXT + "','" + RECEIVETIME + "')";??????????? mycommand.ExecuteNonQuery();??????????? mytran.Commit();??????????? return "true";??????? }??????? catch (Exception ee)??????? {??????????? return "ErrorWebService:" + ee.ToString();??????? }??????? finally??????? { myconn.Close(); }??? }??? #endregion}<----------------------------------------------------------------------------------------------->