This is my Database Table Notes(pkId (Guid) NotesID,string Name,string Author,string Text) and all the properties of Notes are in Notes class implemented INotifyPropertyChanged in Entity class below. Okay all I supposed to do manipulate Data using CRUD
Method e.g Save/Update notes below....
I like to Apologies for my poor English in advance
public class Entity : System.ComponentModel.INotifyPropertyChanged
{
/// <summary>
/// the property changed event handler
/// </summary>
private System.ComponentModel.PropertyChangedEventHandler propertyChangedEventHandler;
/// <summary>
/// used for concurrency
/// </summary>
private object eventLock = new object();
private System.Guid id;
// <summary>
/// Occurs when a property value changes.
/// </summary>
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged
{
add
{
lock (this.eventLock)
{
this.propertyChangedEventHandler += value;
}
}
remove
{
lock (this.eventLock)
{
this.propertyChangedEventHandler -= value;
}
}
}
/// <summary>
/// Gets the id
/// </summary>
public Guid Id
{
get { return this.id; }
}
/// <summary>
/// Raises the <see cref="E:PropertyChanged"/> event.
/// </summary>
/// <param name="args">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
System.ComponentModel.PropertyChangedEventHandler handler = this.propertyChangedEventHandler;
if (handler != null)
{
handler(this, args);
}
}
/// <summary>
/// Called when [property changed].
/// </summary>
/// <param name="propertyName">Name of the property.</param>
protected void OnPropertyChanged(string propertyName)
{
this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
in this class I didn't include the database connection string but I do understand how it suppose to be
public class SqlDatabase
{
public object ExecScalar(string sql)
{
object obj = null;
using (System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand(sql, this._Connection))
{
command.CommandType = System.Data.CommandType.Text;
obj = command.ExecuteScalar();
}
return obj;
}
public int ExecuteSQL(string sql)
{
ValidateDisposed();
System.Diagnostics.Debug.Assert(this._Connection != null);
int num = 0;
using (System.Data.SqlClient.SqlCommand command =
new System.Data.SqlClient.SqlCommand(sql, this._Connection))
{
command.CommandType = System.Data.CommandType.Text;
System.Diagnostics.Debug.Assert(this._Connection != null);
num = command.ExecuteNonQuery();
}
return num;
}
}
This is a Notes class inside common scripts.Common Projects
public class Note : Entity
{
/// <summary>
/// Private field Author
/// </summary>
private string author;
/// <summary>
/// the private field text
/// </summary>
private string text;
/// <summary>
/// the private field name
/// </summary>
private string name;
/// <summary>
/// Gets or private sets the Author
/// </summary>
public string Author
{
get
{
return this.author;
}
set
{
value = value ?? string.Empty;
if (string.Compare(this.author, value, System.StringComparison.InvariantCulture) == 0)
{
this.author = value;
this.OnPropertyChanged(ClassMetadata.GetPropertyName(() => this.Author));
}
}
}
/// <summary>
/// Gets or sets the Name
/// </summary>
public string Name
{
get
{
return this.name;
}
set
{
value = value ?? string.Empty;
if (string.Compare(this.name, value, System.StringComparison.InvariantCulture) == 0)
{
this.name = value;
this.OnPropertyChanged(ClassMetadata.GetPropertyName(() => this.Name));
}
}
}
/// <summary>
/// Gets or private sets Text
/// </summary>
public string Text
{
get
{
return this.text;
}
set
{
value = value ?? string.Empty;
if (string.Compare(this.text, value, System.StringComparison.InvariantCulture) == 0)
{
this.text = value;
this.OnPropertyChanged(ClassMetadata.GetArgumentName(() => this.Text));
}
}
}
}
I have interface called EntityPersister look like this
public interface IEntityPersister
{
void Save(Entity item);
Entity Load(System.Guid id, System.Type entityType);
Entity[] LoadAll(System.Type entityType);
}
Lastly my SqlServerEntityPersister below that implement the IEntityPersister CRUD Method and I am struggling to save, Insert properties of Notes
public class SqlServerEntityPersister : IEntityPersister
{
public void Save(Entity item)
{
if (item.GetType() == typeof(Note))
{
// build the insert statement for the note
using (Data.Sql.SqlDatabase db=new Data.Sql.SqlDatabase())
{
if (db.ExecuteSQL("UPDATE Notes SET Author=@Author,Text=@Text ,Name = @Name WHERE Noteid = @Noteid") == 0)
db.ExecuteSQL("Insert into Notes (NoteId,Author,text,Name) values (@NoteId,@Author @Text,@Name)");
var updateNote = item as Note;//starting here
Note notes = new Note()
{
Author = "@Author",
Text = "@Text",
Name = "@Name",
NoteId = System.Guid.NewGuid()
};
//normally we use cmd.Parameter.Add("@Name",Name);
//cmd.ExecuteNonQuery();
//this where I am stuck because I cannot update or insert the above code
}
}
}
}
Ok on SqlServerEntityPersister class I m struggling to update or Insert Method I don't know how to update Notes properties to the database table eg Author,Name,Text Please Guys I am not using LinQ and EF.