62,205
社区成员
发帖
与我相关
我的任务
分享
public class AddUser
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbuser"].ConnectionString);
public AddUser()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool AddUserS(String name, String pass)
{
String T_name = name;
String T_pass = pass;
String sql = "insert into info values('" + T_name + "','" + T_pass + "')";
SqlCommand cmd = new SqlCommand(sql, con);
int Result = -1;
try
{
con.Open();
Result = cmd.ExecuteNonQuery();
if (Result != -1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
con.Close();
}
return false;
}
}
public partial class DisposeEvent : System.Web.UI.Page
{
CheckUser cu = new CheckUser();
AddUser au = new AddUser();
logon go = new logon();
protected void Page_Load(object sender, EventArgs e)
{
switch (Request.QueryString["Event"])
{
case "Reg":
if (au.AddUserS(Request.QueryString["Name"].ToString(), Request.QueryString["Pass"].ToString()))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
break;
public class AddUser
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbuser"].ConnectionString); // 实例化 Connection 对象。……对于Connection,声明为类级字段,这不是明知的选择!
public AddUser()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool AddUserS(String name, String pass)
{
String T_name = name; // 上下文看,没发现拷贝赋值给局部变量有什么意义
String T_pass = pass;
String sql = "insert into info values('" + T_name + "','" + T_pass + "')"; // 构造 sql script
SqlCommand cmd = new SqlCommand(sql, con); // 实例化 command
int Result = -1;
try
{
con.Open(); // 尝试打开链接
Result = cmd.ExecuteNonQuery(); // 尝试执行指定的 sql
// 这里判断 result 也没有实际意义,根据上下文意思有意义的应该是
// return (Result == 1); // 你绝对能确定一次注册用户,只能且仅能写入一条记录,否则就是 failure
// 按此功能,更应该直接返回 result return, 由上层调用如BLL来确定是否 succes,DAL 尽量不要做跟业务有关的事情。
if (Result != -1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
// 应该
// MyDALException dalEx = new MyDALException("Failure adding new user", ex); // 如果不包装内部异常,你将如法跟踪到原始错误的堆栈信息。
// dalEx.Data.Add("Sql", sql); // 甚至可以存储导致异常的 sql
// throw dalEx; // 重新抛出
throw (ex);
}
finally
{
con.Close();
}
return false; // 此句应该永远不会执行,但是没有此句会编译错误!!!
}
}