MVC 添加用户.

在codeproject看到一个mvc实例.
大致结构如下.
AbstractLayer-baseMember.cs   定义了基类成员
DataLayer-SqlDataAccess.cs  继承IdataAccess,实现接口方法
                 -IdataAccess.cs     接口
BusinessLayer-Member.cs  继承baseMember.cs  返回方法实现
View
添加用户页面.
将页面数据添加到BusinessLayer 下的Member实例(因为BusinessLayer下的Member继承了baseMember)调用AddNewMember();

 

None.gif protected   void  btnSave_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gifAddMember();
InBlock.gif            
int retVal = objMember.AddnewMember(objMember);
InBlock.gif            
if(retVal>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lblMessage.Text 
= "New record added successfully";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lblMessage.Text 
= "An error has occured while processing your request";
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        
public   void  AddMember()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            objMember.firstName 
= txtFirstName.Text.Trim();
InBlock.gif            objMember.lastName 
= txtLastName.Text.Trim();
InBlock.gif            objMember.contactNo 
= txtContactNo.Text.Trim();
InBlock.gif            objMember.emailAddress 
= txtEmailAddress.Text.Trim();
ExpandedBlockEnd.gif        }

BusinessLayer下Member.cs
AddNewMember再返回接口方法的实现

None.gif public   int  AddnewMember(AbstractLayer.baseMember objMember)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            IdataAccess MemberSqlDataAccess 
= new SqlDataAccess(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString());
InBlock.gif            
return MemberSqlDataAccess.AddnewMember(objMember);
ExpandedBlockEnd.gif        }

DataLayer
IdataAccess定义了AddNewMember接口.
SqlDataAccess实现
SqlDataAccess代码:

public int AddnewMember(AbstractLayer.baseMember  objMember)
  {
   SqlTransaction objTrans=null;      
   SqlConnection myConnection =  new SqlConnection(m_Connection_String);
   try
   {
    // Insert Member Personal details only
    myConnection.Open();
    objTrans= myConnection.BeginTransaction();
    SqlParameter [] arrParam=new SqlParameter[4];

    arrParam[0]=new SqlParameter("@fName", objMember.firstName);
    arrParam[1]=new SqlParameter("@lName",objMember.lastName);
    arrParam[2]=new SqlParameter("@coNo",objMember.contactNo);
    arrParam[3]=new SqlParameter("@emailAddr",objMember.emailAddress);
    //pass connection string, storedprocedure name and parameter array
    SqlHelper.ExecuteNonQuery(m_Connection_String,CommandType.StoredProcedure,"usps_proInsMember",arrParam);
    
   }
   catch(Exception Ex)
   { 
    objTrans.Rollback();
    string sError=Ex.Message.ToString(); 
    return -1;
   }
   finally
   {
    myConnection.Close();
   }
   return 1;
  }


 

转载于:https://www.cnblogs.com/yinhl/archive/2006/07/07/445606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值