请帮我看看这代码哪有错
弄了两天没弄懂,不知道哪有问题,点击按钮能刷新,但就是没有添加数据到数据库,也没弹出script提示
UI层
protected void Button2_Click(object sender, EventArgs e)
{
string p_adminame = tbusername.Text.Trim();
string p_adminpwd = tbuserpwd.Text.Trim();
bool b = new P_AdminManager().InsertAdmin(p_adminame,p_adminpwd);
if (b)
{
//script:添加成功,此处代码就不写了
}
else
{
//script:添加失败
}
}
BLL层
namespace BLL
{
public class P_AdminManager
{
private P_AdminDAO P_dao = null;
public P_AdminManager()
{
P_dao = new P_AdminDAO();
}
public bool InsertAdmin(string p_adminame,string p_adminpwd)
{
return P_dao.InsertAdmin(p_adminame,p_adminpwd);
}
}
}
DAL层
namespace DLL
{
public class P_AdminDAO
{
private SQLHelper sqlhelper;
public P_AdminDAO()
{
sqlhelper = new SQLHelper();
}
public bool InsertAdmin(string p_adminame,string p_adminpwd)
{
bool flag = false;
string cmdText = "insert into P_Admin(P_Adminame,P_Adminpwd) values('" + p_adminame + "','" + p_adminpwd + "')";
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@P_Adminame",p_adminame),
new SqlParameter("@P_Adminpwd",p_adminpwd)};
int res = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.Text);
if (res > 0)
{
flag = true;
}
return flag;
}
}
}
Model层
namespace Model
{
public class P_Admin
{
private string P_id;
public string p_id
{
get { return P_id; }
set { P_id = value; }
}
private string P_adminame;
/// 管理员帐户
public string p_adminame
{
get { return P_adminame; }
set { P_adminame = value; }
}
private string P_adminpwd;
/// 管理员密码
public string p_adminpwd
{
get { return P_adminpwd; }
set { P_adminpwd = value; }
}
private string P_admintype;
/// 管理员类型
public string p_admintype
{
get { return P_admintype; }
set { P_admintype = value; }
}
public P_Admin()
{ }
public P_Admin(string P_adminame, string P_adminpwd)
{
this.p_adminame = P_adminame;
this.p_adminpwd = P_adminpwd;
}
}
}
Sqlhelper类
namespace DLL
{
public class SQLHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr = null;
public SQLHelper()
{
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
conn = new SqlConnection(connStr);
}
public SqlConnection GetConn()
{
if (conn.State == ConnectionState.Closed)
{ conn.Open(); }
return conn;
}
#region 执行不带参增删改语句
public int ExecuteNonQuery(string cmdText, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
res = cmd.ExecuteNonQuery();
}
return res;
}
#endregion
#region 执行带参增删改语句
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
int res;
using (cmd = new SqlCommand(cmdText, GetConn()))
{
cmd.CommandType = ct;
cmd.Parameters.AddRange(paras);
res = cmd.ExecuteNonQuery();
}
return res;
}
#endregion
}
}
[解决办法]
1.你可以设置断点看看,有没有进入方法
2.你的添加代码参数设置没用,不过不会影响
string cmdText = "insert into P_Admin(P_Adminame,P_Adminpwd) values('" + p_adminame + "','" + p_adminpwd + "')";
=》
string cmdText = "insert into P_Admin(P_Adminame,P_Adminpwd) values(@P_Adminame,@P_Adminpwd)";
[解决办法]
好像是没有执行方法额
检查下页面的代码里面 是否写了
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
}
[解决办法]
public bool InsertAdmin(string p_adminame,string p_adminpwd) { bool flag = false; string cmdText = "insert into P_Admin(P_Adminame,P_Adminpwd) values('" + p_adminame + "','" + p_adminpwd + "')"; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@P_Adminame",p_adminame), new SqlParameter("@P_Adminpwd",p_adminpwd)}; int res = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.Text); if (res > 0) { flag = true; } return flag; }
[解决办法]
DAL层namespace DLL{ public class P_AdminDAO { private SQLHelper sqlhelper; public P_AdminDAO() { sqlhelper = new SQLHelper(); } public bool InsertAdmin(string p_adminame,string p_adminpwd) { bool flag = false; string cmdText = "insert into P_Admin(P_Adminame,P_Adminpwd) values('" + p_adminame + "','" + p_adminpwd + "')"; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@P_Adminame",p_adminame), new SqlParameter("@P_Adminpwd",p_adminpwd)}; int res = sqlhelper.ExecuteNonQuery(cmdText, paras, CommandType.Text); if (res > 0) { flag = true; } return flag; }}}
[解决办法]