美文网首页
C# 将数据库的数据逐行读入txt文件

C# 将数据库的数据逐行读入txt文件

作者: Will_板凳 | 来源:发表于2020-02-19 19:39 被阅读0次

    Demo

    工作中用到的批量读取数据库写入TXT的小工具,与大家分享

    主函数

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using MySql.Data.MySqlClient;
    
    namespace ReadTest
    {
        class Program
        {
            private const string mysqlString =
                    "server=127.0.0.1;User Id=root;Persist Security Info=True;password=*******;database=***";
    
            private const string sqlGra = "select [字段],[字段] from [TableName]";
            public static void Main(string[] args)
            {
                var mysql = new MySqlConnection(mysqlString);
                try
                {
                    mysql.Open();
                    ReadExcuteResult(mysql, sqlGra);
    
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
    
    
            private static DbDataModel ReadExcuteResult(MySqlConnection connectionIn, string sqlGra)
            {
                try
                {
                    var adapter = new MySqlDataAdapter(sqlGra, connectionIn);
                    DataTable _dataTable = new DataTable();
                    var entities = new Dictionary<string[], object[]>();
                    adapter.Fill(_dataTable);
    
                    foreach (DataColumn dataColumn in _dataTable.Columns)
                    {
                        foreach (DataRow dataRow in _dataTable.Rows)
                        {
                            entities.Add(new[] { dataColumn.ColumnName }, new[] { dataRow[dataColumn.ColumnName] });
                        }
                    }
    
    
    
                    return null;
                }
                
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
    }
    
    

    辅助类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace ReadICDCode
    {
        public class DbDataModel
        {
            public DbDataModel()
            {
                DefaultName = "name";
                DefaultOperation = "operation";
                DefaultType = "type";
                Table = "Table";
                DbVersion = "version";
                TypeLength = "typelength";
                RefTableName = "tablename";
                ConstraintName = "constraintname";
                RefColumnName = "columnname";
                FieldContent = "content";
                Uid = "uid";
                xmlDoc = new XmlDocument();
            }
    
            public string Uid { get; set; }
            public string FieldContent { get; set; }
            public string RefTableName { get; set; }
            public string ConstraintName { get; set; }
            public string RefColumnName { get; set; }
            public string TypeLength { get; set; }
            public string DbVersion { get; set; }
            public string TableName { get; set; }
            public string Table { get; set; }
            public string ColumnName { get; set; }
            public string DefaultName { get; set; }
            public string Operation { get; set; }
            public string DefaultOperation { get; set; }
            public string Type { get; set; }
            public string DefaultType { get; set; }
            public string SqlGra { get; set; }
            public XmlDocument xmlDoc { get; set; }
            public XmlNode Root { get; set; }
            public XmlNodeList NodeList { get; set; }
            public XmlElement List { get; set; }
            public XmlNodeList SubList { get; set; }
        }
    
        public class DM_UpDb_AllTable
        {
            public DM_UpDb_AllTable()
            {
                AllTableLists = new List<DM_UpDb_SqlList>();
            }
    
            public List<DM_UpDb_SqlList> AllTableLists { get; set; }
    
            public class DM_UpDb_SqlList
            {
                public DM_UpDb_SqlList()
                {
                    AllSqlList = new List<SqlList>();
                }
    
                public List<SqlList> AllSqlList { get; set; }
                public string TableName { get; set; }
                public string SqlGra { get; set; }
                public string GetTableDataSql { get; set; }
                public string GetTableKeySql { get; set; }
    
                public class SqlList
                {
                    public SqlList()
                    {
                        Uid = new DM_UpDb_Table_All.TableModel.ContenObject();
                    }
    
                    public string ColumnName { get; set; }
                    public string Operation { get; set; }
                    public string Type { get; set; }
                    public string DbVersion { get; set; }
                    public string TypeLength { get; set; }
                    public string RefTableName { get; set; }
                    public string ConstraintName { get; set; }
                    public string RefColumnName { get; set; }
                    public object FieldContent { get; set; }
                    public DM_UpDb_Table_All.TableModel.ContenObject Uid { get; set; }
                }
            }
        }
    
    
    
        public class DM_UpDb_Table_All
        {
            public DM_UpDb_Table_All()
            {
                AllTableLists = new List<TableModel>();
            }
    
            public List<TableModel> AllTableLists { get; set; }
    
            public class TableModel
            {
                public TableModel()
                {
                    ContentObjects = new List<ContenObject>();
                    KeyObjects = new List<KeyObject>();
                }
    
                public string Column_Name { get; set; }
                public string Data_Type { get; set; }
                public string Type_Length { get; set; }
                public List<ContenObject> ContentObjects { get; set; }
                public List<KeyObject> KeyObjects { get; set; }
    
                public class ContenObject
                {
                    public object[] Content { get; set; }
                }
                public class KeyObject
                {
                    public object[] Key { get; set; }
                    public object[] Table_Name { get; set; }
                    public object[] Column_Name { get; set; }
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:C# 将数据库的数据逐行读入txt文件

          本文链接:https://www.haomeiwen.com/subject/ilkzfhtx.html