zoukankan      html  css  js  c++  java
  • 基于ArcGIS10.0和Oracle10g的空间数据管理平台十四(C#开发)元数据库库管理


    我的新浪微博:http://weibo.com/freshairbrucewoo

    欢迎大家相互交流,共同提高技术。

        由于前段时间工作很忙而且出差去了北京一个多月,所以很久没有介绍了关于基于ArcGIS10.0和Oracle10g的空间数据管理平台这个项目的相关功能实现了,今天开始介绍一个新的功能实现,就是元数据库的管理。

        所谓元数据就是描述数据的数据,元数据库就是存放描述数据的数据。元数据主要用于描述数据的特征等的数据,在很多场合下都会遇到元数据,例如文件系统里面有。这里介绍的元数据主要是指描述空间数据特征的元数据,例如属于哪一类数据等。

        今天由于时间关系就简单介绍一下元数据库管理的实现。

    1.初始化显示元数据库信息的头部信息

            /// <summary>
            /// 初始化datagridView的头部显示信息
            /// </summary>
            private void InitDataGridView()
            {
                //dataGridViewX1.Rows.Clear();
                SqlHelper sh = new SqlHelper();
                string sql = "select id,name,description from jcsjk_databaseinfo";
                DataSet ds = sh.ReturnDataSet(sql, "jcsjk_databaseinfo");
                dataGridViewX1.DataSource = ds.Tables[0];
    
                dataGridViewX1.Columns[0].HeaderText = "元数据库ID";
                dataGridViewX1.Columns[0].Width = 200;
                dataGridViewX1.Columns[1].HeaderText = "元数据库名称";
                dataGridViewX1.Columns[1].Width = 200;
    
                dataGridViewX1.Columns[2].HeaderText = "元数据库描述信息";
                dataGridViewX1.Columns[2].Width = 200;
    
            }

    2.删除元数据库

           /// <summary>
            /// 删除元数据库
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void delMetaDatabaseBtn_Click(object sender, EventArgs e)
            {
                //1.删除所有表
                if (dataGridViewX1.CurrentRow.Index < 0)
                {
                    MessageBox.Show("请选择需要删除的数据库那一行");
                    return;
                }
                string strDatabaseName = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].Cells[1].Value.ToString();
                SqlHelper sh = new SqlHelper();
                string sql = "select table_name from user_tables where table_name like '" +
                    strDatabaseName.ToUpper() + "/_%' ESCAPE '/'";
                OracleDataReader odr = sh.ReturnDataReader(sql);
    
                while (odr.Read())
                {
                    //删除关联外键
                    string strTemp = odr[0].ToString().ToUpper();
                    strTemp = strTemp.Substring(strDatabaseName.Length + 1);
                    sql = "select table_name,constraint_name from user_constraints where constraint_type='R'"
                        + " and constraint_name like '" + strTemp + "%'";
                    OracleDataReader odr1 = sh.ReturnDataReader(sql);
    
                    while (odr1.Read())
                    {
                        sql = "ALTER TABLE " + odr1[0].ToString().ToUpper() + " DROP CONSTRAINT "
                            + odr1[1].ToString().ToUpper();
                        sh.ExecuteSQL(sql);
                    }
                    sql = "drop table " + odr[0].ToString().ToUpper();
                    sh.ExecuteSQL(sql);
                }
                //2.删除记录
                Hashtable ht = new Hashtable();
                ht.Add("name", strDatabaseName);
                try
                {
                    sh.Del("jcsjk_databaseinfo", "name='" + strDatabaseName + "'", ht);
                }
                catch (Exception)
                {
                    LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                    "删除元数据库<" + strDatabaseName + ">失败!");
                    MessageBox.Show("删除元数据库<" + strDatabaseName + ">失败!");
                }
    
                LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                    "删除元数据库<" + strDatabaseName + ">成功!");
                MessageBox.Show("删除元数据库<" + strDatabaseName + ">成功!");
            }


     

    3.清空元数据库

           /// <summary>
            /// 清空元数据库中的数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void clearMetaDataBtn_Click(object sender, EventArgs e)
            {
                if (dataGridViewX1.CurrentRow.Index < 0)
                {
                    MessageBox.Show("选择数据库!");
                    return;
                }
                SqlHelper sh = new SqlHelper();
                string strDatabaseName = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
                //NewDataSet nds = new NewDataSet();
                string sql = string.Empty;
                sql = "select table_name from user_tables where table_name like '" +
                    strDatabaseName.ToUpper() + "/_%' ESCAPE '/'";
                OracleDataReader odr = sh.ReturnDataReader(sql);
    
                while (odr.Read())
                {
                    sql = "delete from " + odr[0].ToString().ToUpper();
                    try
                    {
                        sh.ExecuteSQL(sql);
                    }
                    catch (Exception)
                    {
                        LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "清空元数据库<" + strDatabaseName + ">的数据失败!");
                        MessageBox.Show("清空元数据库<" + strDatabaseName + ">的数据失败!");
                        return;
                    }
                }
                LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                   "清空元数据库<" + strDatabaseName + ">的数据成功!");
                MessageBox.Show("清空元数据库<" + strDatabaseName + ">的数据成功!");
                /*
                foreach (DataTable dt in nds.Tables)
                {
                        string strTableName = strDatabaseName + "_" + dt.TableName;
                        sql = "select * from " + strTableName;
                        DataSet ds = new DataSet();
    
                        OracleDataAdapter oda = new OracleDataAdapter(sql,
                            ConfigurationSettings.AppSettings["ConnectionString"]);
                        try
                        {
                            oda.Fill(ds);
                            oda.Update(dt);
                        }
                        catch (Exception ex)
                        {
                            LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "清空元数据库<" + strDatabaseName + ">的数据失败!");
                            MessageBox.Show("清空元数据库<" + strDatabaseName + ">的数据失败!");
                        }
                }
                LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                   "清空元数据库<" + strDatabaseName + ">的数据成功!");
                MessageBox.Show("清空元数据库<" + strDatabaseName + ">的数据成功!");
                 * */
            }


        清空元数据库与删除元数据库是不同的功能,删除元数据库就是把存放元数据的库都一起删除了,但是清空元数据库只是把库里面的内容删除,库本身存在。

    4.创建元数据库

        这是比较复杂的一个功能,因为创建一个元数据库需要根据一定的标准来创建,这些标准都是通过xsd文件描述的,所以首先要解析这个xml的模式描述文件,然后根据解析的内容存放到相应的字段中去,具体实现如下:

            private void createBtn_Click(object sender, EventArgs e)
            {
                SqlHelper sh = new SqlHelper();
                string sql = string.Empty;
    
                if (databaseIDTxt.Text.Trim() == "" || databaseNameTxt.Text.Trim() == "")
                {
                    errTxt.Text = "数据库ID或名称不能为空.";
                    return;
                }
    
                //查看元数据库是否存在
                sql = "select * from jcsjk_databaseinfo where name='" + databaseNameTxt.Text + "'";
    
                if (sh.GetRecordCount(sql) > 0)
                {
                    MessageBox.Show("此元数据库已经存在,请从新命名元数据库!");
                    return;
                }
    
                Hashtable ht = new Hashtable();
                string strContent = string.Empty;
                DataSet ds = new DataSet();
                //从文件标准创建
                if (!checkBoxX1.Checked)
                {
                    if (metaFileTxt.Text == "" || Path.GetExtension(metaFileTxt.Text).ToLower() != ".xsd")
                    {
                        errTxt.Text = "请选择正确的XSD文件.";
                        return;
                    }
                    if (metaIDTxt.Text.Trim() == "")
                    {
                        errTxt.Text = "元数据标准ID不能为空.";
                        return;
                    }
    
                    //1.读入xsd文件
                    ds.ReadXmlSchema(metaFileTxt.Text);
                    
                    //2.create table in tablespace
                    try
                    {
                        OracleDataSchemaAdapter odsa = new OracleDataSchemaAdapter();
                        odsa.Create(ds, true, databaseNameTxt.Text + "_", "SDE");
                    }
                    catch (ArgumentException ae)
                    {
                        LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                        MessageBox.Show("-- Error --" + ae.Message + "-- Instructions --");
                    }
                    catch (FileNotFoundException)
                    {
                        LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                        MessageBox.Show("File not found: " + metaFileTxt.Text);
                    }
                    catch (Exception)
                    {
                        LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                        MessageBox.Show("创建元数据库<" + databaseNameTxt.Text + ">失败!");
                    }
    
                    //写入元数据标准到数据库
                    StreamReader sr = new StreamReader(new FileStream(metaFileTxt.Text, FileMode.Open),
                        System.Text.Encoding.GetEncoding("GB2312"));
    
                    while (!sr.EndOfStream)
                    {
                        strContent += sr.ReadLine();
                    }
                    sr.Close();
                    string strName = Path.GetFileNameWithoutExtension(metaFileTxt.Text);
                    //
                    sql = "select * from jcsjk_databaseinfo where name='" + strName + "'";
                    if (sh.GetRecordCount(sql) <= 0)
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(metaFileTxt.Text);
                        XmlNodeList xnl = xmlDoc.GetElementsByTagName("xs:schema");
                        XmlNode xn = xnl[0];
                        //插入元数据标准到数据库中,用参数才能插入,因为content字段内容太大
                        sql = "insert into jcsjk_metastand (id, name, content, org, version) values("
                            + ":id,:name,:content,:org,:version)";
                        OracleParameter[] op = new OracleParameter[5];
                        op[0] = new OracleParameter();
                        op[0].ParameterName = "id";
                        op[0].OracleType = OracleType.NVarChar;
                        op[0].Value = metaIDTxt.Text;
                        op[1] = new OracleParameter();
                        op[1].ParameterName = "name";
                        op[1].OracleType = OracleType.NVarChar;
    
                        string strValue = xn.ChildNodes[0].ChildNodes[1].InnerText;
                        
                        strValue = strValue.Substring(strValue.IndexOf(':') + 1);
                        op[1].Value = strValue;
                        op[2] = new OracleParameter();
                        op[2].ParameterName = "content";
                        op[2].OracleType = OracleType.Clob;
                        op[2].Value = strContent;
                        strValue = xn.ChildNodes[0].ChildNodes[2].InnerText;
                        strValue = strValue.Substring(strValue.IndexOf(':') + 1);
                        op[3] = new OracleParameter();
                        op[3].ParameterName = "org";
                        op[3].OracleType = OracleType.NVarChar;
                        op[3].Value = strValue;
                        strValue = xn.ChildNodes[0].ChildNodes[0].InnerText;
                        strValue = strValue.Substring(strValue.IndexOf(':') + 1);
                        op[4] = new OracleParameter();
                        op[4].ParameterName = "version";
                        op[4].OracleType = OracleType.NVarChar;
                        op[4].Value = strValue;
                        try
                        {
                            sh.ExecuteNonQuery(sql, op);
                        }
                        catch (Exception ex)
                        {
                            LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                "写入元数据标准<" + strName + ">到数据库失败!");
                            MessageBox.Show("写入元数据标准<<" + strName + ">到数据库失败!");
                        }
                    }
                }
                //从数据库以存储的标准创建
                else
                {
                    string strStandName = comboBoxEx1.SelectedItem.ToString();
    
                    sql = "select content from jcsjk_metastand where name='" + strStandName + "'";
                    OracleDataReader odr = sh.ReturnDataReader(sql);
                    if (odr.Read())
                    {
                        StreamWriter bw = new StreamWriter(new FileStream("temp.xsd", FileMode.Create),
                            System.Text.Encoding.GetEncoding("GB2312"));
    
                        bw.Write(odr[0].ToString());
    
                        bw.Close();
                        ds.ReadXmlSchema("temp.xsd");
                        System.IO.File.Delete("temp.xsd");
                        try
                        {
                            OracleDataSchemaAdapter odsa = new OracleDataSchemaAdapter();
                            odsa.Create(ds, true, databaseNameTxt.Text + "_", "SDE");
                        }
                        catch (ArgumentException ae)
                        {
                            LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                    "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                            MessageBox.Show("-- Error --" + ae.Message + "-- Instructions --");
                        }
                        catch (FileNotFoundException)
                        {
                            LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                    "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                            MessageBox.Show("File not found: " + metaFileTxt.Text);
                        }
                        catch (Exception)
                        {
                            LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                                    "创建元数据库<" + databaseNameTxt.Text + ">失败!");
                            MessageBox.Show("创建元数据库<" + databaseNameTxt.Text + ">失败!");
                        }
                    }
                    
                }
    
                //插入元数据库信息到数据库
                sql = "select * from jcsjk_databaseinfo where name='" + databaseNameTxt.Text + "'";
                if (sh.GetRecordCount(sql) <= 0)
                {
                    ht.Clear();
                    ht.Add("ID", databaseIDTxt.Text);
                    ht.Add("NAME", databaseNameTxt.Text);
                    if (descriptionText.Text != "")
                    {
                        ht.Add("DESCRIPTION", descriptionText.Text);
                    }
                    try
                    {
                        sh.Insert("jcsjk_databaseinfo", ht);
                    }
                    catch (Exception)
                    {
                        LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                            "插入元数据库<" + databaseNameTxt.Text + ">信息到数据库失败!");
                        MessageBox.Show("插入元数据库<<" + databaseNameTxt.Text + ">信息到数据库失败!");
                    }
                }
    
                LogHelp.writeLog(FrmMain.metaUsername, "元数据库管理",
                           "创建元数据库<" + databaseNameTxt.Text + ">成功!");
                MessageBox.Show("创建元数据库<" + databaseNameTxt.Text + ">成功!");
                Close();
            }


        到此元数据库的管理基本内容已经介绍完毕。

  • 相关阅读:
    JavaScript 随机产生十个整数,放入数组中,对这个数组进行降序排序,并获取到这个数组的最大值和最小值
    JavaScript输出换行
    JavaScript超时调用、间歇调用
    JavaScript内置对象
    JavaScript事件列表
    JavaScript 中 for-in和 for-of 的区别
    JavaScript break指定标签打破多层循环示例
    初识JavaScript
    HTML + CSS CSS设置背景图片后图片没有铺满屏幕等
    设计模式之工厂模式
  • 原文地址:https://www.cnblogs.com/brucewoo/p/3226863.html
Copyright © 2011-2022 走看看