/* written by jaron(贾俊) ,2003-11-04 */
/* 原出处:b/s web技术中文网 http://www.jaron.cn ;*/
/* 欢迎访问我的网站: http://www.jaron.cn http://blog.jaron.cn*/
/* 如对本文有疑问,请在网站留言 /*
/* 转载请注明出处和保留此版权信息 */
/* 欢迎使用sitemanager网站管理系统 http://sitemanager.cnzone.net http://demo.jaron.cn ; */
// class1.cs
using system;
using system.text;
using system.net;
using system.net.sockets;
namespace yzstudysrv
{
?///
?/// class1 的摘要说明。
?///
?class class1
?{
??///
??/// 应用程序的主入口点。
??///
??///
??private const int portnum=11000;
??????? private const string socketserver_address="192.168.1.223";
??[stathread]
??static void main(string[] args)
??{
???//
???//
???// todo: 在此处添加代码以启动应用程序
???bool done=false;
???tcplistener listener=new tcplistener(ipaddress.parse(socketserver_address),portnum);
???
???//tcplistener listener=new tcplistener(portnum);
???string sendbuff="999000";
???sqlaccess cszj;
???byte[] sbuff;
???while(!done)
???{
????listener.start();
????console.writeline("waiting");
????//tcpclient client=listener.accepttcpclient();
????tcpclient client=listener.accepttcpclient();
????console.writeline("connected");
????networkstream ns=client.getstream();
????byte[] sbytes=new byte[client.receivebuffersize];
????ns.read(sbytes,0,(int)client.receivebuffersize);
????string rec=encoding.unicode.getstring(sbytes);
???????????????? console.writeline(rec);
????if(convert.toint32(rec.substring(0,3))!=rec.length)
?????sbuff=encoding.unicode.getbytes("888111");//包长不对
????
????else
????{
???
????
?????cszj=new sqlaccess();
?????if(rec.substring(3,3)=="001")//查询
?????{
??????cszj.getquer(rec,sendbuff);
??????sbuff=encoding.unicode.getbytes(sendbuff);
?????}
???
?????else
??????if(rec.substring(3,3)=="002")//交费
?????{
??????cszj.getjfpack(rec,sendbuff);
??????sbuff=encoding.unicode.getbytes(sendbuff);
?????}//hc
?????else
??????if(rec.substring(3,3)=="003")//冲帐
?????{
??????cszj.getczpack(rec,sendbuff);
?????
??????sbuff=encoding.unicode.getbytes(sendbuff);
?????}
?????else
??????sbuff=encoding.unicode.getbytes("999000");//出错
????}
????try
????{
?????ns.write(sbuff,0,sbuff.length);
?????ns.close();
?????client.close();
????}
????catch (exception er)
????{
?????console.writeline(er.tostring());
????}
???????????
????listener.stop();
????//
???}
??}
?}
}
//sqlaccess.cs
using system;
using system.componentmodel;
using system.collections;
using system.diagnostics;
using system.data.sqlclient;
//using system.data.
//using system.data.sqlclient.sqltransaction;
namespace yzstudysrv
{
?///
?/// sqlaccess 的摘要说明。
?///
?public class sqlaccess : system.componentmodel.component
?{
??private system.data.sqlclient.sqlconnection sqlconnection1;
??private system.data.sqlclient.sqlcommand sqlcommand1;
??private system.data.sqlclient.sqldatareader myreader;
??
??///
??/// 必需的设计器变量。
??///
??private system.componentmodel.container components = null;
??
??public sqlaccess(system.componentmodel.icontainer container)
??{
???///
???/// windows.forms 类撰写设计器支持所必需的
???///
???container.add(this);
???initializecomponent();
???//
???// todo: 在 initializecomponent 调用后添加任何构造函数代码
???//
??}
??public sqlaccess()
??{
???///
???/// windows.forms 类撰写设计器支持所必需的
???///
???initializecomponent();
???//
???// todo: 在 initializecomponent 调用后添加任何构造函数代码
???//
??}
??///
??/// 清理所有正在使用的资源。
??///
??protected override void dispose( bool disposing )
??{
???if( disposing )
???{
????if(components != null)
????{
?????components.dispose();
????}
???}
???base.dispose( disposing );
??}
??#region 组件设计器生成的代码
??///
??/// 设计器支持所需的方法 – 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void initializecomponent()
??{
???this.sqlconnection1 = new system.data.sqlclient.sqlconnection();
???this.sqlcommand1 = new system.data.sqlclient.sqlcommand();
???//
???// sqlconnection1
???//
???this.sqlconnection1.connectionstring = "workstation id=\"bocyz-lyq\";packet size=4096;user id=sa;data source=db;persist sec" +
????"urity info=false;initial catalog=ycedu";
???this.sqlconnection1.infomessage += new system.data.sqlclient.sqlinfomessageeventhandler(this.sqlconnection1_infomessage);
???//
???// sqlcommand1
???//
???this.sqlcommand1.connection = this.sqlconnection1;
??}
??#endregion
??//取查询信息
??public void getquer(string recbuff, string sndbuff)
??{
???string billid=recbuff.substring(61,16);
???string ordertotal,flag,name;
???this.sqlconnection1.open();//打开连接
??????
???sqltransaction mytrans;
???// start a local transaction
???mytrans =sqlconnection1.begintransaction(isolationlevel.readcommitted,"sqltransaction");
???// must assign both transaction object and connection
???// to command object for a pending local transaction
???sqlcommand1.connection = sqlconnection1;
???sqlcommand1.transaction = mytrans;
???try
???{
????this.sqlcommand1.commandtext="select name, ordertotal,deductresaultsymbol from tblorderexpenses where billid="+billid+"";
????myreader=this.sqlcommand1.executereader();
????mytrans.commit();
???}?
???catch(exception e)
???{
????try
????{
?????mytrans.rollback("sqltransaction");
????}
????catch (sqlexception ex)
????{
?????if (mytrans.connection != null)
?????{
??????console.writeline("an exception of type " + ex.gettype() +
???????" was encountered while attempting to roll back the transaction.");
?????}
????}
???
????console.writeline("an exception of type " + e.gettype() +
?????" was encountered while inserting the data.");
????console.writeline("neither record was written to database.");
???}
???finally
???{
????sqlconnection1.close();
???}
??}
//??if(myreader.read())
//???{
//????name=myreader["name"].tostring();
//????ordertotal=myreader["ordertotal"].tostring();
//????flag=myreader["deductresaultsymbol"].tostring();
//????sndbuff=recbuff.substring(0,61)+strpack(name,10)+strpack(ordertotal,7)+flag;
//???}
//???else
//?????? sndbuff="999000";
//????myreader.close();
//????this.sqlconnection1.close();
//??
??//democode
??public void runsqltransaction(string myconnstring)
??{
???sqlconnection myconnection = new sqlconnection(myconnstring);
???myconnection.open();//打开连接
???sqlcommand mycommand = myconnection.createcommand();
???sqltransaction mytrans;
???// start a local transaction
???mytrans = myconnection.begintransaction(isolationlevel.readcommitted,"sampletransaction");
???// must assign both transaction object and connection
???// to command object for a pending local transaction
???mycommand.connection = myconnection;
???mycommand.transaction = mytrans;
???try
???{
????mycommand.commandtext = "insert into region (regionid, regiondescription) values (100, description)";
????mycommand.executenonquery();
????mycommand.commandtext = "insert into region (regionid, regiondescription) values (101, description)";
????mycommand.executenonquery();
????mytrans.commit();
????console.writeline("both records are written to database.");
???}
???catch(exception e)
???{
????try
????{
?????mytrans.rollback("sampletransaction");
????}
????catch (sqlexception ex)
????{
?????if (mytrans.connection != null)
?????{
??????console.writeline("an exception of type " + ex.gettype() +
???????" was encountered while attempting to roll back the transaction.");
?????}
????}
???
????console.writeline("an exception of type " + e.gettype() +
?????" was encountered while inserting the data.");
????console.writeline("neither record was written to database.");
???}
???finally
???{
????myconnection.close();
???}
??}
??????? //冲帐
??public void getczpack(string recbuff, string sndbuff)
??{
???string billid=recbuff.substring(61,16);
???string bankwater=recbuff.substring(77,16);
???
???this.sqlconnection1.open();
???this.sqlcommand1.commandtext ="update tblorderexpenses set deductresaultsymbol=2 where billid="+billid+"and deductresaultsymbol=2"
????+" and bankwater="+bankwater+"";
???int k=0;
???k=this.sqlcommand1.executenonquery();
???if(k==1)
??
????sndbuff=recbuff.substring(0,61)+"0";
???
???else
????sndbuff="999000";
???
???this.sqlconnection1.close();
??
??}
??//交费
??public void getjfpack(string recbuff, string sndbuff)
??{
???string billid=recbuff.substring(61,16);
???string accno=recbuff.substring(77,19);
???string amt=recbuff.substring(96,7);
???string cbflag=recbuff.substring(103,1);
???string bankid=recbuff.substring(10,3);
???string sbranchno=recbuff.substring(13,10);
???string dtdeduct=datetime.now.tostring("yyyymmdd");
???string areaid="0514";
???string bankwater=recbuff.substring(45,16);
???int k=0;
???
???this.sqlconnection1.open();
???this.sqlcommand1.commandtext ="update tblorderexpenses set bankid="+bankid+", servicestationid="
????+sbranchno+",dtdeduct="+dtdeduct+",areaid="+areaid+",deductresaultsymbol=1,bargainersymbol="
????+cbflag+",bankwater="+bankwater+" where billid="+billid+" and ordertotal="+amt+" and deductresaultsymbol = 0";
???k=this.sqlcommand1.executenonquery();
???if(k==1)
????? sndbuff=recbuff.substring(0,61)+"0";
???else
????sndbuff="999000";
???
???this.sqlconnection1.close();
??
??}
??//字符串补位函数
?
??public string strpack(string tmp,int length)
??{
??????????????????????????????????????????
???string atmp;
???int k=0;
???int t=0;
???for(int i=0;i???{??
????if(tmp[i]>0x255)
????{
?????k++;
?????t++;
????}
????t++;
????if(t>length)
?????break;
???}
???int alen=tmp.length+k;
???if(alen??????????????????
???{
????atmp=new string( ,length-alen);
??
????return tmp+atmp;
??
???????
???}
???else
???
????return tmp.substring(0,length-k);
??????????
?????
??
??}
??private void sqlconnection1_infomessage(object sender, system.data.sqlclient.sqlinfomessageeventargs e)
??{
??
??}
??
?}
}
(本文是直接从vs.net贴出的,自行删除前面的??)