2017-02-28 42 views
0

我正在构建一个基于库存的应用程序,可以直接从任何pc运行而无需任何安装,因此我使用SQL CE ..在开始时,我检查数据库是否存在。如果不是我正在创建新数据库:SQL CE不更新Windows窗体中的数据库值

 try 
     { 
      string conString = "Data Source='ITM.sdf';LCID=1033;Password=ABCDEF; Encrypt = TRUE;"; 
      SqlCeEngine engine = new SqlCeEngine(conString); 
      engine.CreateDatabase(); 
      return true; 

     } 
     catch (Exception e) 
     { 
      //MessageBox.Show("Database Already Present"); 
     } 

该数据库是正确创建的,我可以访问数据库中创建表以及..我面对的问题是 - 我在窗口形式插入和更新记录按钮点击使用代码:

  using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring)) 
     { 
      connection.Open(); 
      String Name = NameTxt.Text.ToString(); 
      String Phone = PhoneTxt.Text.ToString(); 
      double balance = Double.Parse(BalanceTxt.Text.ToString()); 
      String City = CityTxt.Text.ToString(); 
      string sqlquery = "INSERT INTO Customers (Name,Phone,Balance,City)" + "Values(@name,@phone, @bal, @city)"; 
      SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection); 
      cmd.Parameters.AddWithValue("@name", Name); 
      cmd.Parameters.AddWithValue("@phone", Phone); 
      cmd.Parameters.AddWithValue("@bal", balance); 
      cmd.Parameters.AddWithValue("@city", City); 
      int x = cmd.ExecuteNonQuery(); 
      if (x > 0) 
      { 
       MessageBox.Show("Data Inserted"); 
      } 
      else 
      { 
       MessageBox.Show("Error Occured. Cannot Insert the data"); 
      } 
      connection.Close(); 
     } 

和更新用代码是

 using (SqlCeConnection connection = new SqlCeConnection(DatabaseConnection.connectionstring)) 
     { 
      connection.Open(); 
      int idtoedit = select_id_edit; 
      String Name = NameEditTxt.Text.ToString(); 
      String Phone = metroTextBox1.Text.ToString(); 
      String City = CityEditTxt.Text.ToString(); 
      string sqlquery = "Update Customers Set Name = @name, Phone = @phone,City = @city where Id = @id"; 
      SqlCeCommand cmd = new SqlCeCommand(sqlquery, connection); 

      cmd.Parameters.AddWithValue("@name", Name); 
      cmd.Parameters.AddWithValue("@phone", Phone); 
      cmd.Parameters.AddWithValue("@id", idtoedit); 
      cmd.Parameters.AddWithValue("@city", City); 
      int x = cmd.ExecuteNonQuery(); 
      if (x > 0) 
      { 
       MessageBox.Show("Data Updated"); 
      } 
      else 
      { 
       MessageBox.Show("Error Occured. Cannot Insert the data"); 
      } 
      loadIntoGrid(); 
      connection.Close(); 
     } 

每当我执行代码插入和更新记录 - 记录反映在数据网格从数据库表中填充适配器。但是,一旦我重新启动应用程序,值不会出现在数据库中。一旦进入百万,价值就会反映在数据库中。我无法理解这个问题背后的原因。

我已经下文称这些文章:

C# - ExecuteNonQuery() isn't working with SQL Server CE

Insert, Update, Delete are not applied on SQL Server CE database file for Windows Mobile

但由于我编程方式创建数据库 - 它是越来越直接创建于斌/调试目录,我看不到它在溶液中用于更改复制选项的视觉工作室中的资源管理器

+0

只是为了澄清是更新和插入查询运行**在同一时间**当if(x> 0)'? –

+0

不..他们正在单独运行 –

回答

1

您可能会用项目中的空白副本重写数据库文件。

参见this answer。您不应将数据库存储在bin文件夹中,而应根据您的需要在AppData或其他文件夹中找到用户或公共配置文件中的某个位置。

连接字符串应该是这样的:

<connectionStrings> 
    <add name="ITMContext" connectionString="data source=|DataDirectory|\ITM.sdf';LCID=1033;Password=ABCDEF; Encrypt = TRUE;" providerName="System.Data.SqlServerCe.4.0"> 

您shuld在应用程序的的Starup部署与您的自定义代码运行你的数据库文件到选择的位置,请参阅本ErikEJ博客:http://erikej.blogspot.cz/2013/11/entity-framework-6-sql-server-compact-4_25.html

private const string dbFileName = "Chinook.sdf"; 

private static void CreateIfNotExists(string fileName) 
{ 
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); 
    // Set the data directory to the users %AppData% folder    
    // So the database file will be placed in: C:\\Users\\<Username>\\AppData\\Roaming\\    
    AppDomain.CurrentDomain.SetData("DataDirectory", path); 

    // Enure that the database file is present 
    if (!System.IO.File.Exists(System.IO.Path.Combine(path, fileName))) 
    { 
     //Get path to our .exe, which also has a copy of the database file 
     var exePath = System.IO.Path.GetDirectoryName(
      new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath); 
     //Copy the file from the .exe location to the %AppData% folder 
     System.IO.File.Copy(
      System.IO.Path.Combine(exePath, fileName), 
      System.IO.Path.Combine(path, fileName)); 
    } 
} 

也请查看this post

您有几个选项可以更改此行为。如果您的sdf文件是 项目内容的一部分,这将影响数据如何持续存在 。请记住,当您调试时,如果在bin/debug文件夹中,您的项目 (包括sdf)的所有输出。

  • 您可以决定不包含sdf文件作为项目的一部分并管理文件位置运行时。

  • 如果您使用“复制如果更新”,并且您对数据库所做的项目更改将覆盖任何运行时/调试更改。

  • 如果您使用“不要复制”,则必须在代码中指定位置(如程序运行所在的上方两个级别)。

  • 如果你已经“始终复制”,在运行时所做的任何修改都会被覆盖

项目中的数据库文件调试与否时可以是不同的。还请检查this answer。您可能正在将您的记录写入数据库文件的另一个副本。

+0

仍然无法正常工作..根据您的建议。如果数据库存在于appdata文件夹中,我检查了应用程序的开始。如果是的话 - 那么我进行插入和更新语句。如果没有 - 我首先在bin文件夹中创建数据库,然后移动到appdata文件夹。更新和插入语句的连接字符串是:public static string connectionstring =“Data Source ='| DataDirectory | /ITM.sdf'; LCID = 1033; Password = ABCDEF; Encrypt = TRUE;”; 仍是同样的问题。更改反映的是运行时间,但不会在应用程序重新启动后反映出来 –

+0

当应用程序正在运行时,更新/插入的数据在那里? –

+0

是的。应用程序正在运行。所有的值都被更新/插入。一旦我重新启动应用程序。值已不存在 –