/* 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贴出的,自行删除前面的??)
本文地址:http://com.8s8s.com/it/it44964.htm