【www.gdgbn.com--ASP.NET MVC】

jquery+asp教程.net 检测账号是否已被注册的实例代码

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="ajax_xml._default" %>




<script type="text/网页特效" src="jquery-1.3.2-vsdoc2.网页特效" language="网页特效"></script>
<script type="text/javascript" language="javascript">
$(function() {
$("#<%=userid.clientid%>").keyup(
function() {
$.ajax({
type: "post",
contenttype: "application/网页特效on",
datatype: "json",
url: "webservice1.asmx/userisexist",
data: "{userid:"" + $("#<%=userid.clientid%>").val() + ""}",
success: function(result) {
if (result.d == "true")
$("#<%=isexist.clientid%>").text("yes");  //账号已存在
else
$("#<%=isexist.clientid%>").text("no");  
}
});
}
);
})
</script>




































  websevic1.asmx.cs

 

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.services;
using system.data;
using system.data.sqlclient;
using system.configuration;
using system.text;
using dal;
namespace ajax_xml
{
///


/// webservice1 的摘要说明
///

[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]
public string userisexist(string userid)
{
string sql = string.format("select * from customers where firstname="" + userid+""");
using (sqldatareader dr = sqlhelper.executesql(sql))
{
if (dr.read())
return "false";
else
return "true";
}
}
}
}

sqlheper.cs


using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.data.sqlclient;
using system.configuration;
namespace dal
{
///


/// 数据库教程操作类
///

public class sqlhelper
{
private static sqlconnection conn;
private static sqlcommand comm;
private static sqldatareader dr;
///
/// 打开数据库连接
///

public static void connopen()
{
try
{
conn = new sqlconnection(configurationmanager.connectionstrings["bbs"].connectionstring);
conn.open();
}
catch (exception e)
{
console.writeline( e.message);
}
}
///
/// 关闭数据库连接,释放资源
///

public static void connclose()
{
if (conn != null)
{
conn.close();
}
if (comm != null)
{
comm.dispose();
}
}
public static sqldatareader executesql(string sql)
{
sqlhelper.connopen();
comm = new sqlcommand(sql, conn);
try
{
dr = comm.executereader();
return dr;
}
catch (exception e)
{
throw e;
}
}
}
}

本文来源:http://www.gdgbn.com/asp/25691/