所謂元數據就是描述數據的數據元數據庫就是存放描述數據的數據元數據主要用於描述數據的特征等的數據在很多場合下都會遇到元數據例如文件系統裡面有這裡介紹的元數據主要是指描述空間數據特征的元數據例如屬於哪一類數據等
今天由於時間關系就簡單介紹一下元數據庫管理的實現
初始化顯示元數據庫信息的頭部信息
[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()}
From:http://tw.wingwit.com/Article/program/ASP/201311/21847.html