0

我正在做一个项目,我通过 USB 读取一些硬件数据,我需要每秒将状态保存到数据库中。我想通过 LINQ 做到这一点,但它似乎不起作用。它无法将更改提交到数据库。任何帮助将非常感激。

    private string connString = "someConnectionString";
    private BMSDataContext BMS_DS;
    private USBConnection USBConn;

    protected override void OnStart(string[] args)
    {
       //Connecting to USB device
       USBConn = new USBConnection();
       USBConn.attemptUSBConnection();
       if (USBConn.getConnectionStatus())
             EventLog.WriteEntry("Successfully Connected to USB device", EventLogEntryType.Information);
       else
             EventLog.WriteEntry("Failed to connect to USB device", EventLogEntryType.Error);

       //initialing the DataContext
       BMS_DS = new BMSDataContext(connString);

       System.Timers.Timer timer = new System.Timers.Timer(5000);
       timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ReadFromUSBAndLog);
       timer.Start();
}

     private void ReadFromUSBAndLog(object sender, System.Timers.ElapsedEventArgs e)
     {
          if (!USBConn.getConnectionStatus())
                   EventLog.WriteEntry("Connection to USB lost!", EventLogEntryType.Error);

          //reading data via USB
          USBConn.receiveViaUSB();

          testLog newRow = new testLog
          {
              value = USBConn.fromDeviceToHostBuffer[1].ToString()
          };

          //THIS IS AS FAR AS IT GOES...
          BMS_DS.testLogs.InsertOnSubmit(newRow);
          //THIS IS NEVER REACHED
          BMS_DS.SubmitChanges();
          EventLog.WriteEntry("Submitted", EventLogEntryType.Information);  
     }
4

1 回答 1

0

好吧,我是愚蠢的。似乎我的测试表没有主键,因此插入失败。我不知道这是一个要求。向表中添加标识 pk 列后工作正常。

谢谢

于 2013-10-06T10:00:20.093 回答