答案:/* 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')";
上一个:Step-By-Step 建立PocketPc应用程序(1)
下一个:为什么创建VCL for .NET? -- Delphi编译器架构师撰文