0

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

4

2 回答 2

0

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

于 2013-12-25T16:06:31.390 回答
0

这是来自我的一个旧项目,它可以工作。只需用您选择的名称更改表“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"]);文件将它们更改为您的字段名称。祝你好运

于 2013-12-25T16:25:22.123 回答