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

OK私は愚かでした。私のテストテーブルには主キーがなく、それが原因で挿入が失敗していたようです。これが必須だとは知りませんでした。ID pk 列をテーブルに追加した後、正常に動作します。

ありがとう

于 2013-10-06T10:00:20.093 に答える