【问题标题】:How to save data from local sdf database into text file?如何将本地 sdf 数据库中的数据保存到文本文件中?
【发布时间】:2013-12-25 15:35:49
【问题描述】:

是否有可能将本地 sdf 数据库中的数据保存到 C# 中的文本文件中?我不知道该怎么做,不幸的是我没有很多时间..数据库的每一行都必须在文本文件的新行中

【问题讨论】:

    标签: c# database text sdf


    【解决方案1】:

    您必须手动完成(即通过代码)。 MS SQL CE 不提供 SQL Server 版本所提供的导入/导出功能。

    【讨论】:

      【解决方案2】:

      这是来自我的一个旧项目,它可以工作。只需用您选择的名称更改表“tableProduit”的名称。如果你遇到问题问....

      using System;
      using System.Data.SqlServerCe;
      using System.Collections.Generic;
      using System.Text;
      using System.Data.SqlClient;
      using System.IO;
      using System.Reflection;
      using System.Windows.Forms;
      using System.Data;
      
      namespace app
      {
          class Connexion
          {
              SqlCeConnection conn;
              string connectionString;
              string chemin;
              public Connexion(string path,string password)
              {
                  this.chemin = path;
                  connectionString = string.Format("DataSource={0}", this.chemin + ";Password="+password);
                  conn = new SqlCeConnection(connectionString); 
              }
      
              public bool isConnected() 
              {
                  try
                  {
                      conn.Open();
                  }catch(SqlCeException e){
                      MessageBox.Show(e.ToString());
                      return false;
                  }
                  bool temp = false;
                  SqlCeDataReader dr;
                  SqlCeCommand cmd = new SqlCeCommand();
                  cmd.Connection = conn;
                  cmd.CommandText = "SELECT * FROM tableProduit";
                  dr = cmd.ExecuteReader();
                  if (dr.Read())
                  {
                      temp = true;
                  }
                  else
                  {
                      temp = false;
                  }
                  dr.Close();
                  conn.Close();
                  return temp;
              }
      
              public void writeData(string filepath,string filetype)
              {
                  conn.Open();
                  SqlCeDataReader dr;
                  SqlCeCommand cmd = new SqlCeCommand();
                  SqlCeDataAdapter adpt = new SqlCeDataAdapter();
                  cmd.Connection = conn;
                  cmd.CommandText = "SELECT * FROM tableProduit";
                  dr = cmd.ExecuteReader();
                  adpt.SelectCommand = cmd;
                  if (filetype == "txt")
                  {
                      TextWriter writer = new StreamWriter(filepath);
                      while (dr.Read())
                      {
                          writer.WriteLine(dr["codeBarre"] + ":" + dr["qte"]);
                      }
                      writer.Close();
                  }
                  else
                  {
                      //Create the data set and table
                      DataSet ds = new DataSet("New_DataSet");
                      DataTable dt = new DataTable("New_DataTable");
      
                      //Set the locale for each
                      ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
                      dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
                      adpt.Fill(dt);
                      ds.Tables.Add(dt);
                  }
                  dr.Close();
                  conn.Close();
              }
          }
      }
      

      编辑:忘记writer.WriteLine(dr["codeBarre"] + ":" + dr["qte"]);的文件,将它们更改为您的字段名称。祝您好运

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-04
        • 2021-08-18
        • 1970-01-01
        • 2017-01-30
        • 2015-10-23
        • 2010-12-18
        相关资源
        最近更新 更多