先简单创建一个DBHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
//数据库连接类
public class DBHelper
{
//创建数据库连接方法
public static SqlConnection GetConn()
{
SqlConnection conn = new SqlConnection("server=.;database=userdb;uid=sa;pwd=123456");
return conn;
}
}
//数据库操作类
public class SQLHelper
{
private SqlConnection conn;
private SqlCommand cmd;
private SqlDataAdapter sda;
private DataSet ds;
//创建数据库查询通用方法
public DataSet RunQuery(string sql)
{
try
{
//获取数据库连接对象
conn = DBHelper.GetConn();
//创建sql命令
cmd = new SqlCommand(sql, conn);
//创建数据适配器执行sql命令
sda = new SqlDataAdapter(cmd);
//实例化数据集
ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch (Exception)
{
throw;
}
}
//创建数据库增删改的通用方法
public int AddDelUp(string sql)
{
try
{
//定义变量接受所受影响行数
int i = 0;
//获取数据库连接对象
conn = DBHelper.GetConn();
//打开数据库连接
conn.Open();
//创建sql命令
cmd = new SqlCommand(sql, conn);
i = cmd.ExecuteNonQuery();
return i;
}
catch (Exception)
{
throw;
}
finally
{
//关闭数据库连接
conn.Close();
}
}
}
}
示例一:验证WebService的使用方法。
首先新建项目解决方案WebApplication1,在项目中添加webForm.aspx页面,页面代码如下:
webForm.aspx页面脚本代码如下:
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="WebService验证用户登录" /><br/><br/>
<asp:Button ID="Button2" runat="server" Text="调用WebService服务" onclick="Button2_Click" /><br/><br/>
<asp:Button ID="Button3" runat="server" Text="调用WCF服务" onclick="Button3_Click" /><br/><br/>
</form>
选择项目,新建Web服务,取名为WebService1.asmx,编写代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </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 WebService1 : System.Web.Services.WebService
{
SQLHelper sqlhelper = new SQLHelper();
[WebMethod]
public string HelloWorld()
{
return "WebService HelloWorld";
}
[WebMethod(Description="webservice登陆")]
public bool yn(string name,string pwd)
{
DataSet ds = sqlhelper.RunQuery(string.Format("select * from useinfo where names='{0}'", name));
if (ds.Tables[0].Rows[0]["pwd"].ToString() == pwd)
{
return true;
}
else
{
return false;
}
}
}
}
选择项目,新建WCF服务,取名为WcfServiceDemo.svc,编写代码如下:
这时候会发现自动生成了接口文件IWcfServiceDemo.cs ,打开里面代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WebApplication1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IWcfServiceDemo”。
[ServiceContract]
public interface IWcfServiceDemo
{
[OperationContract]
string DoWork();
}
}
双击查看WcfServiceDemo.svc文件,发现继承了接口 IWcfServiceDemo,并实现了接口中的方法,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WebApplication1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WcfServiceDemo”。
public class WcfServiceDemo : IWcfServiceDemo
{
public string DoWork()
{
return "WCF HelloWord";
}
}
}
那么到现在为止WebService和WCF都已将创建好了,接下来就是调用了。
在webForm.aspx.cs 后台页面调用脚本代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
WebService1 S = new WebService1();
if (S.yn(this.TextBox1.Text, this.TextBox2.Text))
{
Response.Write("<script>alert('登陆成功')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
WebService2 webs = new WebService2();
string msg = webs.HelloWorld();
Button2.Text = msg;
}
protected void Button3_Click(object sender, EventArgs e)
{
IWcfServiceDemo iwc = new WcfServiceDemo();
string sss = iwc.DoWork();
Button3.Text = sss;
}
} }