zoukankan      html  css  js  c++  java
  • C#中从数据库导出至txt

    用'|'隔开

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;

    namespace 导出数据库表至txt
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    using (FileStream fstream = File.OpenWrite(sfd.FileName))
                    {
                        StreamWriter sw = new StreamWriter(fstream, System.Text.Encoding.GetEncoding("GB2312"));
                        
                        using (SqlConnection conn = new SqlConnection("server=.;database=test;user id=sa;password=123456"))
                        {
                            conn.Open();
                         
                            using (SqlCommand cmd = conn.CreateCommand())
                            {
                                cmd.CommandText = "select * from T_persons";
                                using (SqlDataReader reader=cmd.ExecuteReader())
                                {
                                    while (reader.Read())
                                    {
                                        string name = reader.GetString(reader.GetOrdinal("name"));
                                        int age = reader.GetInt32(reader.GetOrdinal("age"));
                                        sw.WriteLine(name + "|" + age);

                                    }
                                    sw.Flush();
                                }

                            }
                        }
                        MessageBox.Show("数据导出成功!");
                    }
                }
            }

          
        }
    }

  • 相关阅读:
    no module name cx_oracle 的解决方法
    开通博客
    普通用户启动Hadoop格式化namenode出现无法创建目录的问题
    改写文件权限时出现问题___2
    suse添加普通用户赋予root所有权限时出现问题___1
    suse系统vim未正常退出产生的问题(can't write viminfo file /home/zhaoy/.viminfo)
    intellij idea根据mvn仓库添加或改变scala-sdk
    git拉项目和上传项目时遇到的一些问题
    简单的clone项目fromGitHub
    初始机器学习
  • 原文地址:https://www.cnblogs.com/agile2011/p/2058987.html
Copyright © 2011-2022 走看看