是否有可能将本地sdf数据库中的数据保存到C#中的文本文件中?我不知道怎么做,不幸的是我没有太多的时间..数据库的每一行都必须在新的文本文件行中
答案 0 :(得分:0)
您必须手动(即通过代码)。 MS SQL CE不提供SQL Server版本所执行的导入/导出功能。
答案 1 :(得分: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"]);
将其更改为您的字段名称。运气好