新手问问题~关于SQL数据表的问题? 写个Exist(username) return bool判断是否存在该用户如果存在 提示一下。这里用AJAX比较好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 插入数据之前按名称select count(*) 一下,大于 0 就是占用了.提示一下,不做保存小心大小写和空格 public bool IsExist() { string name =this.ddl_mname.SelectedItem.Value.ToString(); string strSQL = "select * from user_info where Name='"+name+"'"; try { ExecuteSqlValue(strSQL); return true; } catch { return false; } }protected static int ExecuteSqlValue(string strSQL) { string strConn= ConfigurationSettings.AppSettings["datasource"]; SqlConnection myCn = new SqlConnection(strConn); SqlCommand myCmd = new SqlCommand(strSQL,myCn); try { myCn.Open(); object r = myCmd.ExecuteScalar(); if(Object.Equals(r,null)) { throw new Exception("value unavailable!"); } else { return (int)r; } } catch(System.Data.SqlClient.SqlException e) { throw new Exception(e.Message); } finally { myCmd.Dispose(); myCn.Close(); } } 太感谢各位了~特别是6楼的laoyingisme 大哥真的太好人了代码都写出来了~太谢谢了 -- 判断用户是否存在create procedure p_existsUser @userName nvarchar(50)asdeclare @value intif (select count(1) from tableName where userName = @userName) > 0beginset @value = 1endelsebeginset @value = 0endreturn @valuego-------------// 判断用户是否存在provite bool bExistsUser(string strUserName){SqlParameter[]par = new SqlParameter[1];reutrn Convert.toBoolen(DbHelperSQL.RunProcedure("p_existsUser", para);}------------- /// <summary> /// 执行存储过程,返回影响的行数 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="rowsAffected">影响的行数</param> /// <returns></returns> public static int RunProcedure(string storedProcName, IDataParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { int result; connection.Open(); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); result = (int)command.Parameters["ReturnValue"].Value; //Connection.Close(); return result; } }----------- 顶,哎来晚了。楼上的说得很详细了,就是用新的name为条件写个sql去数据库验证一下重复。 关于一个执行存储过程的错误 求教高手:asp.net在后台关闭本窗口。 怎么实现任意二级域名 aspx 网页导航菜单样式? [图] 关于asp.net配置问题 asp.net中制作省市县三级联动的下拉菜单问题 请教各位:在xp里怎么运行asp程序? 类似QQ空间更换皮肤的功能怎么实现asp.net,请问大家谁有呢急急 大神们 看看 我哪里错了 求电子书《Building Web Solutions with ASP.NET and ADO.NET》 如何将传值ID与汉字名称改为乱码? 在.net2005中如何实现对数据库的备份
public bool IsExist()
{
string name =this.ddl_mname.SelectedItem.Value.ToString();
string strSQL = "select * from user_info where Name='"+name+"'"; try
{
ExecuteSqlValue(strSQL);
return true;
}
catch
{
return false;
}
}
protected static int ExecuteSqlValue(string strSQL)
{
string strConn= ConfigurationSettings.AppSettings["datasource"]; SqlConnection myCn = new SqlConnection(strConn);
SqlCommand myCmd = new SqlCommand(strSQL,myCn);
try
{
myCn.Open();
object r = myCmd.ExecuteScalar();
if(Object.Equals(r,null))
{
throw new Exception("value unavailable!");
}
else
{
return (int)r;
}
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
create procedure p_existsUser @userName nvarchar(50)
as
declare @value int
if (select count(1) from tableName where userName = @userName) > 0
begin
set @value = 1
end
else
begin
set @value = 0
endreturn @valuego-------------
// 判断用户是否存在
provite bool bExistsUser(string strUserName)
{SqlParameter[]par = new SqlParameter[1];reutrn Convert.toBoolen(DbHelperSQL.RunProcedure("p_existsUser", para);
}------------- /// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <param name="rowsAffected">影响的行数</param>
/// <returns></returns>
public static int RunProcedure(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}
-----------