所谓元数据就是描述数据的数据元数据库就是存放描述数据的数据元数据主要用于描述数据的特征等的数据在很多场合下都会遇到元数据例如文件系统里面有这里介绍的元数据主要是指描述空间数据特征的元数据例如属于哪一类数据等
今天由于时间关系就简单介绍一下元数据库管理的实现
初始化显示元数据库信息的头部信息
[csharp] view plaincopy /// <summary> /// 初始化datagridView的头部显示信息/// </summary> private void InitDataGridView()
{ //dataGridViewXRowsClear()SqlHelper sh = new SqlHelper()string sql = select idnamedescription from jcsjk_databaseinfoDataSet ds = shReturnDataSet(sql jcsjk_databaseinfo)dataGridViewXDataSource = dsTables[]
dataGridViewXColumns[]HeaderText = 元数据库IDdataGridViewXColumns[]Width = dataGridViewXColumns[]HeaderText = 元数据库名称dataGridViewXColumns[]Width =
dataGridViewXColumns[]HeaderText = 元数据库描述信息dataGridViewXColumns[]Width =
} 删除元数据库
[csharp] view plaincopy /// <summary> /// 删除元数据库/// </summary> /// <param name=sender></param> /// <param name=e></param> private void delMetaDatabaseBtn_Click(object sender EventArgs e)
{ //删除所有表if (dataGridViewXCurrentRowIndex < )
{ MessageBoxShow(请选择需要删除的数据库那一行)return} string strDatabaseName = dataGridViewXRows[dataGridViewXCurrentRowIndex]Cells[]ValueToString()SqlHelper sh = new SqlHelper()string sql = select table_name from user_tables where table_name like + strDatabaseNameToUpper() + /_% ESCAPE /OracleDataReader odr = shReturnDataReader(sql)
while (odrRead())
{ //删除关联外键string strTemp = odr[]ToString()ToUpper()strTemp = strTempSubstring(strDatabaseNameLength + )sql = select table_nameconstraint_name from user_constraints where constraint_type=R + and constraint_name like + strTemp + %OracleDataReader odr = shReturnDataReader(sql)
while (odrRead())
{ sql = ALTER TABLE + odr[]ToString()ToUpper() + DROP CONSTRAINT + odr[]ToString()ToUpper()shExecuteSQL(sql)} sql = drop table + odr[]ToString()ToUpper()shExecuteSQL(sql)} //删除记录Hashtable ht = new Hashtable()htAdd(name strDatabaseName)try { shDel(jcsjk_databaseinfo name= + strDatabaseName + ht)} catch (Exception)
{ LogHelpwriteLog(taUsername 元数据库管理删除元数据库< + strDatabaseName + >失败!)MessageBoxShow(删除元数据库< + strDatabaseName + >失败!)}
LogHelpwriteLog(taUsername 元数据库管理删除元数据库< + strDatabaseName + >成功!)MessageBoxShow(删除元数据库< + strDatabaseName + >成功!)}
清空元数据库
[csharp] view plaincopy /// <summary> /// 清空元数据库中的数据/// </summary> /// <param name=sender></param> /// <param name=e></param> private void clearmetaDataBtn_Click(object sender EventArgs e)
{ if (dataGridViewXCurrentRowIndex < )
{ MessageBoxShow(选择数据库!)return} SqlHelper sh = new SqlHelper()string strDatabaseName = dataGridViewXCurrentRowCells[]ValueToString()//NewDataSet nds = new NewDataSet()string sql = stringEmptysql = select table_name from user_tables where table_name like + strDatabaseNameToUpper() + /_% ESCAPE /OracleDataReader odr = shReturnDataReader(sql)
while (odrRead())
{ sql = delete from + odr[]ToString()ToUpper()try { shExecuteSQL(sql)} catch (Exception)
{ LogHelpwriteLog(taUsername 元数据库管理清空元数据库< + strDatabaseName + >的数据失败!)MessageBoxShow(清空元数据库< + strDatabaseName + >的数据失败!)return} LogHelpwriteLog(taUsername 元数据库管理清空元数据库< + strDatabaseName + >的数据成功!)MessageBoxShow(清空元数据库< + strDatabaseName + >的数据成功!)/* foreach (DataTable dt in ndsTables)
{ string strTableName = strDatabaseName + _ + dtTableNamesql = select * from + strTableNameDataSet ds = new DataSet()
OracleDataAdapter oda = new OracleDataAdapter(sqlConfigurationSettingsAppSettings[ConnectionString])try { odaFill(ds)odaUpdate(dt)} catch (Exception ex)
{ LogHelpwriteLog(taUsername 元数据库管理清空元数据库< + strDatabaseName + >的数据失败!)MessageBoxShow(清空元数据库< + strDatabaseName + >的数据失败!)} LogHelpwriteLog(taUsername 元数据库管理清空元数据库< + strDatabaseName + >的数据成功!)MessageBoxShow(清空元数据库< + strDatabaseName + >的数据成功!)* */ }
清空元数据库与删除元数据库是不同的功能删除元数据库就是把存放元数据的库都一起删除了但是清空元数据库只是把库里面的内容删除库本身存在
创建元数据库
这是比较复杂的一个功能因为创建一个元数据库需要根据一定的标准来创建这些标准都是通过xsd文件描述的所以首先要解析这个xml的模式描述文件然后根据解析的内容存放到相应的字段中去具体实现如下
[csharp] view plaincopy private void createBtn_Click(object sender EventArgs e)
{ SqlHelper sh = new SqlHelper()string sql = stringEmpty
if (databaseIDTxtTextTrim() == || databaseNameTxtTextTrim() == )
{ errTxtText = 数据库ID或名称不能为空return}
//查看元数据库是否存在sql = select * from jcsjk_databaseinfo where name= + databaseNameTxtText +
if (shGetRecordCount(sql) > )
{ MessageBoxShow(此元数据库已经存在请从新命名元数据库!)return}
Hashtable ht = new Hashtable()string strContent = stringEmptyDataSet ds = new DataSet()//从文件标准创建if (!checkBoxXChecked)
{ if (metaFileTxtText == || PathGetExtension(metaFileTxtText)ToLower() != xsd)
{ errTxtText = 请选择正确的XSD文件return} if (metaIDTxtTextTrim() == )
{ errTxtText = 元数据标准ID不能为空return}
//读入xsd文件dsReadXmlSchema(metaFileTxtText)
//create table in tablespace try { OracleDataSchemaAdapter odsa = new OracleDataSchemaAdapter()odsaCreate(ds true databaseNameTxtText + _ SDE)} catch (ArgumentException ae)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(—— Error —— + aeMessage + —— Instructions ——)} catch (FileNotFoundException)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(File not found + metaFileTxtText)} catch (Exception)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(创建元数据库< + databaseNameTxtText + >失败!)}
//写入元数据标准到数据库StreamReader sr = new StreamReader(new FileStream(metaFileTxtText FileModeOpen)SystemTextEncodingGetEncoding(GB))
while (!srEndOfStream)
{ strContent += srReadLine()} srClose()string strName = PathGetFileNameWithoutExtension(metaFileTxtText)// sql = select * from jcsjk_databaseinfo where name= + strName + if (shGetRecordCount(sql) <= )
{ XmlDocument xmlDoc = new XmlDocument()xmlDocLoad(metaFileTxtText)XmlNodeList xnl = xmlDocGetElementsByTagName(xsschema)XmlNode xn = xnl[]//插入元数据标准到数据库中用参数才能插入因为content字段内容太大sql = insert into jcsjk_metastand (id name content org version) values( + idnamecontentorgversion)OracleParameter[] op = new OracleParameter[]op[] = new OracleParameter()op[]ParameterName = idop[]OracleType = OracleTypeNVarCharop[]Value = metaIDTxtTextop[] = new OracleParameter()op[]ParameterName = nameop[]OracleType = OracleTypeNVarChar
string strValue = xnChildNodes[]ChildNodes[]InnerText
strValue = strValueSubstring(strValueIndexOf() + )op[]Value = strValueop[] = new OracleParameter()op[]ParameterName = contentop[]OracleType = OracleTypeClobop[]Value = strContentstrValue = xnChildNodes[]ChildNodes[]InnerTextstrValue = strValueSubstring(strValueIndexOf() + )op[] = new OracleParameter()op[]ParameterName = orgop[]OracleType = OracleTypeNVarCharop[]Value = strValuestrValue = xnChildNodes[]ChildNodes[]InnerTextstrValue = strValueSubstring(strValueIndexOf() + )op[] = new OracleParameter()op[]ParameterName = versionop[]OracleType = OracleTypeNVarCharop[]Value = strValuetry { shExecuteNonQuery(sql op)} catch (Exception ex)
{ LogHelpwriteLog(taUsername 元数据库管理写入元数据标准< + strName + >到数据库失败!)MessageBoxShow(写入元数据标准<< + strName + >到数据库失败!)} //从数据库以存储的标准创建else { string strStandName = comboBoxExSelectedItemToString()
sql = select content from jcsjk_metastand where name= + strStandName + OracleDataReader odr = shReturnDataReader(sql)if (odrRead())
{ StreamWriter bw = new StreamWriter(new FileStream(tempxsd FileModeCreate)SystemTextEncodingGetEncoding(GB))
bwWrite(odr[]ToString())
bwClose()dsReadXmlSchema(tempxsd)SystemIOFileDelete(tempxsd)try { OracleDataSchemaAdapter odsa = new OracleDataSchemaAdapter()odsaCreate(ds true databaseNameTxtText + _ SDE)} catch (ArgumentException ae)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(—— Error —— + aeMessage + —— Instructions ——)} catch (FileNotFoundException)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(File not found + metaFileTxtText)} catch (Exception)
{ LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >失败!)MessageBoxShow(创建元数据库< + databaseNameTxtText + >失败!)}
}
//插入元数据库信息到数据库sql = select * from jcsjk_databaseinfo where name= + databaseNameTxtText + if (shGetRecordCount(sql) <= )
{ htClear()htAdd(ID databaseIDTxtText)htAdd(NAME databaseNameTxtText)if (descriptionTextText != )
{ htAdd(DESCRIPTION descriptionTextText)} try { shInsert(jcsjk_databaseinfo ht)} catch (Exception)
{ LogHelpwriteLog(taUsername 元数据库管理插入元数据库< + databaseNameTxtText + >信息到数据库失败!)MessageBoxShow(插入元数据库<< + databaseNameTxtText + >信息到数据库失败!)}
LogHelpwriteLog(taUsername 元数据库管理创建元数据库< + databaseNameTxtText + >成功!)MessageBoxShow(创建元数据库< + databaseNameTxtText + >成功!)Close()}