以下是Linq:
/// <summary>
/// 获取用户
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Users GetUserByName(string name)
{
using (CompaneyDataDataContext mb = new CompaneyDataDataContext())
{
var q = from c in mb.Users
where c.User_Name == name
select c;
if (q.Count() > 0)
return q.First();
else
return null;
}
}
以下是调用判断:
if (bu.GetUserByName(txtName.Text) == null)
{
if (bu.addUser (oneuser))
{
fuImage.SaveAs(Server.MapPath("./") + "/images/" + imageurl);
Response.Write("<script>alert('注册成功');window.location.href='Default.aspx'</script>");
}
else
{
Response.Write("<script>alert('注册失败')</script>");
}
}
else
{
Response.Write("<script>alert('该用户已经存在,请更换用户名,再次添加')</script>");
}
}
private void checkName()
{
SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd=111");
SqlCommand cmd = new SqlCommand("select count(name) from user where name='abc'",conn);
try
{
conn.Open();
if (cmd.ExecuteScalar())
{
//发现有用户使用该昵称
}
else
{
//没有用户使用,插入数据
}
}
catch
{
}
finally
{
conn.Close();
conn.Dispose();
conn = null;
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>文本框失去焦点就验证数据是否合法</title>
<script type="text/javascript">
function Validate()
{
var user=document.getElementById("UserName").value;
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
xmlRequest.onreadystatechange = function() {
if (4 == xmlRequest.readystate) {
if (200 == xmlRequest.status) {
var ps_Msg = xmlRequest.responseText;
alert(ps_Msg);
}
}
}
xmlRequest.open("get", "../Ajax/ValidateServer.aspx?UserName=" + user + "&v=" + Math.random(), true);
xmlRequest.send(null);
//自己拷贝的时候注意 我这里是项目下建了个Ajax文件夹,下面有两个页面Validate.aspx和ValidateServer.aspx
//如果没有文件夹的话 要去掉,直接是"../ValidateServer.aspx?UserName=" + user + "&v=" + Math.random()
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
用户:
<input id="UserName" type="text" onblur="Validate()"/>
</div>
<div>
密码:
<input id="PassWord" type="text" />
</div>
</form>
</body>
</html>
//ValidateServer.aspx只有后台代码 处理页面 可以用Web服务
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Ajax_ValidateServer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string UserName = string.Empty;
if (Request.QueryString["UserName"] != null)
{
UserName = Request.QueryString["UserName"].ToString();
}
try
{
string strSql = "Select Count(*) from Users where UserName = '" + UserName + "'";
//执行Sql语句 如果>0 则此用户已存在;否则用户可注册
using (SqlConnection cn = new SqlConnection("server=localhost;Initial Catalog=Test;uid=sa;pwd=sa"))
{
try
{
//执行Sql语句 这里只是测试一下
if (UserName == "zhang")
{
this.Response.Write("此用户已存在");
}
else
{
this.Response.Write("此账户可用");
}
}
catch
{
this.Response.Write("SQL执行出错");
}
}
}
catch
{
this.Response.Write("错误");
}
finally
{
this.Response.End();
}
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。