在注册页面内,单击按钮查询某用户名是否被注册
最好是详细点的c#代码
------解决方案--------------------------------------------------------
现在已经流行用AJAX来解决这个问题了..
------解决方案--------------------------------------------------------
string strSql = "select username from User where UserName = ' " + UserName.Text + " ' "
不过这样有很多问题:
1.SQL注入攻击
2.层次结构混乱
3.用户体验不好
------解决方案--------------------------------------------------------
楼上正解,
顶
------解决方案--------------------------------------------------------
用存储过程实现:
CREATE PROCEDURE GetNameById
(
@userid varchar(50)
)
AS
select id from userinf where username=@username
GO
public bool IsExist(string UserName)
{
SqlConnection conn = new SqlConnection(ConnStr);
SqlCommand cmd = new SqlCommand( "GetName ", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( "@username ", SqlDbType.varchar,50);
cmd.Parameters[ "@userid "].Value = UserName;
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
retrun true;
}
return false;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
------解决方案--------------------------------------------------------
如amandag(高歌)所说,用ajax比较流行,挺好用的,