如有一表t:
t1 int not null 主键 递增(1,1)
t2 int null
t3 int null
------------
Model 实体类设计如:
public class t
{
protected int _t1;
protected int _t2:
protected int _t3;
public t(){}
public t(int myid)
{
_t1 = myid ;
}
//主键
public int Id
{
get{return _t1;}
}
public int t2
{
get{return _t2;}
set{_t2=values}
} public int t3
{
get{return _t3}
set{_t3=value}
}
}如何设计Dal类 来操作上面的Model实体类??
(假设是Sql server数据库:假设connection =conn command=cmd 对象创建好了的,可直接用的。)
public class dal_t
{
//如何创建
//+1 重载 创建新类 可调用t_Insert 保存为一条新记录,但是如何取Id值(因为Id是递增的,如取数据库最后一条记录+1值,但入同时有人创建新类怎么办?)
public static t Create_t()
{
return new t(); //这里Id值如何设置
} //+2 重载 从现有边创建类
public static t Create_t(int id)
{
cmd.CommandText = "Select*form t where t1 = " + id;
DataReader dr = cmd.ExecuteReader();
dr.Reader();
t tc = new tc (Convert.ToInt32(dr[0])) ;
tc.t2 = Convert.ToInt32(dr[1]);
tc.t3 = Convert.ToInt32(dr[2]); return t3;
}
//如何添加
public static bool t_Insert(t myt)
{
//因该如何写代码 关键是保存Id值是一样的 如何做
} //如何编辑
public static bool t_Update(t myt)
{
cmd.CommandText="update t set t2 ="+ myt.t2 + " set t3 = "+myt.t3 +" where t = "+myt.id;
int b = cmd.ExecuteNonQuery();
if(b ==0)
{
return false;
}else{
return true;
}
} //如何删除
public static bool t_Update(int id)
{
cmd.CommandText="Delete From t Where t1 = " + id ;
int b = cmd.ExecuteNonQuery();
if(b ==0)
{
return false;
}else{
return true;
}
}
}对于我的Dal类谁帮我实现下创建新类 保存新类功能,还有谁对我这样的设计有更好建议告诉我啊.万分感激.
另:谁有用CodeSmith 实现这两层的摸版代码cst文件,谁有能共享下,万分感激.请发到 ..
t1 int not null 主键 递增(1,1)
t2 int null
t3 int null
------------
Model 实体类设计如:
public class t
{
protected int _t1;
protected int _t2:
protected int _t3;
public t(){}
public t(int myid)
{
_t1 = myid ;
}
//主键
public int Id
{
get{return _t1;}
}
public int t2
{
get{return _t2;}
set{_t2=values}
} public int t3
{
get{return _t3}
set{_t3=value}
}
}如何设计Dal类 来操作上面的Model实体类??
(假设是Sql server数据库:假设connection =conn command=cmd 对象创建好了的,可直接用的。)
public class dal_t
{
//如何创建
//+1 重载 创建新类 可调用t_Insert 保存为一条新记录,但是如何取Id值(因为Id是递增的,如取数据库最后一条记录+1值,但入同时有人创建新类怎么办?)
public static t Create_t()
{
return new t(); //这里Id值如何设置
} //+2 重载 从现有边创建类
public static t Create_t(int id)
{
cmd.CommandText = "Select*form t where t1 = " + id;
DataReader dr = cmd.ExecuteReader();
dr.Reader();
t tc = new tc (Convert.ToInt32(dr[0])) ;
tc.t2 = Convert.ToInt32(dr[1]);
tc.t3 = Convert.ToInt32(dr[2]); return t3;
}
//如何添加
public static bool t_Insert(t myt)
{
//因该如何写代码 关键是保存Id值是一样的 如何做
} //如何编辑
public static bool t_Update(t myt)
{
cmd.CommandText="update t set t2 ="+ myt.t2 + " set t3 = "+myt.t3 +" where t = "+myt.id;
int b = cmd.ExecuteNonQuery();
if(b ==0)
{
return false;
}else{
return true;
}
} //如何删除
public static bool t_Update(int id)
{
cmd.CommandText="Delete From t Where t1 = " + id ;
int b = cmd.ExecuteNonQuery();
if(b ==0)
{
return false;
}else{
return true;
}
}
}对于我的Dal类谁帮我实现下创建新类 保存新类功能,还有谁对我这样的设计有更好建议告诉我啊.万分感激.
另:谁有用CodeSmith 实现这两层的摸版代码cst文件,谁有能共享下,万分感激.请发到 ..
现在碰到数据类型处理 要自己写 好难,谁要现成的 我是C#来写Template
摸版的.
public class Model_t
{
protected int _t1;
protected int _t2;
protected int _t3;
public Model_t() { }
//主键
public int Id
{
get { return _t1; }
}
public int t2
{
get { return _t2; }
set { _t2 = value; }
} public int t3
{
get { return _t3; }
set { _t3 = value; }
}
} //项目二,有必要写接口
public interface I_t
{
//比如这里有个添加数据的方法
bool t_Add(Model_t model);
} //项目三,数据层
public class SQL_t :I_t
{
bool I_t.t_Add(Model_t model)
{
SqlParameter[] para = {
new SqlParameter("@t2", SqlDbType.VarChar,5),
new SqlParameter("@t3", SqlDbType.VarChar,5)};
para[0].Value = model.t2;
para[1].Value = model.t3;
SqlCommand cmd = new SqlCommand("insert t(t2,t3) values(@t2,t3)", "fdsafa");
foreach (SqlParameter parm in para)
{
cmd.Parameters.Add(parm);
}
return cmd.ExecuteNonQuery() > 0;
}
}
//DAL,其实应该写反射到相关的数据处理类的.这里我省略了.
public class DAL
{
I_t dal = new SQL_t();
//简单的处理方法,我直接return出来,具体你要怎么样处理,自己决定
public bool t_Add(Model_t model)
{
return dal.t_Add(model);
}
} //例子 public class test
{
//添加信息
public void test()
{
Model_t mod = new Model_t();
mod.t2 = "aa";
mod.t3 = "bb";
new DAL().t_Add(mod);
}
public class DAL
{
I_t dal = new SQL_t();
//简单的处理方法,我直接return出来,具体你要怎么样处理,自己决定
public bool t_Add(Model_t model)
{
return dal.t_Add(model);
}
} 我这里把他反射实体类Model_t
如:
public class Model_t
{
protected int _t1;
protected int _t2;
protected int _t3;
protected i_t it = new SQL_t();
public Model_t() { }
/*....代码*/
//把方法反射到这里
public bool t_Add()
{
it.t_Add(this);
}
} 我这样做到底行不行啊.符合不符合分层结构啊.
大家给点意见:
表 t:
结构: t1 int not null 主键 递增(1,1)
t2 int null
t3 varchar(50) null//实体类
public class Model
{
protected int _t1;
protected int _t2:
protected int _t3;
public t(){}
public t(int myid)
{
_t1 = myid ;
}
//主键
public int Id
{
get{return _t1;}
}
public int t2
{
get{return _t2;}
set{_t2=values}
} public int t3
{
get{return _t3}
set{_t3=value}
}
}//实体类接口 IModel
public inertface IModel
{
//添加
bool Add_Model(Model md);
//删除
bool Updat_Model(Model md);
//删除
bool Delete_Model(Model md);
}//方法类接口 IDal
public inertface IDal
{
Model Create_Model(int id); //根据Id创建对象
Dataset WriteData(); //返回多行数据
}//方法类 Dal 实现接口 IModel IDal ,这里限制了Dal对直接使用IModel接口方法
public class Dal:IModel,IDal
{
public bool IModel.Add_Model(Model md)
{
//对应Sql代码
}
public bool IModel.Updat_Model(Model md)
{
//对应Sql代码
} public bool IModel.Delete_Model(Model md)
{
//对应Sql代码
}
public Model Create_Model(int id)
{
//对应Sql代码
}
public DataSet WriteData(int id)
{
//对应Sql代码
}
}//下面我们IModel 方法在反射到Model类中使用//重写我们的Model类
public class Model
{
protected int _t1;
protected int _t2:
protected int _t3;
protected IModel imd = new Dal();
public t(){}
public t(int myid)
{
_t1 = myid ;
}
//主键
public int Id
{
get{return _t1;}
}
public int t2
{
get{return _t2;}
set{_t2=values}
} public int t3
{
get{return _t3}
set{_t3=value}
}
//添加 删除 编辑 方法
public bool Add_Model()
{
imd.Add_Model(this);
} public bool Update_Model()
{
imd.Update_Model(this);
} public bool delete_Model()
{
imd.Delete_Model(this);
}
}