我有一些代表数据库表的类(实体)
public class ALBUM
{
public int Id { get; set; }
public string Name { get; set; }
public int Tracks { get; set; }
}
public class BOOK
{
public int Id { get; set; }
public string Author { get, set; }
}
然后获取各个存储过程的参数,插入,更新,任何
public Command getParameters(string spName, dynamic entity, Connection conn)
{
Command cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
// Get properties (fields) from entity (table)
PropertyInfo[] properties = entity.GetType().GetProperties();
for (int = 0; i < properties.Length; i++)
{
// here I set Parameters: name, DBType, and Value from properties
}
}
现在我打电话
public void Some()
{
ALBUM someAlbum = new ALBUM();
BOOK anyBook = new BOOK(); // all respective data from DB is set
Command newCommand = getParameters("myAlbumSP", someAlbum, myConnection);
Command newCommand = getParameters("mySPBooks", anyBook, myConnection);
}
我如何定义“实体”参数?,不是动态的。
它有效,只是我正在寻找其他方法来做到这一点,我相信有很多。