这是我的泛型方法:
public interface IDataMethods<T>
{
T Find(int id);
IEnumerable<T> Get();
bool Add<T>();
bool Update<T>();
bool Delete(int id);
}
我在以下类中继承了这个方法:
public class UserDAL : BaseDAL, IDataMethods<UserModel>
{
public IEnumerable<UserModel> Get()
{
IDbCommand cmd = Db.GetCommand("SELECT Id, givenName, familyName, email from [User]", CommandType.Text);
Db.OpenConnection();
IDataReader result = cmd.ExecuteReader();
IEnumerable<UserModel> collection = result.SetDataToModels<UserModel>();
Db.CloseConnection();
return collection;
}
public UserModel Find(int id)
{
long ts = 0;
long te = 0;
ts = Environment.TickCount;
IDbCommand cmd = Db.GetCommand("SELECT Id, givenName, familyName, email from [User] WHERE Id=@Id", CommandType.Text);
cmd.ParamIn("@Id", id.ToString(), DbType.Int32);
Db.OpenConnection();
IDataReader result = cmd.ExecuteReader();
UserModel entity = result.SetDataToModel<UserModel>();
Db.CloseConnection();
te = Environment.TickCount - ts;
return entity;
}
public bool Add<T>(UserModel um)
{
IDbCommand cmd = Db.GetCommand("INSERT INTO [User] (givenName, familyName, email)VALUES(" + um.givenName + "," + um.familyName + "," + um.email + ")");
cmd.ParamIn("@givenName", um.givenName, DbType.String);
cmd.ParamIn("@familyName", um.givenName, DbType.String);
cmd.ParamIn("@email", um.givenName, DbType.String);
Db.OpenConnection();
cmd.ExecuteNonQuery();
Db.CloseConnection();
return true;
}
public bool Update<T>()
{
UserModel um
IDbCommand cmd = Db.GetCommand("UPDATE [User] SET(givenName=" + um.givenName + ",familyName=" + um.familyName + ",email="+um.email+") WHERE Id="+um.Id+"");
cmd.ParamIn("@givenName", um.givenName, DbType.String);
cmd.ParamIn("@familyName", um.familyName, DbType.String);
cmd.ParamIn("@email", um.email, DbType.String);
Db.OpenConnection();
cmd.ExecuteNonQuery();
Db.CloseConnection();
return true;
}
public bool Delete(int id)
{
IDbCommand cmd = Db.GetCommand("DELETE givenName,familyName,email FROM [User] WHERE Id=@Id", CommandType.Text);
cmd.ParamIn("@Id", id.ToString(), DbType.Int32);
Db.OpenConnection();
cmd.ExecuteNonQuery();
Db.CloseConnection();
return true;
}
}
在这里,我的删除和查找工作正常,但我的添加和更新不工作。我不知道如何为此添加和更新传递输入参数。这里(UserModel um)这是我的模型类,其中包含该属性,但它不起作用。