当前位置:编程学习 > C#/ASP.NET >>

企业库DAL写法

/// <summary>
       /// 添加学生信息
       /// </summary>
       /// <param name="stu">学生实体类</param>
       /// <returns>返回影响行数</returns>
       public int AddStuInfo(StuInfo stu)
       {
           if (stu!=null)
           {
               Database db = DatabaseFactory.CreateDatabase("connstring");
               DbCommand cmd = db.GetStoredProcCommand("P_AddStuInfos");
               //db.AddInParameter(cmd, "ID", DbType.Guid, stu.ID);
               //db.AddInParameter(cmd, "name", DbType.String, stu.StuName);
               //db.AddInParameter(cmd, "gender", DbType.String, stu.Gender);
               //db.AddInParameter(cmd, "age", DbType.Int32, stu.Age);
               db.AddInParameter(cmd, "@ID", DbType.Guid);
               db.AddInParameter(cmd, "@name",DbType.String,50);
               db.AddInParameter(cmd, "@gender",DbType.String,50);
               db.AddInParameter(cmd, "@age",DbType.Int32);

               db.SetParameterValue(cmd, "ID",stu.ID);
               db.SetParameterValue(cmd,"name",stu.StuName);
               db.SetParameterValue(cmd, "gender", stu.Gender);
               db.SetParameterValue(cmd,"age",stu.Age);
               int result = db.ExecuteNonQuery(cmd);
               if (result==1)
               {
                   return result;
               }           
           }
           return 0;
       }
/// <summary>
       /// 根据编号得到学生实体类
       /// </summary>
       /// <param name="id">学生编号</param>
       /// <returns>返回实体类</returns>
       public StuInfo GetStuInfo(Guid id)
       {
           Database db = DatabaseFactory.CreateDatabase("connstring");
           string sql = "SELECT * FROM StuInfos WHERE ID='" + id + "'";
           DbCommand cmd = db.GetSqlStringCommand(sql);
           //db.AddInParameter(cmd, "@ID", DbType.Guid, id);
           StuInfo stu = new StuInfo();
           using (IDataReader dr = db.ExecuteReader(cmd))
           {
               while (dr.Read())
               {
                   stu.ID = new Guid(dr[0].ToString());
                   stu.StuName = dr[1].ToString();
                   stu.Gender = dr[2].ToString();
                   stu.Age = Convert.ToInt32(dr[3]);
               }
           }
           return stu;
       } --------------------编程问答-------------------- 没问题????

就是数据访问层写的方法啊! --------------------编程问答-------------------- 神马问题?
存储插入数据,SQL获取数据,怎么了?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,