1)用VS2008创建空白解决方案“WebServiceProject”。
2)在空白解决方案“WebServiceProject”中添加ASP.NET Web服务器应用程序“WebServices”。
3)在ASP.NET Web服务器应用程序“WebServices”中添加Web服务“StudentWebService.asmx”。
4)在ASP.NET Web服务器应用程序“WebServices”中添加LINQ to SQL类“StudentDataClasses.dbml”。
5)显示“服务器资源管理器”,右击“数据库连接”连接“school”数据库。
6)将“school” 数据库中的“student”表添加到“StudentDataClasses”中保存。
7)Web.config连接数据库的代码如下:
<connectionStrings>
<add name="schoolConnectionString" connectionString="Data Source=DG-CAOZHENHUA/S2005;Initial Catalog=school;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
8)StudentWebService.asmx代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
namespace WebServices
{
/// <summary>
/// StudentWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class StudentWebService : System.Web.Services.WebService
{
private string connectionString = ConfigurationManager.ConnectionStrings["schoolConnectionString"].ToString();
private StudentDataClassesDataContext dc = null;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/// <summary>
/// 添加学生信息
/// </summary>
/// <param name="name"></param>
/// <param name="password"></param>
/// <returns></returns>
[WebMethod]
public bool AddStudent(string name, string password)
{
bool flag = false;
student entity = new student();
entity.name = name;
entity.password = password;
try
{
dc = new StudentDataClassesDataContext(connectionString);
dc.student.InsertOnSubmit(entity);
dc.SubmitChanges();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
return flag;
}
}
}
9)发布StudentWebService到iis中http://192.168.0.45:99/StudentWebService.asmx
10)在空白解决方案“WebServiceProject”中添加Windows 窗体应用程序“AddStudent”。
11)“AddStudent”添加服务应用,服务的地址:http://192.168.0.45:99/StudentWebService.asmx,服务的命名空间:StudentServiceReference。
12)右击“StudentServiceReference”,选择配置服务引用,将生成异步操作打钩。
13)“AddStudent”代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AddStudent
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
StudentServiceReference.StudentWebServiceSoapClient swssc = new AddStudent.StudentServiceReference.StudentWebServiceSoapClient();
private void btnAdd_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string password = txtPassword.Text;
swssc.AddStudentCompleted += new EventHandler<AddStudent.StudentServiceReference.AddStudentCompletedEventArgs>(swssc_AddStudentCompleted);
swssc.AddStudentAsync(name, password);
}
void swssc_AddStudentCompleted(object sender, AddStudent.StudentServiceReference.AddStudentCompletedEventArgs e)
{
if (e.Result == true)
{
MessageBox.Show("add success!");
}
else
{
MessageBox.Show("add failure!");
}
}
}
}