前面说过,所有的管理信息都以DataSet的形式存入了ml3文件中,那么,我们需要一个类,它能够读取这个ml3文件,并且将读取的结果返回给我们:
class TSysData
...{
private DataSet FDataSet;
public DataSet DataSet
...{
get
...{
return FDataSet;
}
}
public TSysData(string fileName)
...{
FDataSet = new DataSet();
try
...{
FDataSet.ReadXml(fileName);
}
catch (Exception)
...{
TSysDataCheck.CheckData(FDataSet, fileName);
}
}
}
注意最后一句TSysDataCheck.CheckData(FDataSet, fileName),这是让类TSysDataCheck来检查数据结构是否正确,这个后面再说。
其实这里有一个bug,如果这个文件是通过其他数据集.WriteXml来得到的,将不会引发异常。但由于是自己使用,我相信自己会避免这种错误,因此没有做其他的处理。(其实修改一下是很简单的事情。)
在TSysDataCheck中检查数据结构,首先要检查某个表是否存在:
private static void CheckTable(DataSet dataSet, string tableName)
...{
bool have;
DataTable testdataTable = null;
try
...{
testdataTable = dataSet.Tables[tableName];
have = true;
}
catch (Exception)
...{
have = false;
}
if ((!have) || (testdataTable == null))
...{
DataTable dataTable = new DataTable(tableName);
dataSet.Tables.Add(dataTable);
}
}
其次,检查某个表里是某个字段是否存在:
private static void CheckField(DataSet dataSet, string tableName, string fieldName, int fieldType, int fieldLength)
...{
bool have;
DataColumn testdataColumn = null;
try
...{
testdataColumn = dataSet.Tables[tableName].Columns[fieldName];
have = true;
}
catch (Exception)
...{
have = false;
}
if ((!have) || (testdataColumn == null))
...{
DataColumn dataColumn = new DataColumn(fieldName);
switch (fieldType)
...{
case 1:
dataColumn.DataType = typeof(string);
dataColumn.MaxLength = fieldLength;
break;
case 2:
dataColumn.DataType = typeof(int);
break;
case 3:
dataColumn.DataType = typeof(DateTime);
break;
default:
dataColumn.DataType = typeof(string);
dataColumn.MaxLength = 50;
break;
}
dataSet.Tables[tableName].Columns.Add(dataColumn);
}
其中,fieldType是一个int型常量,代表了常用的数据类型(fieldType 1,string 2 int 3 DateTime)。fieldLength只有当fieldType=1,为string时才有作用。
最后,可能需要为表指定主键:
private static void SetPreKey(DataSet dataSet, string tableName, string fieldName)
...{
DataColumn[] cols = new DataColumn[1];
cols[0] = dataSet.Tables[tableName].Columns[fieldName];
dataSet.Tables[tableName].PrimaryKey = cols;
}
有了这三个方法,就可以检查执行数据结构的操作:
public static void CheckData(DataSet dataSet, string fileName)
...{
if (System.IO.Path.GetExtension(fileName).ToUpper() != ".ML3")
...{
MessageBox.Show("文件后缀名不为".ml3"");
Application.Exit();
System.Threading.Thread.CurrentThread.Abort();
return;
}
MStyle#region MStyle
CheckTable(dataSet, "MStyle");
CheckField(dataSet, "MStyle", "sid", 1, 50);
CheckField(dataSet, "MStyle", "sname", 1, 255);
CheckField(dataSet, "MStyle", "pid", 1, 50);
CheckField(dataSet, "MStyle", "sort", 2, 0);
SetPreKey(dataSet, "MStyle", "sid");
#endregion
MLogList#region MLogList
CheckTable(dataSet, "MLogList");
CheckField(dataSet, "MLogList", "sid", 1, 50);
CheckField(dataSet, "MLogList", "lid", 1, 50);
CheckField(dataSet, "MLogList", "lname", 1, 255);
CheckField(dataSet, "MLogList", "lCreateTime", 3, 0);
CheckField(dataSet, "MLogList", "lLastChangeTime", 3, 0);
SetPreKey(dataSet, "MLogList", "lid");
#endregion
MFileList#region MFileList
CheckTable(dataSet, "MFileList");
CheckField(dataSet, "MFileList", "lid", 1, 50);
CheckField(dataSet, "MFileList", "fid", 1, 50);
CheckField(dataSet, "MFileList", "fname", 1, 255);
CheckField(dataSet, "MFileList", "fCreateTime", 3, 0);
SetPreKey(dataSet, "MFileList", "fid");
#endregion
}
各个字段的含义很简单,从字段名就可以看出来。
检查完数据结构,可能还需要一些初始化数据与其他操作:
1。我希望每个ml3文件都有一个id,与其他的ml3文件区分开来(这里使用了Guid.而这个id在后面也会用到。):
ML3FileID#region ML3FileID
CheckTable(dataSet, "ML3FileID");
CheckField(dataSet, "ML3FileID", "ML3ID", 1, 50);
SetPreKey(dataSet, "ML3FileID", "ML3ID");
#endregion
ML3FileID data#region ML3FileID data
if (dataSet.Tables["ML3FileID"].Rows.Count == 0)
...{
DataRow row = dataSet.Tables["ML3FileID"].NewRow();
row.BeginEdit();
row["ML3ID"] = Guid.NewGuid().ToString().ToUpper();
row.EndEdit();
dataSet.Tables["ML3FileID"].Rows.Add(row);
}
#endregion
2。在日志类型中,要有一个默认的类型,即回收站:
MStyle data#region MStyle data
DataRow[] rows = dataSet.Tables["MStyle"].Select("sid = 1");
bool have = (rows != null);
if (have)
have = (rows.Length == 1);
if (!have)
...{
DataRow row = dataSet.Tables["MStyle"].NewRow();
row.BeginEdit();
row["sid"] = "1";
row["pid"] = "-1";
row["sname"] = "回收站";
row["sort"] = 99999;
row.EndEdit();
dataSet.Tables["MStyle"].Rows.Add(row);
}
#endregion
这里指定了回收站的id="1",sort=9999。
3。最后,需要将这些数据写回到ml3文件中:
dataSet.WriteXml(fileName, XmlWriteMode.WriteSchema);
这样,数据结构检查也便完成了。
下一篇,启动。
ie.2008-04-08