本文介绍了2次数据插入(复制)3层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 数据层代码方法 public int AddUsers(Users objUser) { SqlConnection con = new SqlConnection(ConnString); con.Open(); SqlCommand cmd = new SqlCommand( SpCreateUser ,con); cmd.CommandType = CommandType.StoredProcedure; 尝试 { cmd.Parameters.AddWithValue( @ Full_name,objUser.fullname); cmd.Parameters.AddWithValue( @ Dob,objUser.DOB); cmd.Parameters.AddWithValue( @ email,objUser.Email); cmd.Parameters.AddWithValue( @ phoneno,objUser.Phoneno); cmd.Parameters.AddWithValue( @ loginid,objUser.loginId); cmd.Parameters.AddWithValue( @ pwd,objUser.Password); cmd.ExecuteNonQuery(); int totalrecordscreated =( int )cmd.ExecuteNonQuery(); return totalrecordscreated; } catch (例外情况) { throw ex; } 最后 { con.Close(); } } 业务层方法: public int AddUsers(Users objUser) { DataLayer objUserDAL = new DataLayer(); 尝试 { return objUserDAL.AddUsers(objUser); } catch (例外情况) { throw ex; } 最后 { // objUser = null; } } UI layer-register.aspx.cs protected void btnRegsubmit_Click( object sender,EventArgs e) { Users objUser = new Users(); objUser.fullname = tbfullname.Text; objUser.DOB = tbdob.Text; objUser.Email = tbemail.Text; objUser.Phoneno = tbphoneno.Text; objUser.loginId = tbloginid.Text; objUser.Password = tbpwd.Text; BAL objBAL = new BAL(); int records_no = objBAL.AddUsers(objUser); Response.Write( 创建记录数 + records_no.ToString()); } 数据: Trans_id 日期 Person_id Payee_details金额Expense_Type 20 2018-08-27 admin food panda 254 食物 21 2018-08-27 admin VRl 600 旅行 22 2018-08-14 sam rewgrg 564 食物 23 2018-08-14 sam rewgrg 564 食物 具有DAL和BLL层的接口。提交后数据记录插入两次(重复。)这里有什么逻辑错误? 我尝试过: 帮助我,因为我是新手编码,不了解创建此问题的逻辑解决方案 您正在运行查询两次,替换此 // 首先插入 cmd.ExecuteNonQuery(); // 第二次插入 int totalrecordscreated =( int )cmd.ExecuteNonQuery(); return totalrecordscreated; 为此 int numberOfRecords = cmd.ExecuteNonQuery(); DATA layer code methodpublic int AddUsers(Users objUser) { SqlConnection con = new SqlConnection(ConnString); con.Open(); SqlCommand cmd = new SqlCommand("SpCreateUser", con); cmd.CommandType = CommandType.StoredProcedure; try { cmd.Parameters.AddWithValue("@Full_name", objUser.fullname); cmd.Parameters.AddWithValue("@Dob", objUser.DOB); cmd.Parameters.AddWithValue("@email", objUser.Email); cmd.Parameters.AddWithValue("@phoneno", objUser.Phoneno); cmd.Parameters.AddWithValue("@loginid", objUser.loginId); cmd.Parameters.AddWithValue("@pwd", objUser.Password); cmd.ExecuteNonQuery(); int totalrecordscreated = (int)cmd.ExecuteNonQuery(); return totalrecordscreated; } catch (Exception ex) { throw ex; } finally { con.Close(); } }Business layer Method :public int AddUsers(Users objUser) { DataLayer objUserDAL = new DataLayer(); try { return objUserDAL.AddUsers(objUser); } catch (Exception ex) { throw ex; } finally { //objUser = null; } }UI layer- register.aspx.csprotected void btnRegsubmit_Click(object sender, EventArgs e){ Users objUser = new Users(); objUser.fullname = tbfullname.Text; objUser.DOB = tbdob.Text; objUser.Email = tbemail.Text; objUser.Phoneno = tbphoneno.Text; objUser.loginId = tbloginid.Text; objUser.Password = tbpwd.Text; BAL objBAL = new BAL(); int records_no = objBAL.AddUsers(objUser); Response.Write("No of created records " + records_no.ToString());}data:Trans_idDatePerson_idPayee_detailsAmountExpense_Type202018-08-27adminfood panda254Food212018-08-27adminVRl600Travel222018-08-14samrewgrg564Food232018-08-14samrewgrg564Foodhaving interfaces for DAL and BLL layers. after submitting the data records are inserting two times(duplication.) What logic is wrong here?What I have tried:Help me since i am new to coding, not understanding what logic is creating this problem 解决方案 You are running the query twice, replace this//first insertcmd.ExecuteNonQuery(); //second insertint totalrecordscreated = (int)cmd.ExecuteNonQuery();return totalrecordscreated;For thisint numberOfRecords = cmd.ExecuteNonQuery(); 这篇关于2次数据插入(复制)3层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-10 02:05