2017-09-01 76 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类的复制/粘贴来更新我的文章。 –