烦了我几天的代码终于完成了。
一直没有心情写,今天终于耐着性子写完了。
居然一次通过。Happy。
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace DBUtil {
/**//// <summary>
/// 数据库里面的“种类”表的封装。
/// 这类表的特征是只有两列,一列表示种类的名称(Name),另外一列表示种类的ID(ID)。
/// 表名需要命名为“*Enum”格式。
/// </summary>
sealed class EnumEntry /**//*: IEnumerable*/ {
Singleton#region Singleton
Static Register#region Static Register
private static Dictionary<string, EnumEntry> _Reg = new Dictionary<string, EnumEntry>();
private static EnumEntry Regist(string s, EnumEntry ee) {
_Reg[s] = ee;
return ee;
}
private static EnumEntry GetRegisted(string s) {
if(_Reg.ContainsKey(s)) {
return _Reg[s];
}
else {
return null;
}
}
#endregion
public static EnumEntry GetInstance(string s) {
EnumEntry ee = GetRegisted(s);
if(ee == null) {
return Regist(s, new EnumEntry(s));
}
else {
return ee;
}
}
#endregion
Load from database to construct#region Load from database to construct
private EnumEntry(string s) {
_prefix = s;
Load();
}
private string _SelectSQL = @"Select Name From {0} Order By ID ASC";
private void Load() {
_SelectSQL = String.Format(_SelectSQL, _prefix + "Enum");
OleDbCommand comm = new OleDbCommand(_SelectSQL, _myConn.conn);
_myConn.conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
while(reader.Read()) {
_enums.Add(reader.GetString(0));
}
reader.Close();
_myConn.conn.Close();
}
#endregion
Add a enum type#region Add a enum type
private string _InsertSQL = @"Insert Into {0}
Values (?, ?)";
public void Add(string Name) {
if(_enums.IndexOf(Name) != -1) {
throw new Exception("Same name cannot be input twice!");
}
_InsertSQL = String.Format(_InsertSQL, _prefix + "Enum");
int ID = _enums.Count - 1;
//Add in object model
_enums.Add(Name);
//Add in database
IDbCommand comm = new OleDbCommand(_InsertSQL, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("ID", ID));
comm.Parameters.Add(new OleDbParameter("Name", Name));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
}
#endregion
Remove a enum type#region Remove a enum type
private string _UpdateSQL = @"Update {0}
Set ID = ?
Where ID = ?";
private string _DeleteSQLName = @"Delete From {0} Where Name = ?";
public void Remove(string Name) {
if(_enums.IndexOf(Name) == -1) {
throw new Exception("There's no such name!");
}
_DeleteSQLName = String.Format(_DeleteSQLName, _prefix + "Enum");
_UpdateSQL = String.Format(_UpdateSQL, _prefix + "Enum");
int StartID = _enums.IndexOf(Name);
int cnt = _enums.Count - StartID - 1;
//Delete from object model
_enums.Remove(Name);
//Delete from database
IDbCommand comm = new OleDbCommand(_DeleteSQLName, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("Name", Name));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
while(cnt != 0) {
IDbCommand commup = new OleDbCommand(_UpdateSQL, _myConn.conn);
commup.Parameters.Add(new OleDbParameter("ID", StartID));
commup.Parameters.Add(new OleDbParameter("ID", StartID + 1));
_myConn.conn.Open();
commup.ExecuteNonQuery();
_myConn.conn.Close();
cnt--;
StartID++;
}
}
private string _DeleteSQLID = @"Delete From {0} Where ID = ?";
public void Remove(int ID) {
if(ID < 0 || ID >= _enums.Count) {
throw new Exception("There's no such ID!");
}
_DeleteSQLID = String.Format(_DeleteSQLID, _prefix + "Enum");
_UpdateSQL = String.Format(_UpdateSQL, _prefix + "Enum");
int cnt = _enums.Count - ID - 1;
//Delete from object model
_enums.RemoveAt(ID);
//Delete from database
IDbCommand comm = new OleDbCommand(_DeleteSQLID, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("ID", ID));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
while(cnt != 0) {
IDbCommand commup = new OleDbCommand(_UpdateSQL, _myConn.conn);
commup.Parameters.Add(new OleDbParameter("ID", ID));
commup.Parameters.Add(new OleDbParameter("ID", ID + 1));
_myConn.conn.Open();
commup.ExecuteNonQuery();
_myConn.conn.Close();
cnt--;
ID++;
}
}
#endregion
public string this[int i] {
get {
return _enums[i];
}
set {
_enums[i] = value;
}
}
public int this[string name] {
get {
return _enums.IndexOf(name);
}
}
public int Count {
get {
return _enums.Count;
}
}
IEnumerable Members#region IEnumerable Members
//public IEnumerator GetEnumerator() {
// return _enums.GetEnumerator();
//}
#endregion
private List<string> _enums = new List<string>();
private string _prefix = null;
private MyConn _myConn = MyConn.GetInstance();
}
}
居然一次通过。Happy。
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace DBUtil {
/**//// <summary>
/// 数据库里面的“种类”表的封装。
/// 这类表的特征是只有两列,一列表示种类的名称(Name),另外一列表示种类的ID(ID)。
/// 表名需要命名为“*Enum”格式。
/// </summary>
sealed class EnumEntry /**//*: IEnumerable*/ {
Singleton#region Singleton
Static Register#region Static Register
private static Dictionary<string, EnumEntry> _Reg = new Dictionary<string, EnumEntry>();
private static EnumEntry Regist(string s, EnumEntry ee) {
_Reg[s] = ee;
return ee;
}
private static EnumEntry GetRegisted(string s) {
if(_Reg.ContainsKey(s)) {
return _Reg[s];
}
else {
return null;
}
}
#endregion
public static EnumEntry GetInstance(string s) {
EnumEntry ee = GetRegisted(s);
if(ee == null) {
return Regist(s, new EnumEntry(s));
}
else {
return ee;
}
}
#endregion
Load from database to construct#region Load from database to construct
private EnumEntry(string s) {
_prefix = s;
Load();
}
private string _SelectSQL = @"Select Name From {0} Order By ID ASC";
private void Load() {
_SelectSQL = String.Format(_SelectSQL, _prefix + "Enum");
OleDbCommand comm = new OleDbCommand(_SelectSQL, _myConn.conn);
_myConn.conn.Open();
OleDbDataReader reader = comm.ExecuteReader();
while(reader.Read()) {
_enums.Add(reader.GetString(0));
}
reader.Close();
_myConn.conn.Close();
}
#endregion
Add a enum type#region Add a enum type
private string _InsertSQL = @"Insert Into {0}
Values (?, ?)";
public void Add(string Name) {
if(_enums.IndexOf(Name) != -1) {
throw new Exception("Same name cannot be input twice!");
}
_InsertSQL = String.Format(_InsertSQL, _prefix + "Enum");
int ID = _enums.Count - 1;
//Add in object model
_enums.Add(Name);
//Add in database
IDbCommand comm = new OleDbCommand(_InsertSQL, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("ID", ID));
comm.Parameters.Add(new OleDbParameter("Name", Name));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
}
#endregion
Remove a enum type#region Remove a enum type
private string _UpdateSQL = @"Update {0}
Set ID = ?
Where ID = ?";
private string _DeleteSQLName = @"Delete From {0} Where Name = ?";
public void Remove(string Name) {
if(_enums.IndexOf(Name) == -1) {
throw new Exception("There's no such name!");
}
_DeleteSQLName = String.Format(_DeleteSQLName, _prefix + "Enum");
_UpdateSQL = String.Format(_UpdateSQL, _prefix + "Enum");
int StartID = _enums.IndexOf(Name);
int cnt = _enums.Count - StartID - 1;
//Delete from object model
_enums.Remove(Name);
//Delete from database
IDbCommand comm = new OleDbCommand(_DeleteSQLName, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("Name", Name));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
while(cnt != 0) {
IDbCommand commup = new OleDbCommand(_UpdateSQL, _myConn.conn);
commup.Parameters.Add(new OleDbParameter("ID", StartID));
commup.Parameters.Add(new OleDbParameter("ID", StartID + 1));
_myConn.conn.Open();
commup.ExecuteNonQuery();
_myConn.conn.Close();
cnt--;
StartID++;
}
}
private string _DeleteSQLID = @"Delete From {0} Where ID = ?";
public void Remove(int ID) {
if(ID < 0 || ID >= _enums.Count) {
throw new Exception("There's no such ID!");
}
_DeleteSQLID = String.Format(_DeleteSQLID, _prefix + "Enum");
_UpdateSQL = String.Format(_UpdateSQL, _prefix + "Enum");
int cnt = _enums.Count - ID - 1;
//Delete from object model
_enums.RemoveAt(ID);
//Delete from database
IDbCommand comm = new OleDbCommand(_DeleteSQLID, _myConn.conn);
comm.Parameters.Add(new OleDbParameter("ID", ID));
_myConn.conn.Open();
comm.ExecuteNonQuery();
_myConn.conn.Close();
while(cnt != 0) {
IDbCommand commup = new OleDbCommand(_UpdateSQL, _myConn.conn);
commup.Parameters.Add(new OleDbParameter("ID", ID));
commup.Parameters.Add(new OleDbParameter("ID", ID + 1));
_myConn.conn.Open();
commup.ExecuteNonQuery();
_myConn.conn.Close();
cnt--;
ID++;
}
}
#endregion
public string this[int i] {
get {
return _enums[i];
}
set {
_enums[i] = value;
}
}
public int this[string name] {
get {
return _enums.IndexOf(name);
}
}
public int Count {
get {
return _enums.Count;
}
}
IEnumerable Members#region IEnumerable Members
//public IEnumerator GetEnumerator() {
// return _enums.GetEnumerator();
//}
#endregion
private List<string> _enums = new List<string>();
private string _prefix = null;
private MyConn _myConn = MyConn.GetInstance();
}
}