1

我有一个 Visual C# 2008 项目。在我的项目浏览器中,我有一个 SQL CE 数据库(sdf 文件),因此我可以在设计器中使用数据库和数据集。在运行时,sdf 文件被复制到输出目录。当用户保存自己的数据库时,此 sdf 文件将复制到用户选择的任何文件名,并将其数据集保存到副本中。到目前为止没有问题。

如果可能的话,我想以不同的方式做到这一点。我不想在程序首次运行时复制 sdf 文件,而是对其进行设置,以便当用户保存他们的数据库时,然后它使用他们选择的名称复制出 sdf 文件并将他们的数据集保存到其中.

我已经阅读了将文件写为二进制文件的方法,这确实有效,但它需要几行似乎不必要的代码。有没有办法只是告诉程序“嘿,继续快速复制这个 sdf 文件”?毕竟,该程序能够在您首次运行时自动执行此操作。

4

2 回答 2

1

将数据库文件添加为项目中的资源。然后,当您希望文件出现时,只需将字节数组写入文件即可。

例子:

File.WriteAllBytes("destination path", Properties.Resources.YourResourceName);
于 2012-09-01T00:59:31.967 回答
0

我喜欢与您分享我的知识堆栈用户您可以分析此代码找到可以提供帮助的东西

使用系统;

使用 System.Collections.Generic;

使用 System.ComponentModel;

使用 System.Data;

使用 System.Drawing;

使用 System.Linq;

使用 System.Text;

使用 System.Windows.Forms;

使用 System.Data.SqlServerCe;

使用 System.IO;

namespace localdatabaseconnect

{

public partial class Form1 : Form

{
    public Form1()
    {
        InitializeComponent();
    }
    string conString = Properties.Settings.Default.locallyConnectionString;
    private void button1_Click(object sender, EventArgs e)
    {


        // Open the connection using the connection string.
        using (SqlCeConnection con = new SqlCeConnection(conString))
        {
            con.Open();

            // Insert into the SqlCe table. ExecuteNonQuery is best for inserts.
            int id = int.Parse(textBox1.Text);
            string nom = textBox2.Text;
            string prenom = textBox3.Text;
            using (SqlCeCommand com = new SqlCeCommand("INSERT INTO testme (id,nom,prenom) VALUES(@id,@nom,@prenom)", con))
            {
                com.Parameters.AddWithValue("@id", id);
                com.Parameters.AddWithValue("@nom", nom);
                com.Parameters.AddWithValue("@prenom", prenom);
                com.ExecuteNonQuery();
            }
            con.Close();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        using (SqlCeConnection con = new SqlCeConnection(conString))
        {
            con.Open();
            // Read in all values in the table.
            using (SqlCeCommand com = new SqlCeCommand("SELECT * FROM testme", con))
            {
                SqlCeDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    string num = reader[0].ToString();
                    MessageBox.Show("hi"+num);
                }
            }
            con.Close();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {

        string path = "c:\\ChekatyResources";
     try{

      if (!Directory.Exists(path))
        {

      // Try to create the directory.
          DirectoryInfo di = Directory.CreateDirectory(path);
         MessageBox.Show("file is created");
         }
       else {

          MessageBox.Show("file is exist");

          }
          }
            catch (IOException ioex)
           {
  MessageBox.Show(ioex.Message);
           }

       }

    private void Form1_Load(object sender, EventArgs e)
    {
        //string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;

        //string path = (System.IO.Path.GetDirectoryName(executable));
        //MessageBox.Show(path);
        //File.WriteAllBytes("destination path",);
        string paths = "c:\\";
        AppDomain.CurrentDomain.SetData("DataDirectory", paths);

        string connStr = @"Data Source =c:\ChekatyResources\locally.sdf;";

        if (!File.Exists(@"c:\ChekatyResources\locally.sdf"))
        {

            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();

            SqlCeConnection conn = null;


            try
            {
                conn = new SqlCeConnection(connStr);
                conn.Open();

                SqlCeCommand cmd = conn.CreateCommand();
                cmd.CommandText = "CREATE TABLE testme(id int, prenom ntext,nom ntext)";
                cmd.ExecuteNonQuery();
            }
            catch
            {

            }
            finally
            {
                conn.Close();
            }
        }
        else { MessageBox.Show("it's exist"); }
    }
}

}

于 2014-11-06T22:12:26.340 回答