public abstract class GeneralDB : IEntity
{
protected DataTable table;
protected int currentRow;
protected string primaryKey;
public GeneralDB(string tableName, string primaryKey)
{
DAL.GetInstance().AddTable(tableName);//bring data from database
table = DAL.GetInstance().GetTable(tableName);
this.primaryKey = primaryKey;
if (IsEmpty())
currentRow = -1;
else
currentRow = 0;
}
#region NAVIGATION
/// <summary>
/// going first line
/// </summary>
public void GoToFirst()
{
if (IsEmpty())
throw new Exception("navigate on empty table");
currentRow = 0;
}
/// <summary>
/// going to last line
/// </summary>
public void GoToLast()
{
if (IsEmpty())
throw new Exception("navigate on empty table");
currentRow = table.Rows.Count - 1;
}
/// <summary>
/// go next line if in the end go back to first
/// </summary>
public void MoveNext()
{
if (IsEmpty())
throw new Exception("navigate on empty table");
currentRow = (currentRow + 1) % table.Rows.Count;
}
/// <summary>
/// moves to the previous object. If reaches the beginning, goes back
/// to the end
/// </summary>
public void MovePrev()
{
if (IsEmpty())
throw new Exception("navigate on empty table");
if (this.currentRow == 0)
currentRow = table.Rows.Count - 1;
else
--currentRow;
}
/// <summary>
/// search obj by its key
/// </summary>
/// <param name="key">the key being looked for</param>
/// <returns>true if found and false if no such row exists</returns>
public bool Find(object key)
{
int r = 0;
foreach (DataRow dr in table.Rows)
{
if (dr[primaryKey].Equals(key))
{
currentRow = r;
return true;
}
else
r++;
}
return false;
}
/// <summary>
/// return current row
/// </summary>
/// <returns>Data Row of current row</returns>
protected DataRow GetThisRow()
{
return table.Rows[currentRow];
}
#endregion
#region GENERAL OPERATIONS
/// <summary>
/// return num of lines
/// </summary>
/// <returns>number of rows</returns>
public int Size()
{
return table.Rows.Count;
}
/// <summary>
/// check if table is empty
/// </summary>
/// <returns> true if empty, false if not empty</returns>
public bool IsEmpty()
{
return table.Rows.Count == 0;
}
public virtual void Save()
{
DAL.GetInstance().Update(table.TableName);
}
#endregion
public DataRow[] Filter(string filterString)
{
if (filterString.Trim().Length == 0)
return table.Select();
return table.Select(filterString);
}
#region CRUD
public void Add(IEntity obj)
{
DataRow dr = table.NewRow();
obj.Populate(dr);
table.Rows.Add(dr);
Find(dr[primaryKey]);
}
public void UpdateRow(IEntity obj)
{
DataRow dr = GetThisRow();
obj.Populate(dr);
}
public void UpdateRowWithStock(IEntity obj)
{
obj.PopulateStock(table.Rows[currentRow]);
}
public virtual void DeleteCurrentRow()
{
string sqlString = "Delete from " + table.TableName + " where " + primaryKey + " = " + table.Rows[currentRow][primaryKey].ToString();
DAL.GetInstance().ExecuteNonQuery(sqlString);
MoveNext();
object key = table.Rows[currentRow][primaryKey];
MovePrev();
table.Rows.Remove(GetThisRow());
if (!Find(key))
currentRow = -1;
}
#endregion
public abstract object GetCurrentRow();
public string GetNameType() { return table.TableName; }
public abstract void Populate(DataRow dr);
}
}
我想知道如何解决这个错误,因为写这个没有帮助:
它告诉我在generaldb中添加这个public void populatestock(datarow dr),我不明白我应该在那里写什么....
它在这一行中给我一个错误:" throw new NotImplementedException(); "
它告诉我:"The method or operation is not implemented."
无论如何,任何人都可以帮我处理这个错误:
" Error1 'MatankProj.DB.GeneralDB' does not implement interface
member'MatankProj.Entities.IEntity.PopulateStock(System.Data.DataRow)' "
答案 0 :(得分:0)
首先,您应该参考接口MatankProj.Entities.IEntity
的文档。在那里,您应该能够收集信息,实施PopulateStock(System.Data.Row)
的意图。其次,你已经谈过Visual Studio(我假设)给你错误throw new NotImplementedException();
。实际上,这不是错误。您似乎试图通过单击从IEntity实施成员之类的内容来尝试自动实现缺少的成员。然后Visual Studio总是做这样的事情:
public void PopulateStock(System.Data.DataRow dataRow)
{
throw new NotImplementedException();
}
...因为VS希望您用自己的代码替换throw new NotImplementedException();
。
既然你说它“告诉你”方法或操作没有实现。,该方法至少执行一次,因此你应该注意这一点。如果您能够找到MatankProj.Entities.IEntity
的另一个实现,请查看该实现。