0

这是我的泛型方法:

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)这是我的模型类,其中包含该属性,但它不起作用。

4

2 回答 2

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;
    }   
}
于 2013-08-19T12:17:20.237 回答
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();
    }
}   
于 2013-08-19T13:13:10.773 回答