将 SQL 信息写入 TXT 文件 [英] Write SQL info to a TXT file

查看:50
本文介绍了将 SQL 信息写入 TXT 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中读取一行信息并将其写入 txt 文件.我已经弄清楚了大部分,但我收到以下错误字段初始值设定项无法引用非静态字段、方法或属性‘reader_writer.filewriter.filePath’",我不知道为什么.有人可以解释一下我的问题吗?

Im trying to read a row of information from a DB and write that out to a txt file. I have most of it figured out, but I get the following error "A field initializer cannot reference the non-static field, method, or property 'reader_writer.filewriter.filePath'" and I dont know why. Can someone please explain my problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data.Common;

namespace reader_writer
{
public class filewriter
{

    //public string filePath = "";
    bool fileExists = false;
    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string dbFile = filePath + @"\sqlfile.txt";

    public void Main(string[] args)
    {


        fileExists = File.Exists(dbFile);

        if (fileExists)
        {
            writeFileFromDB();
        }

        else
        {
            File.Create(dbFile);
            writeFileFromDB();
        }

    }


    public void writeFileFromDB()
    {
        //create connection
        SqlCommand comm = new SqlCommand();
        comm.Connection = new SqlConnection(@"MY DB CONNECTION STRING");
        String sql = @"SELECT ROW1, ROW2
                           FROM Export.TABLENAME";

        comm.CommandText = sql;
        comm.Connection.Open();

        SqlDataReader sqlReader = comm.ExecuteReader();

        while (sqlReader.Read())
        {
            StreamWriter writer = File.CreateText(dbFile);
            writer.WriteLine(sqlReader["ROW1"] + "\t" + sqlReader["ROW2"]);
            writer.Close();
        }

        sqlReader.Close();
        comm.Connection.Close();
    }
}

}

推荐答案

这是一个既能正常工作又能稍微清理一下的版本.它摆脱了导致您的问题的更广泛范围的变量.它使用一种写入文件的方式,这样您就不必检测它是否已经存在.它将您的 ROW1 到 ROW2 重命名为列,这就是它们的实际含义.这样就不必每次写一行时都打开/关闭文件.

Here's a version that works as well as cleans it up a bit. It gets away from the wider scope variables that were causing your problem. It uses a way to write to the file that makes it so you don't have to detect if it exists already. It renames your ROW1 to ROW2 to columns which is what they actually are. And it makes it so it doesn't have to open/close the file every time you write a row.

   static void Main(string[] args)
    {
        string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string dbFile = filePath + @"\sqlfile.txt";

        writeFileFromDB(dbFile);
    }

    public static void writeFileFromDB(string dbFile)
    {
        //create connection
        SqlCommand comm = new SqlCommand();
        comm.Connection = new SqlConnection(@"MY DB CONNECTION STRING");
        String sql = @"SELECT COLUMN1, COLUMN2
                       FROM Export.TABLENAME";

        comm.CommandText = sql;
        comm.Connection.Open();

        SqlDataReader sqlReader = comm.ExecuteReader();

        // Open the file for write operations.  If exists, it will overwrite due to the "false" parameter
        using (StreamWriter file = new StreamWriter(dbFile, false))
        {
            while (sqlReader.Read())
            {
                file.WriteLine(sqlReader["COLUMN1"] + "\t" + sqlReader["COLUMN2"]);
            }
        }

        sqlReader.Close();
        comm.Connection.Close();
    }

这篇关于将 SQL 信息写入 TXT 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆