这是我的泛型方法:
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嗯)这是我的modelclass,它包含属性,但它不起作用。
答案 0 :(得分:0)
试试这个
public interface IDataMethods<T>
{
T Find(int id);
IEnumerable<T> Get();
bool Add(T t);
bool Update(T 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(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(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;
}
}
答案 1 :(得分:0)
正如我在评论中所写,您需要更改界面以传递应更新/添加的模型。
您还应该使用参数化命令并使用using语句或try / finally安全地处理已使用的资源。
如果我理解了模型并且没有做出任何复制/粘贴错误,那么应该这样做:
public interface IDataMethods<T>
{
T Find(int id);
IEnumerable<T> Get();
bool Add<T>(T model);
bool Update<T>(T model);
bool Delete(int id);
}
public class UserDAL : BaseDAL, IDataMethods<UserModel>
{
public IEnumerable<UserModel> Get()
{
Db.OpenConnection();
try
{
using(IDbCommand cmd = Db.GetCommand("SELECT Id, givenName, familyName, email from [User]", CommandType.Text))
using(IDataReader result = cmd.ExecuteReader())
{
return result.SetDataToModels<UserModel>();
}
finally
{
Db.CloseConnection();
}
}
public UserModel Find(int id)
{
Db.OpenConnection();
try
{
using(IDbCommand cmd = Db.GetCommand("SELECT Id, givenName, familyName, email from [User] WHERE Id=@Id", CommandType.Text))
{
cmd.ParamIn("@Id", id.ToString(), DbType.Int32);
using(IDataReader result = cmd.ExecuteReader())
{
return result.SetDataToModel<UserModel>();
}
}
}
finally
{
Db.CloseConnection();
}
}
public bool Add(UserModel um)
{
Db.OpenConnection();
try
{
using(IDbCommand cmd = Db.GetCommand("INSERT INTO [User] (givenName, familyName, email)VALUES(@givenName, @familyName, @email)")
{
cmd.ParamIn("@givenName", um.givenName, DbType.String);
cmd.ParamIn("@familyName", um.givenName, DbType.String);
cmd.ParamIn("@email", um.email, DbType.String);
int affected = cmd.ExecuteNonQuery();
return affected > 0;
}
}
finally
{
Db.CloseConnection();
}
}
public bool Update(UserModel um)
{
Db.OpenConnection();
try
{
using(IDbCommand cmd = Db.GetCommand("UPDATE [User] SET givenName=@givenName, familyName=@familyName, email=@email WHERE Id=@Id"))
{
cmd.ParamIn("@givenName", um.givenName, DbType.String);
cmd.ParamIn("@familyName", um.familyName, DbType.String);
cmd.ParamIn("@email", um.email, DbType.String);
return cmd.ExecuteNonQuery() == 1;
}
}
finally
{
Db.CloseConnection();
}
}
public bool Delete(int id)
{
try
{
Db.OpenConnection();
using(IDbCommand cmd = Db.GetCommand("DELETE givenName,familyName,email FROM [User] WHERE Id=@Id", CommandType.Text))
{
cmd.ParamIn("@Id", id.ToString(), DbType.Int32);
return cmd.ExecuteNonQuery() == 1;
}
}
finally
{
Db.CloseConnection();
}
}