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

📄 converter.cs

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

namespace XmlToCvs
{
    public class Converter : IThransProtocol
    {

        #region IThransProtocol 成员
        private string filname, dir;
        private DataTable csvtable = new DataTable();
        public void closeDesConnection()
        {
            
        }

        public void closeSourceConnection()
        {
            
        }

        public void createDesContentction(string constr)
        {
            _desconnection = null;
            if (_desconnection != null) _desconnection.Close();
            dir = constr.Substring(0, constr.LastIndexOf("\\"));
            filname = constr.Substring(constr.LastIndexOf("\\") + 1);
            filname = filname.Substring(0, filname.LastIndexOf("."));
            //MessageBox.Show(dir + "---" + filname);
            try
            {
                string str = "Driver={Microsoft Text Driver (*.txt; *.csv)};" + "Dbq=" + dir + ";Persist SecurityInfo=false;";
                _desconnection = new OdbcConnection(str);
                _desconnection.Open();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
        }

        public void createSourceContenction(string constr)
        {
            
        }
        private int _cursum;
        public int cursum
        {
            get
            {
                return _cursum;
            }
            set
            {
                _cursum = value;
            }
        }
        private OdbcConnection _desconnection;
        public object desconnection
        {
            get
            {
                return _desconnection;
            }
            set
            {
                _desconnection = (OdbcConnection)value;
            }
        }
        private int _dtype;
        public int dtype
        {
            get
            {
                return _dtype;
            }
            set
            {
                _dtype = value;
            }
        }

        public System.Collections.ArrayList getAllDesField(string table)
        {
            ArrayList temp = new ArrayList();
            OdbcCommand ocd = new OdbcCommand("select * from [" + table + ".csv]");
            ocd.Connection = _desconnection;
            OdbcDataAdapter oda = new OdbcDataAdapter(ocd);
            oda.Fill(this.csvtable);

            foreach (DataColumn column in csvtable.Columns)
            {
                temp.Add(column.ColumnName);
            }
            return temp;
        }

        public System.Collections.ArrayList getAllDesTable()
        {
            ArrayList temp = new ArrayList();
            temp.Add(filname);
            return temp;
        }

        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 = new ArrayList();
        public System.Collections.ArrayList msg
        {
            get
            {
                return _msg;
            }
            set
            {
                _msg = value;
            }
        }

        public System.Data.DataTable sourceToXml(string source, System.Collections.ArrayList allfield)
        {
            return null;
        }
        private object _sourceconnection;
        public object sourceconnection
        {
            get
            {
                return _sourceconnection;
            }
            set
            {
                _sourceconnection = 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;
            }
        }
        private ArrayList _allres = new ArrayList();
        public ArrayList allres
        {
            get
            {
                return _allres;
            }
            set
            {
                _allres = (ArrayList)value;
            }
        }
        private ArrayList _errorres = new ArrayList();
        public ArrayList errorres
        {
            get
            {
                return _errorres;
            }
            set
            {
                _errorres = (ArrayList)value;
            }
        }
        public void xmlToDes(string des, System.Collections.ArrayList rows, System.Collections.ArrayList field)
        {
            _allres.Clear();
            _errorres.Clear();
            OdbcCommand ocmd = new OdbcCommand();
            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+".csv" + 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 + "'";
            return value;
        }


        #endregion
    }
}

⌨️ 快捷键说明

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