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.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
namespace 批量写入坐标
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection conn;
            string strSQL = "";
            conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"F:\research\空间可达性分析\沈阳数据\最终的poi数据\新建个人地理数据库.mdb");
            conn.Open();
            strSQL = "Select OBJECTID,Lon,Lat,Lon_1,Lat_1 from Export_Output";           
            if (strSQL != "")
            {
                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                cmd.ExecuteNonQuery();

               
                DataSet myds = new DataSet();
                OleDbDataAdapter oda = new OleDbDataAdapter(strSQL, conn);
                oda.Fill(myds);
                FileStream stream = new FileStream(Application.StartupPath + "\\20200212.txt", FileMode.Create);//fileMode指定是读取还是写入
                StreamWriter writer = new StreamWriter(stream);
                for (int intI = 0; intI < myds.Tables[0].Rows.Count; intI++)
                {
                                       writer.WriteLine(myds.Tables[0].Rows[intI][0].ToString()+"|"+ myds.Tables[intI].Rows[0][1].ToString()+"|"+ myds.Tables[0].Rows[intI][2].ToString()+"|"+ myds.Tables[0].Rows[intI][3].ToString()+"|"+ myds.Tables[0].Rows[intI][4].ToString());//写入一行,写完后会自动换行

                }
                writer.Close();//释放内存
                stream.Close();//释放内存
            }
            conn.Close();
            this.Close();
        }
    }
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值