将SQL信息写入TXT文件



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

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重命名为列,这就是它们的实际情况。它使它不必每次写一行都打开/关闭文件。

   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();
    }
string dbFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"sqlfile.txt";

为什么要定义filePath,然后只使用它来定义dbFile?

Main(string[] args)方法内部设置dbFile变量。它在类声明中不起作用。

如果不使用方法或使字段为静态,则不能引用实例字段。

请参阅MSDN参考资料:http://msdn.microsoft.com/en-us/library/5724t6za(v=vs.80).aspx

问题似乎是您的dbFile被声明为类的字段(实例变量)。但是Writer在初始化时没有被实例化。

当您声明类级变量并将其设置为非静态值时,就会出现您提到的错误。它们不能用于初始化另一个字段。就像你在中所做的那样

StreamWriter writer = File.CreateText(dbFile);

不能使用实例变量初始化另一个实例变量。编译器可以重新排列这些。

不能保证dbfile会在写入程序之前初始化。

将字段初始化保留为常数值或简单的新语句。

最新更新