搜索
您的当前位置:首页正文

1C#短信猫连接发送接收等代码1

来源:星星旅游
usingSystem;

usingSystem.IO.Ports;

usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Threading;usingSystem.Drawing;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Text;

usingSystem.Windows.Forms;

namespaceylModem.Modem{

publicpartialclassModemLink:UserControl{

publicModemLink(){

InitializeComponent();

txtCenterNO.Text=System.Configuration.ConfigurationManager.AppSettings[\"CenterNo\"];

}

publicstaticintTotalNum=Int32.Parse(System.Configuration.ConfigurationManager.AppSettings[\"TotalNum\"]);

publicstaticintUsedNum=Int32.Parse(System.Configuration.ConfigurationManager.AppSettings[\"UsedNum\"]);

publicstaticintLastDay=Int32.Parse(System.Configuration.ConfigurationManager.AppSettings[\"LastDay\"]);

publicstaticintLastHour=Int32.Parse(System.Configuration.ConfigurationManager.AppSettings[\"LastHour\"]);

publicstaticstringcenterNO=\"\";

publicstaticComPortClass.ComPortClasssp=null;

publicstaticboolloginStatus=false;

privatevoidbtnConnect_Click(objectsender,EventArgse){

sp=newComPortClass.ComPortClass(cmbPortName.Text.Trim(),Convert.ToInt32(cmbPortNum.Text));cmbPortName.Enabled=false;cmbPortNum.Enabled=false;btnConnect.Enabled=false;btnClose.Enabled=true;if(sp.sp.IsOpen){

loginStatus=true;

this.rtbDisplay.Text=sp.SetMsgCenter(txtCenterNO.Text.Trim());centerNO=txtCenterNO.Text.Trim();

sp.sp.DataReceived+=newSerialDataReceivedEventHandler(ReceiveMessage);

}else{

rtbDisplay.Text=\"串口打开失败!\";

}

}

privatevoidbtnClose_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";sp.CloseCom();loginStatus=false;cmbPortName.Enabled=true;cmbPortNum.Enabled=true;btnConnect.Enabled=true;btnClose.Enabled=false;

}

privatevoidbtnCenterNO_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";if(loginStatus){

rtbDisplay.Text=sp.ATCommand(\"AT+CSCA?\");

}else{

rtbDisplay.Text=\"请先连接短信猫!\";

}

}

privatevoidbtnSIM_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";if(loginStatus){

rtbDisplay.Text=sp.ATCommand(\"AT+CCID\");

}else{

rtbDisplay.Text=\"请先连接短信猫!\";

}

}

privatevoidbtnSetCenterNo_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";if(loginStatus){

AppConfigconfig=newAppConfig();

config.ConfigType=(int)ConfigFileType.AppConfig;

config.SetValue(\"CenterNoxtCenterNO.Text.Trim());

centerNO=txtCenterNO.Text.Trim();

rtbDisplay.Text=sp.ATCommand(\"AT+CSCA=\"+txtCenterNO.Text.Trim()+\";&W\");

}else{

rtbDisplay.Text=\"请先连接短信猫!\";

}

}

privatevoidbtnTest_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";if(loginStatus){

rtbDisplay.Text=sp.ATCommand(\"AT+CSQ\");

}else{

rtbDisplay.Text=\"请先连接短信猫!\";

}

}

privatevoidbtnPhoneNum_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";if(loginStatus){

rtbDisplay.Text=sp.ATCommand(\"AT+CNUM\");

}else

{

rtbDisplay.Text=\"请先连接短信猫!\";

}

}

privatevoidbtnClear_Click(objectsender,EventArgse){

rtbDisplay.Text=\"\";

}

privatevoidReceiveMessage(objectsender,SerialDataReceivedEventArgse){

try{

Thread.Sleep(1000);stringstr=sp.ReadCom();Thread.Sleep(2000);

str=str.Replace(\"/r/n\\"\").Replace(\"\\"\");if(str.Length>6){

if(str.Substring(0,5)==\"+CMTI\"){

intindex=str.IndexOf(\

stringlocation=str.Substring(index+1);stringvalue=sp.ReadMsg(location);

//MessageBox.Show(value,\"有新短信\MessageBoxButtons.OK,

MessageBoxIcon.Information);

if(value.Substring(0,3)==\"内容:\"){

InsertReceive(value);

}

sp.ATCommand(\"AT+CMGD=\"+location);

}

}

}

catch(Exceptionex){

MessageBox.Show(ex.Message);

}

}

privatevoidInsertReceive(stringpdu){

Stringsendmsg=pdu.Substring(pdu.IndexOf(\"内容:\")+3,pdu.IndexOf(\"---发送者:\")-pdu.IndexOf(\"内容:

\")-3);

sendmsg=sendmsg.Replace(\"/0\\"\");

Stringsendmobile=pdu.Substring(pdu.IndexOf(\"---发送者:\")+7,pdu.IndexOf(\"---接收时间:\")-pdu.IndexOf(\"---发送者:\")-7);

Stringreceivetime=pdu.Substring(pdu.IndexOf(\"---接收时间:\")+8);

OleDbConnectionmyConnection=newOleDbConnection(ylModem.main.DBConfig);myConnection.Open();

OleDbDataAdapteradapter=newOleDbDataAdapter();

adapter.SelectCommand=newOleDbCommand(\"selectphrase.receivefromphrase,phrasegroupwhere

phrasegroup.id=phrase.groupidandphrasegroup.status=1andphrase.phrase='\"+sendmsg.Trim()+\"'\myConnection);

DataSetds=newDataSet();adapter.Fill(ds,\"receive\");

if(ds.Tables[\"receive\"].Rows.Count==0){

OleDbCommandmyCommand=myConnection.CreateCommand();

OleDbTransactionmyTrans=myConnection.BeginTransaction(IsolationLevel.ReadCommitted);

myCommand.Transaction=myTrans;

myCommand.CommandText=\"INSERTINTOreceivemsg(sendmobile,sendmsg,receivetime,receivestatus)

values('\"+sendmobile+\"','\"+sendmsg+\"','\"+receivetime+\"','无回复')\";

myCommand.ExecuteNonQuery();myTrans.Commit();

myCommand.Dispose();myTrans.Dispose();

}else{

StringreceiveStatus=InsertSend(sendmobile,(String)ds.Tables[\"receive\"].Rows[0][\"receive\"]);

OleDbCommandmyCommand=myConnection.CreateCommand();

OleDbTransactionmyTrans=myConnection.BeginTransaction(IsolationLevel.ReadCommitted);

myCommand.Transaction=myTrans;

myCommand.CommandText=\"INSERTINTOreceivemsg(sendmobile,sendmsg,receivetime,receivestatus)

values('\"+sendmobile+\"','\"+sendmsg+\"','\"+receivetime+\"','\"+receiveStatus+\"')\";

myCommand.ExecuteNonQuery();myTrans.Commit();

myCommand.Dispose();myTrans.Dispose();

}

ds.Dispose();

adapter.Dispose();myConnection.Close();

}

privatestringInsertSend(stringsendMobile,stringsendMsg){

if(LastDay!=System.DateTime.Now.Day||LastHour!=System.DateTime.Now.Hour){

UsedNum=0;

LastDay=System.DateTime.Now.Day;LastHour=System.DateTime.Now.Hour;

}

if(UsedNumstringsendresult=sp.SendToCom(sendMobile.Trim(),centerNO.Trim(),sendMsg.Trim());

if(sendresult.Contains(\"OK\")){

UsedNum++;

OleDbConnectionmyConnection=newOleDbConnection(ylModem.main.DBConfig);myConnection.Open();

OleDbCommandmyCommand=myConnection.CreateCommand();

OleDbTransactionmyTrans=myConnection.BeginTransaction(IsolationLevel.ReadCommitted);

myCommand.Transaction=myTrans;

myCommand.CommandText=\"INSERTINTOsendreport(sendmobile,sendmsg,sendstatus)values('\"

+sendMobile+\"','\"+sendMsg+\"','发送成功')\";

myCommand.ExecuteNonQuery();myTrans.Commit();

myCommand.Dispose();myTrans.Dispose();myConnection.Close();

return\"回复成功\";

}else{

OleDbConnectionmyConnection=newOleDbConnection(ylModem.main.DBConfig);myConnection.Open();

OleDbCommandmyCommand=myConnection.CreateCommand();

OleDbTransactionmyTrans=myConnection.BeginTransaction(IsolationLevel.ReadCommitted);

myCommand.Transaction=myTrans;

myCommand.CommandText=\"INSERTINTOsendreport(sendmobile,sendmsg,sendstatus)values('\"

+sendMobile+\"','\"+sendMsg+\"','发送失败')\";

myCommand.ExecuteNonQuery();}

}

}

myTrans.Commit();

myCommand.Dispose();myTrans.Dispose();myConnection.Close();

return\"回复失败\";

}

}else{

return\"未回复\";

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/long102/archive/2007/10/31/1859309.aspx

如今手机已成为大众的交流工具。有关手机的程序开发越来越广泛,本节通过几个典型实例介绍如何利用短信猫发送、接收短信、远程控制计算机、业务员销售数据采集和短信息娱乐互动平台。实例431利用短信猫收发短信息实例说明

短信猫是利用SIM卡发送短信的硬件设备,通过串口或USB接口(根据设备型号而定)与计算机相连。在程序中可以利用短信猫发送或接收短信。本例实现了利用短信猫收发短信息的功能。实例运行结果如图13.15所示。技术要点

本例使用的是北京人大金仓信息技术有限公司的串口短信猫。在购买短信猫时会附带包括SDK的开发包,其中提供了操作短信猫的函数(封装在dllforvc.dll动态库中)。下面介绍操作短信猫的主要函数。(1)GSMModemGetSnInfoNew函数

该函数获取短信猫注册需要的信息,代码如下:

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetSnInfoNew\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetSnInfoNew(stringdevice,stringbaudrate);

参数说明如下。ll

device:通信端口,为null时系统会自动检测。baudrate:通讯波特率,为null时系统会自动检测。

(2)GSMModemGetDevice函数

该函数获取当前的通讯端口,代码如下:

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetDevice\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetDevice();

(3)GSMModemGetBaudrate函数

该函数获取当前的通讯波特率,代码如下:

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetBaudrate\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetBaudrate();

(4)GSMModemInitNew函数

该函数用于初始化短信猫。语法如下:

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemInitNew\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolGSMModemInitNew(

stringdevice,stringbaudrate,stringinitstring,

stringcharset,boolswHandshake,stringsn);

参数说明如下。llllll

device:标识通信端口,如果为NULL,系统会自动检测。baudrate:标识通讯波特率,如果为NULL,系统会自动检测。initstring:标识初始化命令,为NULL即可。charset:标识通讯字符集,为NULL即可。

swHandshake:标识是否进行软件握手,为False即可。sn:标识短信猫的授权号,需要根据实际情况填写。

(5)GSMModemSMSsend函数

该函数用于发送手机短信。语法如下:

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemSMSsend\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolGSMModemSMSsend(

stringserviceCenterAddress,intencodeval,stringtext,inttextlen,stringphonenumber,boolrequestStatusReport);

参数说明如下。llllll

serviceCenterAddress:标识短信中心号码,为NULL即可。encodeval:标识短信息编码格式,如果为8,表示中文短信编码。text:标识短信内容。

textlen:标识短信内容的长度。

phonenumber:标识接收短信的电话号码。requestStatusReport:标识状态报告。

(6)GSMModemSMSReadAll函数

该函数取得所有短信息,包括SIM卡和手机中的短信息。返回的短信内容格式为电话号码1|短信内容1||电话号码2|短信内容2||:

//接收短信息返回字符串格式为:手机号码|短信内容||手机号码|短信内容||//RD_opt为1表示接收短信息后不做任何处理,为0表示接收后删除信息[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemSMSReadAll\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemSMSReadAll(intRD_opt);

参数说明如下。l实现过程

(1)新建一个项目,命名为Ex13_14,默认窗体为Form1。

(2)在Form1窗体中,主要添加TextBox控件和Label控件,控件的数量及用途如图13.15所示,添加两个Button控件,分别用于发送短信息和

RD_opt:对读取后的短信息进行处理,0表示删除,1表示不做处理。

接收短信息。(3)主要程序代码。

将所使用的函数封装在GMS类中。代码如下:classGMS{

//初始化gsmmodem,并连接gsmmodem[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemInitNew\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolGSMModemInitNew(

stringdevice,stringbaudrate,stringinitstring,stringcharset,boolswHandshake,stringsn);

//获取短信猫新的标识号码[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetSnInfoNew\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetSnInfoNew(stringdevice,stringbaudrate);//获取当前通讯端口[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetDevice\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetDevice();//获取当前通讯波特率[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetBaudrate\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetBaudrate();//断开连接并释放内存空间[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemRelease\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternvoidGSMModemRelease();//取得错误信息

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemGetErrorMsg\

CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemGetErrorMsg();//发送短信息

[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemSMSsend\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolGSMModemSMSsend(

stringserviceCenterAddress,intencodeval,stringtext,inttextlen,stringphonenumber,boolrequestStatusReport);

//接收短信息返回字符串格式为:手机号码|短信内容||手机号码|短信内容||//RD_opt为1接收短信息后不做任何处理,0为接收后删除信息[DllImport(\"dllforvc.dll\

EntryPoint=\"GSMModemSMSReadAll\CharSet=CharSet.Ansi,

CallingConvention=CallingConvention.StdCall)]

publicstaticexternstringGSMModemSMSReadAll(intRD_opt);

}

在装载Form1窗体时,获取设备信息。代码如下:

privatevoidForm1_Load(objectsender,EventArgse){

//机器号码,当参数为空时,函数自动获取设备信息

txtJQHM.Text=GMS.GSMModemGetSnInfoNew(txtCOM.Text,txtBTL.Text);txtCOM.Text=GMS.GSMModemGetDevice();txtBTL.Text=GMS.GSMModemGetBaudrate();

}

发送短信息。代码如下:

privatevoidbtnSend_Click(objectsender,EventArgse){

if(txtSJHM.Text==\"\"){

MessageBox.Show(\"手机号码不能为空!\MessageBoxButtons.OK);

txtSJHM.Focus();return;

}

if(txtContent.Text==\"\"){

MessageBox.Show(\"短信内容不能为空!\\"提示\MessageBoxButtons.OK);

txtContent.Focus();

//COM1//波特率

return;

}

//连接设备

if(GMS.GSMModemInitNew(txtCOM.Text,txtBTL.Text,null,null,false,txtPower.Text)==false){

MessageBox.Show(\"设备连接失败!\"+GMS.GSMModemGetErrorMsg(),\"提示\MessageBoxButtons.OK);return;

}

//发送短信

if(GMS.GSMModemSMSsend(null,8,txtContent.Text,

Encoding.Default.GetByteCount(txtContent.Text),txtSJHM.Text,false)==true)

MessageBox.Show(\"短信发送成功!\\"提示\MessageBoxButtons.OK);

else

MessageBox.Show(\"短信发送失败!\"+GMS.GSMModemGetErrorMsg(),\"提示\MessageBoxButtons.OK);

}

接收短信息。代码如下:

privatevoidbtnResvice_Click(objectsender,EventArgse){

//1)连接设备

if(GMS.GSMModemInitNew(txtCOM.Text,txtBTL.Text,null,null,false,txtPower.Text)==false){

MessageBox.Show(\"连接失败!\"+GMS.GSMModemGetErrorMsg(),\"提示\MessageBoxButtons.OK);return;

}

//2)接收短信

txtContent.Text=GMS.GSMModemSMSReadAll(1);txtSJHM.Text=txtContent.Text.Substring(0,13);

txtContent.Text=txtContent.Text.Substring(13,txtContent.Text.Length-13);

}

举一反三

根据本实例,读者可以开发以下程序。利用短信猫群发短信。

办公自动化系统,办公短信通知、短信日程提醒、应急信息短信发布和短信审批等。实例432利用短信远程关闭计算机实例说明

本例实现了利用短信远程关闭计算机的功能。运行程序,首先,进行关机信息设置;然后,开启服务;最后,通过手机向短信猫发送“关机”数据。片刻之后,指定的计算机将会自动关机。程序如图13.16所示。技术要点

相关函数请参见实例“利用短信猫收发短信息”中的技术要点。实现过程

(1)新建一个项目,命名为Ex13_15,默认窗体为Form1。

(2)在Form1窗体中,主要添加TextBox控件和Label控件,控件的数量及用途如图13.16所示,添加一个Button控件,用于开启或停止远程关闭计算机服务。(3)主要程序代码。

privatevoidForm1_Load(objectsender,EventArgse){

//机器号码

txtJQHM.Text=GMS.GSMModemGetSnInfoNew(txtCOM.Text,txtBTL.Text);txtCOM.Text=GMS.GSMModemGetDevice();txtBTL.Text=GMS.GSMModemGetBaudrate();labStatus.Text=\"服务关闭中。。。。。\";

}

privatevoidClose_Windows(){

try{

//指定生成WMI连接所需的所有设置

ConnectionOptionsop=newConnectionOptions();op.Username=txtUser.Text;//远程计算机用户名称op.Password=txtPWD.Text;//设置操作管理范围

ManagementScopescope=newManagementScope(\"\\\\\\\\\"+txtIP.Text+\"\\\\root\\\\cimv2\op);

scope.Connect();//将此ManagementScope连接到实际的WMI范围。

ObjectQueryoq=newObjectQuery(\"SELECT*FROMWin32_OperatingSystem\");

ManagementObjectSearcherquery=newManagementObjectSearcher(scope,oq);

//得到WMI控制

ManagementObjectCollectionqueryCollection=query.Get();foreach(ManagementObjectobjinqueryCollection){

obj.InvokeMethod(\"ShutDown\null);//执行关闭远程计算机}

}

catch(Exceptionex){

Processp=newProcess();p.StartInfo.FileName=\"cmd.exe\";p.StartInfo.UseShellExecute=false;p.StartInfo.RedirectStandardInput=true;p.StartInfo.RedirectStandardOutput=true;p.StartInfo.RedirectStandardError=true;p.StartInfo.CreateNoWindow=true;p.Start();

p.StandardInput.WriteLine(\"shutdown/s\");p.StandardInput.WriteLine(\"exit\");

}

}

privatevoidtimer1_Tick(objectsender,EventArgse){

//远程计算机用户密码//COM1//波特率

//连接设备

if(GMS.GSMModemInitNew(txtCOM.Text,txtBTL.Text,null,null,false,txtPower.Text)==false){

MessageBox.Show(\"连接失败!\"+GMS.GSMModemGetErrorMsg(),\"提示\MessageBoxButtons.OK);return;

}

//接收短信

stringstr=GMS.GSMModemSMSReadAll(1);if(str==null)

return;

if(str.Substring(str.IndexOf(\"|\")+1,2)==\"关机\"){

this.Close_Windows();

}

}

privatevoidbutton1_Click(objectsender,EventArgse){

if(button1.Text==\"开启服务\"){

timer1.Enabled=true;

labStatus.Text=\"关机命令采集中。。。。。\";button1.Text=\"停止服务\";

}else{

timer1.Enabled=false;button1.Text=\"开启服务\";

labStatus.Text=\"服务关闭中。。。。。\";

}

}

举一反三

根据本实例,读者可以实现以下功能。利用短信实现客户资料查询。

保险行业中:保单查询、续费提醒、客户生日提醒和保费计算等。实例433短信息采集烟草销售数据实例说明

在各类销售行业中,产品销售数据量是企业不可缺少的一项数据。当销售人员在外地出差并且在没有计算机的情况下,如何及时的将销售数据汇报到公司中呢?

本例实现利用短信息采集烟草销售数据的功能。销售人员根据规定的格式编辑短信发送到短信息猫中即可。运行程序,单击【烟草销售信息采集】按钮采集烟草销售数据,然后单击【统计】按钮,将销售数据整理出来。如图13.17所示。技术要点

相关函数请参见实例“利用短信猫收发短信息”中的技术要点。

另外,程序规定的编辑短信息格式为,以冒号“:”分隔并结束。例如“张三:业务员:12:长春:3400:”。其中,主要使用String.Split()方法将信息数据拆分。

图13.17短信息采集烟草销售数据

String.Split()方法:返回包含此实例中的子字符串(由指定Char数组的元素分隔)的String数组。语法:

publicstring[]Split(

paramschar[]separator

)

参数说明如下。ll实现过程

(1)新建一个项目,命名为Ex13_16,默认窗体为Form1。

(2)在Form1窗体中,主要添加TextBox控件和Label控件,控件的数量及用途如图13.17所示,添加3个Button控件,分别用于发送短信息、采集销售数据和整理采集数据,添加两个DataGridView表格,分别用于显示短信息内容和整理后的销售数据。(3)主要程序代码。

单击【烟草销售信息采集】按钮,接受短信息并保存到数据库中。代码如下:

privatevoidbtnResvice_Click(objectsender,EventArgse){

//连接设备

if(GMS.GSMModemInitNew(txtCOM.Text,txtBTL.Text,null,null,false,txtPower.Text)==false){

MessageBox.Show(\"连接失败!\"+GMS.GSMModemGetErrorMsg(),\"提示\MessageBoxButtons.OK);return;

}

//接收短信

stringcontent=GMS.GSMModemSMSReadAll(0);if(content==null){

this.getMessage();return;

}

content=content.Replace(\"||\\"|\");//替换||为|string[]str_sp=content.Split('|');//进行分离intk=0;

dataGridView1.ColumnCount=2;

dataGridView1.RowCount=str_sp.Length/2;dataGridView1.Columns[0].HeaderText=\"手机号码\";dataGridView1.Columns[1].HeaderText=\"短信息\";for(inti=0;ifor(intj=0;j<2;j++){

dataGridView1[j,i].Value=str_sp[k];if(k%2!=0)

this.InsertMessage(\"insertintoRecivedBox(Mobile,Content,reciveTime)values('\"+

separator:分隔此实例中子字符串的Unicode字符数组,不包含分隔符的空数组或空引用。

返回值:一个数组,其元素包含此实例中的子字符串,这些子字符串由separator中的一个或多个字符分隔。

Convert.ToString(dataGridView1[0,i].Value)+\"','\"+Convert.ToString(dataGridView1[1,i].Value)+\"','\"+DateTime.Now.ToString()+\"')\");

k++;

}

}

this.getMessage();

}

自定义方法getSplitMessage()用来拆分短信息并且整理为正规数据。代码如下:

privatevoidgetSplitMessage(){

stringcontent=\"\";

for(inti=0;icontent=content+Convert.ToString(dataGridView1[\"短信息\i].Value);

}

string[]str_sp=content.Split(':');//进行分离intk=0;

dataGridView2.ColumnCount=5;

dataGridView2.RowCount=str_sp.Length/5;dataGridView2.Columns[0].HeaderText=\"姓名\";dataGridView2.Columns[1].HeaderText=\"职务\";dataGridView2.Columns[2].HeaderText=\"月份\";dataGridView2.Columns[3].HeaderText=\"销售地区\";dataGridView2.Columns[4].HeaderText=\"销售数量\";for(inti=0;ifor(intj=0;j<5;j++){

dataGridView2[j,i].Value=str_sp[k];k++;

}

}

}

自定义InsertMessage()方法,将接受的短信息保存到数据库中。代码如下:

privatevoidInsertMessage(stringstrSql){

//将短信息内容添加到数据库中

OleDbConnectioncon=newOleDbConnection(\"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\"+\"message.mdb\"

+\";PersistSecurityInfo=False\");

con.Open();

OleDbCommandcmd=newOleDbCommand(strSql,con);cmd.ExecuteNonQuery();con.Close();

}

自定义getMessage()方法,获取数据库中所有的短信息数据。代码如下:

privatevoidgetMessage(){

OleDbConnectioncon=newOleDbConnection(\"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\"+\"message.mdb\"

+\";PersistSecurityInfo=False\");

OleDbDataAdapterdap=newOleDbDataAdapter(\"selectmobileas手机号码,contentas短信息,reciveTimeas日

期fromRecivedBox\con);

DataSetds=newDataSet();dap.Fill(ds);

dataGridView1.DataSource=ds.Tables[0].DefaultView;

}

调用自定义getSplitMessage()方法,将整理后的销售数据显示在DataGridView表格中。代码如下:

privatevoidbtnFind_Click(objectsender,EventArgse){

if(dataGridView1.Rows.Count>0)this.getSplitMessage();

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top