webservice1.asmx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace work058 { /// <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 { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(Description="检查用户表中是否已存在指定的用户名")] public bool ExistsUserName(string userName) { bool exists = false; System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder(); bu.DataSource = "(local)"; bu.InitialCatalog = "test"; bu.UserID = "sa"; bu.Password = "123456"; string conString = bu.ConnectionString; System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString); con.Open(); //主动打开,要主动关闭, string sql = "select user_name from userinfo where user_name=@uName"; System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con); cmd.Parameters.AddWithValue("@uName",userName); System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); //行为,附带关闭,就不用主动关闭数据库连接 if (reader.HasRows) { exists = true; } else { exists = false; } reader.Close(); //这里关闭reader时,一并关闭了con return exists; } [WebMethod(Description="获取服务器的当前时间")] public DateTime GetServerTime() { return DateTime.Now; } } }
web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.webServer> <directoryBrowse enabled="true" /> </system.webServer> </configuration>
sql
create database test; create table UserInfo( user_id int identity(1,1) not null, user_name varchar(20) not null unique, real_name nvarchar(8) not null, age tinyint not null, sex bit not null, mobile varchar(14), phone varchar(14), email varchar(50) not null, primary key(user_id) ); insert into UserInfo values('zhangfei','张飞',36,1,'13011110001','8845996','zf@qq.com'); insert into UserInfo values('guanyu','关羽',38,1,'13011110002','8845995','gy@qq.com'); insert into UserInfo values('liubei','刘备',42,1,'13011110003','8845994','lb@qq.com'); insert into UserInfo values('zhaoyun','赵云',32,1,'13011110004','8845993','zy@qq.com'); insert into UserInfo values('huangzhong','黄忠',50,1,'13011110005','8845992','hz@qq.com'); insert into UserInfo values('caocao','曹操',48,1,'13011110006','8845991','cc@qq.com'); insert into UserInfo values('sunquan','孙权',33,1,'13011110007','8845990','sq@qq.com'); insert into UserInfo values('diaochan','貂蝉',20,0,'13011110008','8845880','dc@qq.com'); insert into UserInfo values('daqiao','大乔',21,0,'13011110009','8845881','dq@qq.com'); insert into UserInfo values('xiaoqiao','小乔',20,0,'13011110010','8845882','xq@qq.com');