WebService服务端
private SqlDataReader GetUserReader(string sUserID)
{
string sSQL = "SELECT GUID,Name, Password,Birthday,Email, Mobile,Address,UserType FROM [User] WHERE GUID=@GUID";
SqlConnection cn = GetOpenedConnection();
cmdGetUser = new SqlCommand(sSQL, cn);
cmdGetUser.Parameters.AddWithValue("@GUID", sUserID);
SqlDataReader reader = cmdGetUser.ExecuteReader();
return reader;
}
这个方法外部看不到,相当于过程函数,返回一个SqlDataReader,供下面这个方法调用。
[WebMethod(Description = "获取单个用户")]
public string[] GetUser(string sUserID)
{
SqlDataReader reader = GetUserReader(sUserID);
string[] sResult = new string[8];
if (reader.Read())
{
sResult[0] = reader["GUID"].ToString();
sResult[1] = reader["Name"].ToString();
sResult[2] = reader["Password"].ToString();
sResult[3] = reader["Birthday"].ToString();
sResult[4] = reader["Email"].ToString();
sResult[5] = reader["Mobile"].ToString();
sResult[6] = reader["Address"].ToString();
sResult[7] = reader["UserType"].ToString();
}
reader.Close();
return sResult;
}
Web客户端
现在在客户端页面调用返回用户信息方法。如图:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/youngerch/bai1.jpg)
主要代码:
private void BindData()
{
if (UserID == "") return;
txtName.Text = sUserData[ 1];
txtEmail.Text = sUserData[ 4];
txtMobile.Text = sUserData[ 5];
txtAddress.Text = sUserData[ 6];
}
定义一个sUserData数组,实例化一个webservice对象myService,调用GetUser()方法。
然后将值赋值给页面上对应的控件。