Web Service获取实体类函数只能正确运行一次

发布于 2021-11-29 10:51:48 字数 4995 浏览 960 评论 1

/*
 * 由SharpDevelop创建。
 * 用户: Kingsun
 * 日期: 14-5-25
 * 时间: 23:06
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Runtime.Serialization;
using System.IO;

namespace QrBikeWS
{
	/// <summary>
	/// Description of bike.
	/// </summary>
	public class Bike
	{
		private string _id;
		private string _name;
		private string _phone;
		private string _dorm;
		private string _color;
		private string _areas;
		private char _state;
		public Bike()
		{
		}
		
		public string id
		{
			get{return _id;}
			set{this._id=value;}
		}		
		public string name
		{
			get{return _name;}
			set{this._name=value;}
		}		
		public string phone
		{
			get{return _phone;}
			set{this._phone=value;}
		}		
		public string dorm
		{
			get{return _dorm;}
			set{this._dorm=value;}
		}		
		public string color
		{
			get{return _color;}
			set{this._color=value;}
		}		
		public string areas
		{
			get{return _areas;}
			set{this._areas=value;}
		}		
		public char state
		{
			get{return _state;}
			set{this._state=value;}
		}
	}
}



/*
 * 由SharpDevelop创建。
 * 用户: Kingsun
 * 日期: 14-5-25
 * 时间: 22:07
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace QrBikeWS
{
	/// <summary>
	/// Description of DBHelper.
	/// </summary>
	public class DBHelper:IDisposable
	{
		public static SqlConnection sqlCon;
		public DBHelper()
		{
			if(sqlCon==null)
			{
				sqlCon=new SqlConnection();
				sqlCon.ConnectionString=@"Data Source=GODSHIPSQLEXPRESS;User ID=qrbike;Password=qrbike";
				sqlCon.Open();
			}
		}
		
		//销毁函数
		public void Dispose()
		{
			if(sqlCon!=null)
			{
				sqlCon.Close();
				sqlCon=null;
			}
		}
		
		//获取Bike记录
		public Bike getBike(string id)
		{
			Bike bike = new Bike();
			try 
			{
				string sql="select id,name,phone,dorm,color,areas,state from bike where id="+id;
				SqlCommand cmd=new SqlCommand(sql,sqlCon);
				SqlParameter para=new SqlParameter(id,SqlDbType.VarChar);
				para.Value="%"+id+"%";
				cmd.Parameters.Add(para);
				SqlDataReader reader=cmd.ExecuteReader();
				
				if(reader.HasRows)
				{
					while(reader.Read())
					{
						bike.id=(string)reader.GetValue(0);
						bike.name=(string)reader.GetValue(1);
						bike.phone=(string)reader.GetValue(2);
						bike.dorm=(string)reader.GetValue(3);
						bike.color=(string)reader.GetValue(4);
						bike.areas=(string)reader.GetValue(5);
						bike.state=(char)reader.GetValue(6);
					}
				}
				reader.Close();
				cmd.Dispose();
			}catch(Exception e){}
			return bike;
		}
	}
}



/*
 * 由SharpDevelop创建。
 * 用户: Kingsun
 * 日期: 14-5-25
 * 时间: 21:45
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections;

namespace QrBikeWS
{
	[WebService]
	public class Soap : System.Web.Services.WebService
	{
		DBHelper dbhelper = new DBHelper();
		/// <summary>
		/// Logs into the web service
		/// </summary>
		/// <param name="userName">The User Name to login in as</param>
		/// <param name="password">User's password</param>
		/// <returns>True on successful login.</returns>
		[WebMethod(EnableSession=true)]
		public bool Login(string userName, string password)
		{
			//NOTE: There are better ways of doing authentication. This is just illustrates Session usage.
			UserName = userName;
			return true;
		}
		
		/// <summary>
		/// Logs out of the Session.
		/// </summary>
		[WebMethod(EnableSession=true)]
		public void Logout()
		{    
			Context.Session.Abandon();
		}
		
		/// <summary>
		/// UserName of the logged in user.
		/// </summary>
		private string UserName {
			get {return (string)Context.Session["User"];}
			set {Context.Session["User"] = value;}
		}
		
		[WebMethod(EnableSession=true)]
		public Bike getBike(string id)
		{
			return dbhelper.getBike(id);
		}
	}
}



第一次调用函数返回如下图:

第二次调用函数返回:

很明显第二次调用时,没有生成bike存储相关数据并返回,返回了一个空的bike,而且只显示类型为char的变量state。这是为什么呢?难道不是每次调用都连接都数据库获取数据,生成新的bike赋值并返回的吗? 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2021-11-29 11:35:07

更改属性state类型为string就可以了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文