⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 converter.cs

📁 可以实现任意数据库之间的数据互导,可以实现任意数据库之间的数据互导
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using LC_XMLControl;
using System.Collections;
using System.Data.OracleClient;
using System.Windows.Forms;
using System.Threading;

namespace XmlToOracel
{
    public class Converter : IThransProtocol
    {
        #region IThransProtocol 成员
        private ArrayList _allres = null;
        public System.Collections.ArrayList allres
        {
            get
            {
                return _allres;
            }
            set
            {
                _allres = (ArrayList)value;
            }
        }

        public void closeDesConnection()
        {
            
        }

        public void closeSourceConnection()
        {
            
        }

        public void createDesContentction(string constr)
        {
            _desconnection = null;
            if (_desconnection != null) _desconnection.Close();
            string[] temp = constr.Split('#');
            string cstr = "";
            int k = 0;
            if (temp.Length > 0)
            {
                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i].Trim().ToString() != "")
                    {

                        temp[k++] = temp[i];
                    }
                }
            }
            cstr = "server=" + temp[0] + ";user=" + temp[2] + ";password=" + temp[3];
            try
            {
                _desconnection = new OracleConnection(cstr);
                _desconnection.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }

        public void createSourceContenction(string constr)
        {
            
        }
        private int _cursum;
        public int cursum
        {
            get
            {
                return _cursum;
            }
            set
            {
                _cursum = value;
            }
        }
        private OracleConnection _desconnection;
        public object desconnection
        {
            get
            {
                return _desconnection;
            }
            set
            {
                _desconnection = (OracleConnection)value;
            }
        }
        private int _dtype;
        public int dtype
        {
            get
            {
                return _dtype;
            }
            set
            {
                _dtype = value;
            }
        }
        private ArrayList _errorres = null;
        public System.Collections.ArrayList errorres
        {
            get
            {
                return _errorres;
            }
            set
            {
                _errorres = (ArrayList)value;
            }
        }

        public System.Collections.ArrayList getAllDesField(string table)
        {
            ArrayList altemp = new ArrayList();
            OracleCommand cmd = new OracleCommand("select   *   from   user_tab_columns where TABLE_NAME=('" + table + "')");
            cmd.Connection = _desconnection;
            OracleDataReader sdr = cmd.ExecuteReader();
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    altemp.Add(sdr["COLUMN_NAME"].ToString());
                }
            }
            sdr.Close();
            return altemp;

        }

        public System.Collections.ArrayList getAllDesTable()
        {
            ArrayList altemp = new ArrayList();
            OracleCommand cmd = new OracleCommand("select * from user_tables");
            cmd.Connection = _desconnection;
            OracleDataReader sdr = cmd.ExecuteReader();
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    altemp.Add(sdr["TABLE_NAME"].ToString());
                }
            }
            sdr.Close();
            return altemp;
        }

        public System.Collections.ArrayList getAllSourceField(string table)
        {
            return null;
        }

        public System.Collections.ArrayList getAllSourceTable()
        {
            return null;
        }
        private string _info;
        public string info
        {
            get
            {
                return _info;
            }
            set
            {
                _info = value;
            }
        }
        private ArrayList _msg = null;
        public System.Collections.ArrayList msg
        {
            get
            {
                return _msg;
            }
            set
            {
                _msg = (ArrayList)value;
            }
        }

        public System.Data.DataTable sourceToXml(string source, System.Collections.ArrayList allfield)
        {
            return null;
        }
        private object _sourceconnection = null;
        public object sourceconnection
        {
            get
            {
                return _sourceconnection;
            }
            set
            {
                _sourceconnection = (Object)value;
            }
        }
        private string _spliter;
        public string spliter
        {
            get
            {
                return _spliter;
            }
            set
            {
                _spliter = value;
            }
        }
        private int _stype;
        public int stype
        {
            get
            {
                return _stype;
            }
            set
            {
                _stype = value;
            }
        }

        public void xmlToDes(string des, System.Collections.ArrayList rows, System.Collections.ArrayList field)
        {
            _allres.Clear();
            _errorres.Clear();
            _msg.Clear();
            OracleCommand ocmd = new OracleCommand();
            ocmd.Connection = _desconnection;
            //[1] = "type=数字■id=1133|type=字符串■pwd=10124160|type=字符串■username=Hewitt|"
            string temp;
            string[] fields;
            string str = "";
            string f = " ";
            f = " (";
            for (int i = 0; i < field.Count; i++)
            {

                f += field[i].ToString() + " , ";

            }
            f = f.Substring(0, f.Length - 2);
            f += ") ";

            for (int i = 0; i < rows.Count; i++)
            {
                str = "insert  into " + des + f + "values(";
                temp = rows[i].ToString();
                fields = temp.Split('|');
                //
                for (int k = 0; k < fields.Length; k++)
                {
                    if (fields[k].Trim() != "")
                    {
                        str += getFieldValue(fields[k]) + ",";
                    }
                }
                str = str.Substring(0, str.Length - 1);
                str += ")";
                try
                {
                    _cursum = i + 1;
                    ocmd.CommandText = str;
                    ocmd.ExecuteNonQuery();
                    info = _cursum + "  " + DateTime.Now.ToString() + " :    " + str + "被成功执行!";
                    _allres.Add(info);
                    Thread.Sleep(100);

                }
                catch (Exception ee)
                {
                    info = _cursum + "  " + DateTime.Now.ToString() + " :    " + "在执行" + str + "时候发生了  " + ee.Message.ToString();
                    _allres.Add(info);
                    _errorres.Add(info);
                    continue;
                }

            }

        }

        private string getFieldValue(string str)
        {
            string type = str.Substring(str.IndexOf('=') + 1, str.IndexOf('■') - 5);

            string value = str.Substring(str.LastIndexOf('=') + 1);
            if (type == "字符串")
                return "'" + value + "'";
            if (type == "日期")
            {
                try
                {
                    Convert.ToDateTime(value);
                    return "'" + value + "'";
                }
                catch (Exception e)
                {
                    return "'2000-01-01'";
                }
            }
            return value;
        }



        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -