2017-09-01 72 views
0

我在庫存應用程序的C#項目中使用Csv文件和datagridview,嘗試將行更新爲CSV文件!datagridview將行更新到csv文件

我需要的,如果用戶編輯一行當前單詞更新與新詞但在這裏我的問題是我需要保存當前的字和新詞和僞代碼示例獲得總:

foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       if(row in column is modified) 
        update specific row with comma to current file and load it... 
      } 

CSV文件的樣子, 電流:

1; 2; 4; 5

更新:

1; 2,A ;; 4; 5改變裝置總:1周時間...

下一行改性:

1; A ;; 4,B,℃; 5發生變化的設備ç總變化:2次...

與數據庫可以很容易地更新數據,但我沒有安裝的SQL Server所以這個選項不對我來說,我認爲...

我的目標是跟蹤設備出/入,如果你有一個解決方案,請分享它。

回答

1

使用SQL服務器的缺點,可能是什麼like this could helpLiteDB您需要LiteDB來託管您的數據,並在需要時將其導出爲CSV。處理CSV文件通常意味着您每次更新時都會重新寫入整個文件......這很慢並且很麻煩。我建議您使用CSV將數據從點A傳輸到點B,但不保留數據。此外,如果你真的想堅持CSV,看看微軟Ace OLEDB驅動程序,以前稱爲JET驅動程序。我用它來查詢CSV文件,但我從來沒有用它來更新...所以你的里程可能會有所不同。

使用實際的數據庫或數據庫驅動程序的缺點是,您必須使用StreamReader和StreamWriter。用StreamReader讀取文件,用StreamWriter寫入新文件。在你的StreanReader中。這意味着您將在StreamReader中有代碼來查找要更新的正確行。

下面是我創建並用於與LiteDB交互的類。這並不是那麼強大,但它確實正是我當時所需要做的。我必須對我的平臺上託管的一系列產品進行更改,並使用它來跟蹤進度。

using System; 
using LiteDB; 

namespace FixProductsProperty 
{ 

    public enum ListAction 
    { 
     Add = 0, 
     Remove, 
     Update, 
     Disable, 
     Enable 
    } 

    class DbInteractions 
    { 
     public static readonly string dbFilename = "MyDatabaseName.db"; 
     public static readonly string dbItemsTableName = "MyTableName"; 
     public void ToDataBase(ListAction incomingAction, TrackingDbEntry dbEntry = null) 
     { 

      if (dbEntry == null) 
      { 
       Exception ex = new Exception("dbEntry can not be null"); 
       throw ex; 
      } 


      // Open database (or create if not exits) 
      using (var db = new LiteDatabase(dbFilename)) 
      { 

       var backupListInDB = db.GetCollection<TrackingDbEntry>(dbItemsTableName); 

       //ovverride action if needed 
       if (incomingAction == ListAction.Add) 
       { 
        var tempone = backupListInDB.FindOne(p => p.ProductID == dbEntry.ProductID); 
        if (backupListInDB.FindOne(p => p.ProductID == dbEntry.ProductID) != null) 
        { 
         //the record already exists 
         incomingAction = ListAction.Update; 
         //IOException ex = new IOException("Err: Duplicate. " + dbEntry.ProductID + " is already in the database."); 
         //throw ex; 
        } 
        else 
        { 
         //the record does not already exist 
         incomingAction = ListAction.Add; 
        } 
       } 

       switch (incomingAction) 
       { 
        case ListAction.Add: 
         backupListInDB.Insert(dbEntry); 
         break; 
        case ListAction.Remove: 
         //backupListInDB.Delete(p => p.FileOrFolderPath == backupItem.FileOrFolderPath); 
         if (dbEntry.ProductID != 0) 
         { 
          backupListInDB.Delete(dbEntry.ProductID); 
         } 
         break; 
        case ListAction.Update: 
         if (dbEntry.ProductID != 0) 
         { 
          backupListInDB.Update(dbEntry.ProductID, dbEntry); 
         } 
         break; 
        case ListAction.Disable: 
         break; 
        case ListAction.Enable: 
         break; 
        default: 
         break; 
       } 

       backupListInDB.EnsureIndex(p => p.ProductID); 
       // Use Linq to query documents 
       //var results = backupListInDB.Find(x => x.Name.StartsWith("Jo")); 
      } 
     } 

    } 
} 

我用這樣的:

DbInteractions yeah = new DbInteractions(); 
yeah.ToDataBase(ListAction.Add, new TrackingDbEntry { ProductID = dataBoundItem.ProductID, StoreID = dataBoundItem.StoreID, ChangeStatus = true }); 

對不起...我的變量命名約定有時吹...

+0

感謝您的回覆,我會檢查文件,你用litedb之前?如果你有一個例子,我會很高興實施LiteDB到我的應用程序需要1 DLL的工作它是完美的。 – Kate

+0

我以前使用LiteDB,這是相當不錯,因爲我的'低科技'的要求。 [他們的網站](http://www.litedb.org/)的頭版在底部附近有代碼示例。那是我開始的地方。 –

+0

@Kate,用我的LiteDB類的複製/粘貼來更新我的文章。 –