#region Properties
public int ID
{
get;
set;
}
public string Name
{
get;
set;
}
public string MetaTitle
{
get;
set;
}
public string MetaDescription
{
get;
set;
}
public virtual int WebsiteID
{
get;
set;
}
public DateTime TimeStamp
{
get;
set;
}
#endregion Properties
#region Methods
public void Insert()
{
string sqlString = "INSERT INTO Pages ([name], [value], [meta_title], [meta_description], [website_id]) " +
"VALUES (@Name, @Image, @Description, @MetaTitle, @MetaDescription, @WebsiteID);";
SqlConnection sqlConnection =
new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
using (SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection))
{
sqlCommand.Parameters.AddWithValue("@Name", this.Name);
sqlCommand.Parameters.AddWithValue("@Image", this.Image);
sqlCommand.Parameters.AddWithValue("@Description", this.Description);
sqlCommand.Parameters.AddWithValue("@MetaTitle", this.MetaTitle);
sqlCommand.Parameters.AddWithValue("@MetaDescription", this.MetaDescription);
sqlCommand.Parameters.AddWithValue("@WebsiteID", this.WebsiteID);
try
{
sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
sqlConnection.Dispose();
}
catch (SqlException e)
{
}
}
}
Two questions:
I have another class called Section which will derive from the class above called Page. However, Section will not have a WebsiteID but will have a PageID instead. They’re both integers but how do I go about changing the property name is the derived class.
The second thing is, should I override the Insert() method to use another sqlString? If not, what is the best way of implementing such requirements?
Thanks in advance.
I think your inheritance might be a bit off. A “section” is NOT a “page”, or is it?
Why not have a base class, called
Contentor something like that, with the properties:And have
Pageextend it adding theWebsiteIDand Section adding thePageID.But I would also recommend separating the entity model from the data access. Using something like Entity Framework or NHibernate to handle persistence for you would save you from having to write that code manually.
That way you could have a more connected model, where
Sectionactually references aPageentity, and not handle IDs manually. AlsoPagecould containSectionsin the same manner (like aList<Section>), and you’d possibly have a cleaner model.