将SQL表转储到.csv C#

时间:2016-04-19 13:15:44

标签: c# sql .net sql-server csv

我正在尝试在我的应用程序中实现一个脚本,该脚本将转储整个内容(现在,但我正在尝试编写代码以便我可以轻松地自定义它以仅抓取某些列)的sql db(运行ms sql server express 2014)到.csv文件。

这是我目前编写的代码:

        public void doCsvWrite(string timeStamp){       
        try {
            //specify file name of log file (csv).
            string newFileName = "C:/TestDirectory/DataExport-" + timeStamp + ".csv";
            //check to see if file exists, if not create an empty file with the specified file name.
            if (!File.Exists(newFileName)) {
                FileStream fs = new FileStream(newFileName, FileMode.CreateNew);
                fs.Close();
                //define header of new file, and write header to file.
                string csvHeader = "ITEM1,ITEM2,ITEM3,ITEM4,ITEM5";
                using (FileStream fsWHT = new FileStream(newFileName, FileMode.Append, FileAccess.Write))
                using(StreamWriter swT = new StreamWriter(fsWHT))
                {
                    swT.WriteLine(csvHeader.ToString());
                }
            }
            //set up connection to database.
            SqlConnection myDEConnection;   
            String cDEString = "Data Source=localhost\\NAMEDPIPE;Initial Catalog=db;User Id=user;Password=pwd";
            String strDEStatement = "SELECT * FROM table"; 

            try
            {
                myDEConnection = new SqlConnection(cDEString);
            }
            catch (Exception ex)
            {  
                //error handling here.
                return;
            }

            try
            {
                myDEConnection.Open();
            }
            catch (Exception ex)
            {
                //error handling here.
                return;
            }
            SqlDataReader reader = null;
            SqlCommand myDECommand = new SqlCommand(strDEStatement, myDEConnection);
            try
            {
                reader = myDECommand.ExecuteReader();
                while (reader.Read())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        if(reader["Column1"].ToString() == "") {
                            //does nothing if the current line is "bugged" (containing no values at all, typically happens after reboot of 3rd party equipment).
                        }
                        else {
                            //grab relevant tag data and set the csv line for the current row.
                            string csvDetails = reader["Column1"] + "," + reader["Column2"] + "," + String.Format("{0:0.0}", reader["Column3"]) + "," + String.Format("{0:0.000}", reader["Column4"]) + "," + reader["Column5"];

                            using (FileStream fsWDT = new FileStream(newFileName, FileMode.Append, FileAccess.Write))
                            using(StreamWriter swDT = new StreamWriter(fsWDT))
                            {
                                //write csv line to file.
                                swDT.WriteLine(csvDetails.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //error handling here.
                myDEConnection.Close();
                return;
            }
            myDEConnection.Close();
        }
        catch (Exception ex)
        {
            //error handling here.
            MessageBox.Show(ex.Message);
        }
    }

现在,当我将它与第三方基于SQLite的数据库一起使用时,这工作正常,但是我将这个修改后的输出改为我的MSSQL数据库看起来像这样(ITEM1是主键,标准自动递增ID字段):

ITEM1,ITEM2,ITEM3,ITEM4,ITEM5
1,row1_item2,row1_item3,row1_item4,row1_item5
1,row1_item2,row1_item3,row1_item4,row1_item5
1,row1_item2,row1_item3,row1_item4,row1_item5
1,row1_item2,row1_item3,row1_item4,row1_item5
1,row1_item2,row1_item3,row1_item4,row1_item5
1,row1_item2,row1_item3,row1_item4,row1_item5
2,row2_item2,row2_item3,row2_item4,row2_item5
2,row2_item2,row2_item3,row2_item4,row2_item5
2,row2_item2,row2_item3,row2_item4,row2_item5
2,row2_item2,row2_item3,row2_item4,row2_item5
2,row2_item2,row2_item3,row2_item4,row2_item5
3,row3_item2,row3_item3,row3_item4,row3_item5
3,row3_item2,row3_item3,row3_item4,row3_item5
3,row3_item2,row3_item3,row3_item4,row3_item5
3,row3_item2,row3_item3,row3_item4,row3_item5
....

所以它似乎写了同一行的几个条目,我想在每一行中只有一行。有什么建议吗?

提前致谢。

编辑:感谢大家的回答!

4 个答案:

答案 0 :(得分:2)

以下部分不需要for循环。因为它从0循环到FieldCount,我假设循环最初是为了将每列的文本附加在一起,但在循环内部有一行连接文本并将其分配给csvDetails。

        try
        {
            reader = myDECommand.ExecuteReader();
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    if(reader["Column1"].ToString() == "") {
                        //does nothing if the current line is "bugged" (containing no values at all, typically happens after reboot of 3rd party equipment).
                    }
                    else {
                        //grab relevant tag data and set the csv line for the current row.
                        string csvDetails = reader["Column1"] + "," + reader["Column2"] + "," + String.Format("{0:0.0}", reader["Column3"]) + "," + String.Format("{0:0.000}", reader["Column4"]) + "," + reader["Column5"];

                        using (FileStream fsWDT = new FileStream(newFileName, FileMode.Append, FileAccess.Write))
                        using(StreamWriter swDT = new StreamWriter(fsWDT))
                        {
                            //write csv line to file.
                            swDT.WriteLine(csvDetails.ToString());
                        }
                    }
                }
            }
        }

答案 1 :(得分:1)

你有一个循环遍及fieldcount的for循环。

for (int i = 0; i < reader.FieldCount; i++)

我认为如果你删除循环就行了,因为你不需要遍历列。

答案 2 :(得分:1)

通常,我们使用专门设计的导出/导入实用程序来转储数据。 但是,如果您必须实施自己的例程,我建议分解

private static IEnumerable<IDataRecord> SourceData(String sql) {
  using (SqlConnection con = new SqlConnection(ConnectionStringHere)) {
    con.Open();

    using (SqlCommand q = new SqlCommand(sql, con)) {
      using (var reader = q.ExecuteReader()) {
        while (reader.Read()) {
          //TODO: you may want to add additional conditions here

          yield return reader; 
        }
      }
    }
  }
}

private static IEnumerable<String> ToCsv(IEnumerable<IDataRecord> data) {
  foreach (IDataRecord record in data) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < record .FieldCount; ++i) {
      String chunk = Convert.ToString(record .GetValue(0));

      if (i > 0)
        sb.Append(','); 

      if (chunk.Contains(',') || chunk.Contains(';'))
        chunk = "\"" + chunk.Replace("\"", "\"\"") +  "\"";

      sb.Append(chunk);
    }

    yield return sb.ToString(); 
  } 
}

拥有SourceDataToCsv您可以轻松实现

private static void WriteMyCsv(String fileName) {
  var source = SourceData("SELECT * FROM table");

  File.WriteAllLines(fileName, ToCsv(source));
}

答案 3 :(得分:0)

这是因为输出放在for-loop

for (int i = 0; i < reader.FieldCount; i++)

并且每条记录都重复FieldCount-times