2017-02-28 80 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

是的。應用程序正在運行。所有的值都被更新/插入。一旦我重新啓動應用程序。值已不存在 –