2013-08-01 121 views
1

我試圖從數據庫讀取一行信息並將其寫入一個txt文件。我大部分都想通了,但我得到以下錯誤「字段初始值設定項不能引用非靜態字段,方法或屬性'reader_writer.filewriter.filePath'」,我不知道爲什麼。有人能解釋我的問題嗎?將SQL信息寫入TXT文件

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(); 
    } 
} 

}

+0

的可能重複的[A字段初始不能引用非靜態字段,方法或屬性?](http://stackoverflow.com/questions/7400677/a-field-initializer-cannot-reference-the-non -static場法-或屬性) –

回答

2

這裏有一個工程,以及清除它有點版本。它遠離了導致問題的更廣泛的範圍變量。它使用一種方法來寫入文件,使其不必檢測它是否已經存在。它將你的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(); 
    } 
0

設置Main(string[] args)方法內的dbFile變量。它在類聲明中不起作用。

1
string dbFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\sqlfile.txt"; 

有,爲什麼你只使用它來定義DBFILE定義文件路徑,然後一個原因是什麼?

0

問題似乎是你的dbFile被聲明爲你的類的字段(實例變量)。但是Writer在初始化時沒有實例化。

你提到的錯誤,當你聲明和類級別變量設置爲一個非靜態值時發生。它們不能用於初始化另一個字段。就像你在做的那樣

StreamWriter writer = File.CreateText(dbFile); 

你不能用一個實例變量來初始化另一個實例變量。編譯器可以重新排列這些。

沒有保證DBFILE作家之前將被初始化。

保留字段初始化爲恆定值或一個簡單的新聲明。