これは私のジェネリックメソッドです:
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;
}
}
ここで、削除と検索は正常に機能していますが、追加と更新は機能していません。この Add と Update の入力パラメーターを渡す方法がわかりません。ここ(UserModel um)は、プロパティを含む私のモデルクラスですが、機能していません。