public static DataTable ReadTable() { //string conString = "Data Source=Trips;Version=3;"; SQLiteConnection conn = new SQLiteConnection(conString); SQLiteConnection con = null; SQLiteCommand command = null; SQLiteDataAdapter da = null; DataTable dt = new DataTable(); try { string sql = string.Format("select * from Trips"); command = new SQLiteCommand(sql, conn); da = new SQLiteDataAdapter(command); da.Fill(dt); } catch { } finally { if (da != null) { da.Dispose(); } if (command != null) { command.Dispose(); } if (con != null) { con.Close(); } } DataTable newTable = TableCenter.getTestTable(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = newTable.NewRow(); row["ProductNumber"] = dt.Rows[i]["ProductNumber"]; row["Item"] = dt.Rows[i]["Item"]; row["Time"] = dt.Rows[i]["Time"]; row["Result"] = dt.Rows[i]["Result"]; row["Type"] = dt.Rows[i]["Type"]; row["Ir"] = dt.Rows[i]["Ir"]; row["Tr"] = dt.Rows[i]["Tr"]; row["I"] = dt.Rows[i]["I"]; row["T"] = dt.Rows[i]["T"]; //if (i >= 3 && i < dt.Rows.Count - 1) //{ // continue; //} //row["DeviceCode"] = "DW45"; //row["FactoryCode"] = "Shihlin Electric"; //row["ProductNumber"] = dt.Rows[i]["ProductNumber"]; //row["ProductDate"] = dt.Rows[i]["ProductDate"]; //row["ProductNumber"] = dt.Rows[i]["ProductNumber"]; //row["BreakType"] = "XSIC-P3G1"; //row["Inm"] = dt.Rows[i]["Inm"]; //row["In"] = dt.Rows[i]["In"]; //row["Imcr"] = dt.Rows[i]["Imcr"]; //row["Version"] = dt.Rows[i]["Version"]; //row["ControllerType"] = dt.Rows[i]["ControllerType"]; //row["Fn"] = dt.Rows[i]["Fn"]; //row["SaveTime"] = DateTime.Now; //if (dt.Rows[i]["Inm"].ToString() == "1") //{ // row["Inm"] = "2000"; //} newTable.Rows.Add(row); } return(newTable); }
void loadData(Com com) { try { DataTable testDevices = TableCenter.getDeviceTable(); DataTable testRecords = TableCenter.getTestTable(); DataTable tripDevices = TableCenter.getDeviceTable(); DataTable tripRecords = TableCenter.getTripTable(); //获取记录条数 byte[] snd = new byte[] { 1, 3, 0, 0, 0, 1 }; byte[] rcv; rcv = com.Execute(snd); int testCount = rcv[1]; snd[2] = 0x60; rcv = com.Execute(snd); int tripCount = rcv[1]; //总通信次数: int total = 2 + testCount * (14 + 1) + tripCount * (10 + 1); //*1.05,保存数据至数据库需花费时间 total = (int)(total * 1.05); int index = 2; Update(this, new ComsEventArgs(100, index * 100 / total)); //read test device info for (int i = 0; i < testCount; i++) { int start = 0x10 + 0x10 * i; snd[2] = (byte)(start / 256); snd[3] = (byte)(start % 256); snd[5] = 0x0d; rcv = com.Execute(snd); DataRow deviceRow = testDevices.NewRow(); TableCenter.genDeviceRow(ref deviceRow, rcv); testDevices.Rows.Add(deviceRow); index++; Update(this, new ComsEventArgs(100, index * 100 / total)); //read test detail record for (int j = 0; j < Tools.TestTypes.Length; j++) { start = 0x1000 + 0x0800 * i + 0x09 * j; snd[2] = (byte)(start / 256); snd[3] = (byte)(start % 256); snd[5] = 0x09; rcv = com.Execute(snd); if (rcv[1] != 0 && rcv[0] != 0xff) { DataRow testRow = testRecords.NewRow(); TableCenter.genTestRow(ref testRow, deviceRow["ProductNumber"].ToString(), Tools.TestTypes[j], rcv); testRecords.Rows.Add(testRow); } index++; Update(this, new ComsEventArgs(100, index * 100 / total)); } } //read trip device info for (int i = 0; i < tripCount; i++) { int start = 0x6010 + 0x10 * i; snd[2] = (byte)(start / 256); snd[3] = (byte)(start % 256); snd[5] = 0x0d; rcv = com.Execute(snd); DataRow deviceRow = tripDevices.NewRow(); TableCenter.genDeviceRow(ref deviceRow, rcv); tripDevices.Rows.Add(deviceRow); index++; Update(this, new ComsEventArgs(100, index * 100 / total)); //read trip detail record for (int j = 0; j < 10; j++) { start = 0x7100 + 0x0100 * i + 0x0d * j; snd[2] = (byte)(start / 256); snd[3] = (byte)(start % 256); snd[5] = 0x0d; rcv = com.Execute(snd); if (rcv[0] == 0xff) { index += 10 - j; Update(this, new ComsEventArgs(100, index * 100 / total)); break; } else { DataRow tripRow = tripRecords.NewRow(); TableCenter.genTripRow(ref tripRow, deviceRow["ProductNumber"].ToString(), rcv); tripRecords.Rows.Add(tripRow); index++; Update(this, new ComsEventArgs(100, index * 100 / total)); } } } DataAccess.UpdateTable(testDevices, "TestDevices"); DataAccess.UpdateTable(testRecords, "TestRecords"); DataAccess.UpdateTable(tripDevices, "TripDevices"); DataAccess.UpdateTable(tripRecords, "TripRecords"); Update(this, new ComsEventArgs(100, 100, ComState.SUCCESSED)); } catch (Exception exp) { Update(this, new ComsEventArgs(100, 100, ComState.FAILED)); } }
//修改数据表记录 public static void UpdateDeviceTable() { SQLiteConnection con = null; SQLiteCommand command = null; SQLiteDataAdapter oda = null; SQLiteCommandBuilder ocb = null; try { con = new SQLiteConnection(conString); command = con.CreateCommand(); command.CommandText = "select * from TestDevices"; oda = new SQLiteDataAdapter(command); ocb = new SQLiteCommandBuilder(oda); oda.UpdateCommand = ocb.GetUpdateCommand(); oda.InsertCommand = ocb.GetInsertCommand(); oda.DeleteCommand = ocb.GetDeleteCommand(); byte[] rcv = ACBTester.genDeviceInfo(); DataTable dt = TableCenter.getDeviceTable(); DataRow deviceRow = dt.NewRow(); deviceRow["DeviceCode"] = "DW45"; deviceRow["FactoryCode"] = "Shihlin Electirc"; deviceRow["ProductNumber"] = (rcv[7] * 256 + rcv[8]).ToString("D4") + (rcv[9] * 256 + rcv[10]).ToString("D4"); deviceRow["ProductDate"] = new DateTime(rcv[11] * 256 + rcv[12], rcv[13], rcv[14]); deviceRow["Inm"] = rcv[17] * 256 + rcv[18]; deviceRow["In"] = rcv[19] * 256 + rcv[20]; deviceRow["Imcr"] = rcv[21] + rcv[22]; int Inm = rcv[17] * 256 + rcv[18]; int nInm = Inm == 2000 ? 1 : (Inm == 3200 ? 2 : 3); deviceRow["BreakType"] = string.Format("XSIC-P{0}G{1}", rcv[16], nInm); deviceRow["Version"] = ""; deviceRow["ControllerType"] = ""; deviceRow["Fn"] = rcv[27] * 256 + rcv[28]; deviceRow["SaveTime"] = DateTime.Now; dt.Rows.Add(deviceRow); int n = oda.Update(dt); } catch { } finally { if (ocb != null) { ocb.Dispose(); } if (oda != null) { oda.Dispose(); } if (command != null) { command.Dispose(); } if (con != null) { con.Close(); } } }