private void cmdSelectBitFromProjectInDataRowsList_Click(object sender, RoutedEventArgs e) { if (grdDatasetFields.SelectedItem != null) { DatasetConfigRow conf = grdDatasetFields.SelectedItem as DatasetConfigRow; var tmp = DotNetSiemensPLCToolBoxLibrary.Projectfiles.SelectProjectPart.SelectTAG(""); if (tmp != null) { conf.PLCTag = tmp; } } }
private void cmdEditBitInDataRowsList_Click(object sender, RoutedEventArgs e) { if (grdDatasetFields.SelectedItem != null) { DatasetConfigRow conf = grdDatasetFields.SelectedItem as DatasetConfigRow; var tmp = DotNetSiemensPLCToolBoxLibrary.Communication.PLCTagEditor.ShowPLCTagEditor(conf.PLCTag); if (tmp != null) { conf.PLCTag = tmp; } } }
private void cmdAddDatasetRowsFromProjectFile_Click(object sender, RoutedEventArgs e) { string DataBaseTyp; string DataBlockTyp; //Anpassungen von Henning Göpfert-Dürwald & Christoph Reinshaus var tags = DotNetSiemensPLCToolBoxLibrary.Projectfiles.SelectProjectPart.SelectTAGs(""); if (tags != null) { foreach (PLCTag tag in tags) { #region Create new row DatasetConfig conf = grdDatasets.SelectedItem as DatasetConfig; ConnectionConfig akConn = null; string FieldType = ""; if (conf.DatasetConfigRows.Count > 0) { akConn = conf.DatasetConfigRows[conf.DatasetConfigRows.Count - 1].Connection; FieldType = conf.DatasetConfigRows[conf.DatasetConfigRows.Count - 1].DatabaseFieldType; } conf.DatasetConfigRows.Add(new DatasetConfigRow() { DatabaseField = "Row_" + (conf.DatasetConfigRows.Count + 1).ToString(), Connection = akConn, DatabaseFieldType = FieldType }); #endregion DatasetConfigRow confRow = grdDatasetFields.Items[grdDatasetFields.Items.Count - 1] as DatasetConfigRow; confRow.PLCTag = tag; confRow.DatabaseField = tag.ValueName; if (conf.Storage != null) { DataBaseTyp = conf.Storage.GetDefaultDatabaseFieldTypeForLibNoDaveTag(tag); confRow.DatabaseFieldType = DataBaseTyp; } } } }
protected override bool _internal_Write() { try { for (int n = 0; n < _maxAdd; n++) { IEnumerable <object> values = _intValueList[n]; var addDateTime = _intDateTimesList[n]; List <PLCTag> writeList = new List <PLCTag>(); using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { DatasetConfigRow field = e1.Current; Object value = e2.Current; field.PLCTag.Value = value; writeList.Add(field.PLCTag); } } _plcConnection.WriteValues(writeList, true); } } catch (Exception ex) { throw; } if (_newDataCallback != null) { _newDataCallback(datasetConfig.Name); } return(true); }
protected override bool _internal_Write() { //Look if the Connection is still open.. try { string sql = "SELECT id FROM " + dataTable + " WHERE id = 0"; myCmd.Connection = myDBConn; myCmd.CommandText = sql; myCmd.ExecuteNonQuery(); } catch (Exception) { myDBConn.Close(); //Verbindung schließen! myDBConn.Open(); if (myDBConn.State != System.Data.ConnectionState.Open) { Logging.LogText("Error ReConnecting to Database! Dataset:" + datasetConfig.Name, Logging.LogLevel.Error); return(false); } myDBConn.ChangeDatabase(myConfig.Database); } //Add the Fields to the Database myCmd.Connection = myDBConn; if (datasetConfig.UseDbUpdateNoInsert) { myCmd.CommandText = updateCommand + " " + (datasetConfig.UpdateWhereClause ?? ""); } else { myCmd.CommandText = insertCommand; } for (int n = 0; n < _maxAdd; n++) //foreach (IEnumerable<object> values in _intValueList) { myCmd.Parameters.Clear(); IEnumerable <object> values = _intValueList[n]; using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { //foreach (DatasetConfigRow field in fieldList) //{ DatasetConfigRow field = e1.Current; Object value = e2.Current; //values[fnr++]; myCmd.Parameters.Add(new MySqlParameter("?" + field.DatabaseField, value)); } } int tryCounter = 0; nomol: try { myCmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } } /* * //Ringpufferarchiv... * if (myProtokollDaten.MaxDatasets > 0) * { * string delstr = "DELETE FROM " + myProtokollDaten.DataBaseTable + " WHERE id <= (SELECT max(id) FROM " + myProtokollDaten.DataBaseTable + ") - (" + myProtokollDaten.MaxDatasets.ToString() + ")"; * myCmd.CommandText = delstr; * myCmd.ExecuteNonQuery(); * } */ return(true); }
protected override bool _internal_Write() { //Look if the Connection is still open.. try { string sql = "SELECT id FROM " + dataTable + " WHERE id = 0"; myCmd = new FbCommand(sql, myDBConn); myCmd.ExecuteNonQuery(); } catch (Exception) { myDBConn.Close(); //Verbindung schließen! myDBConn.Open(); if (myDBConn.State != System.Data.ConnectionState.Open) { Logging.LogText("Error ReConnecting to Database! Dataset:" + datasetConfig.Name, Logging.LogLevel.Error); return(false); } myDBConn.ChangeDatabase(myConfig.DatabaseFile); } //Add the Fields to the Database myCmd = new FbCommand(insertCommand, myDBConn); for (int n = 0; n < _maxAdd; n++) //foreach (IEnumerable<object> values in _intValueList) { myCmd.Parameters.Clear(); IEnumerable <object> values = _intValueList[n]; using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { //foreach (DatasetConfigRow field in fieldList) //{ DatasetConfigRow field = e1.Current; Object value = e2.Current; //values[fnr++]; switch (field.PLCTag.TagDataType) { case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.LInt: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.LWord: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Int: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Dint: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Dword: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Byte: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDByte: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDWord: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDDWord: myCmd.Parameters.Add(new FbParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = value.ToString() }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Float: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.LReal: myCmd.Parameters.Add(new FbParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = value.ToString().Replace(',', '.') }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.DateTime: myCmd.Parameters.Add(new FbParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = ((DateTime)value).ToString("dd-MM-yyyy HH:mm:ss") }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.String: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.CharArray: myCmd.Parameters.Add(new FbParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = (String)value }); break; } } } int tryCounter = 0; nomol: try { myCmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } } /* * //Ringpufferarchiv... * if (myProtokollDaten.MaxDatasets > 0) * { * string delstr = "DELETE FROM " + myProtokollDaten.DataBaseTable + " WHERE id <= (SELECT max(id) FROM " + myProtokollDaten.DataBaseTable + ") - (" + myProtokollDaten.MaxDatasets.ToString() + ")"; * myCmd.CommandText = delstr; * myCmd.ExecuteNonQuery(); * } */ return(true); }
protected override bool _internal_Write() { //Look if the Connection is still open.. try { string sql = "SELECT id FROM " + dataTable + " WHERE id = 0"; myCmd.Connection = myDBConn; myCmd.CommandText = sql; myCmd.ExecuteNonQuery(); } catch (Exception ex) { Logging.LogTextToLog4Net("Exception in SQL:" + myCmd.CommandText, Logging.LogLevel.Error, ex); myDBConn.Close(); //Verbindung schließen! myDBConn.Open(); if (myDBConn.State != System.Data.ConnectionState.Open) { Logging.LogText("Error ReConnecting to Database! Dataset:" + datasetConfig.Name, Logging.LogLevel.Error); return(false); } myDBConn.ChangeDatabase(myConfig.Database); } //Add the Fields to the Database myCmd.Connection = myDBConn; myCmd.CommandText = insertCommand; for (int n = 0; n < _maxAdd; n++) //foreach (IEnumerable<object> values in _intValueList) { myCmd.Parameters.Clear(); IEnumerable <object> values = _intValueList[n]; using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { //foreach (DatasetConfigRow field in fieldList) //{ DatasetConfigRow field = e1.Current; Object value = e2.Current; //values[fnr++]; var par = new NpgsqlParameter("@" + field.DatabaseField, value); myCmd.Parameters.Add(par); //switch (field.DatabaseFieldType) //{ // case "int8": // par.NpgsqlDbType = NpgsqlDbType.Bigint; // //par.Size = 1; // break; // case "timestamp": // par.NpgsqlDbType = NpgsqlDbType.Timestamp; // //par.Size = 1; // break; // case "varchar": // par.NpgsqlDbType = NpgsqlDbType.Varchar; // par.Size = value != null ? value.ToString().Length : 0; // break; //} //Logging.LogText(string.Format("KeyValue Key:{0} Value:{1}", field.DatabaseField, value ?? "null"), Logging.LogLevel.Information); } } int tryCounter = 0; nomol: try { myCmd.ExecuteNonQuery(); } catch (NpgsqlException ex) { Logging.LogTextToLog4Net("Exception in SQL:" + myCmd.CommandText, Logging.LogLevel.Error, ex); //if (ex.ErrorCode == "08P01") //{ // myCmd = new NpgsqlCommand(); // myCmd.Connection = myDBConn; // myCmd.CommandText = ex.ErrorSql; // myCmd.Parameters.Clear(); // myCmd.ExecuteNonQuery(); //} //else //{ // Logging.LogText( // "Exception (ColumnName:" + (ex.ColumnName ?? "") + ", SQL:" + (ex.ErrorSql ?? "") + // ", Detail:" + // (ex.Detail ?? "") + ": ", // Logging.LogLevel.Error); // //using (StreamWriter outfile = new StreamWriter("c:\\error.txt", true)) // //{ // // outfile.WriteLine("Exception (ColumnName:" + (ex.ColumnName ?? "") + ", Detail:" + // // (ex.Detail ?? "") + ", SQL:" + (ex.ErrorSql ?? "") + ": "); // //} // throw ex; //} throw; } catch (Exception ex) { Logging.LogText("Exception: ", ex, Logging.LogLevel.Error); throw ex; } } /* * //Ringpufferarchiv... * if (myProtokollDaten.MaxDatasets > 0) * { * string delstr = "DELETE FROM " + myProtokollDaten.DataBaseTable + " WHERE id <= (SELECT max(id) FROM " + myProtokollDaten.DataBaseTable + ") - (" + myProtokollDaten.MaxDatasets.ToString() + ")"; * myCmd.CommandText = delstr; * myCmd.ExecuteNonQuery(); * } */ return(true); }
protected override bool _internal_Write() { lock (FileNameLockObjects[myConfig.DatabaseFile]) { //Look if the Connection is still open.. try { string sql = "SELECT id FROM " + dataTable + " WHERE id = 0"; myCmd.Connection = myDBConn; myCmd.CommandText = sql; myCmd.ExecuteNonQuery(); } catch (Exception) { myDBConn.Close(); //Verbindung schließen! myDBConn.Open(); if (myDBConn.State != System.Data.ConnectionState.Open) { Logging.LogText("Error ReConnecting to Database! Dataset:" + datasetConfig.Name, Logging.LogLevel.Error); return(false); } } //Add the Fields to the Database myCmd.Connection = myDBConn; myCmd.CommandText = insertCommand; int tryCounter = 0; nomol: try { using (DbTransaction dbTrans = myDBConn.BeginTransaction()) { using (DbCommand cmd = myDBConn.CreateCommand()) { cmd.CommandText = insertCommand; for (int n = 0; n < _maxAdd; n++) //foreach (IEnumerable<object> values in _intValueList) { cmd.Parameters.Clear(); IEnumerable <object> values = _intValueList[n]; var addDateTime = _intDateTimesList[n]; if (!string.IsNullOrEmpty(dateFieldName)) { cmd.Parameters.Add(new SQLiteParameter("@" + dateFieldName, System.Data.DbType.String) { Value = addDateTime.ToString("yyyy.MM.dd - HH:mm:ss.fff") }); } using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { //foreach (DatasetConfigRow field in fieldList) //{ DatasetConfigRow field = e1.Current; Object value = e2.Current; //values[fnr++]; switch (field.PLCTag.LibNoDaveDataType) { case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Int: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Dint: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Word: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Dword: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Byte: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDByte: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDWord: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.BCDDWord: cmd.Parameters.Add(new SQLiteParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = value.ToString() }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.Float: cmd.Parameters.Add(new SQLiteParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = value.ToString().Replace(',', '.') }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.DateTime: cmd.Parameters.Add(new SQLiteParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fffffff") }); break; case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.String: case DotNetSiemensPLCToolBoxLibrary.DataTypes.TagDataType.CharArray: cmd.Parameters.Add(new SQLiteParameter("@" + field.DatabaseField, System.Data.DbType.String) { Value = (String)value }); break; } } } cmd.ExecuteNonQuery(); } //Ringpufferarchiv... if (datasetConfig.MaxDatasets > 0) { string delstr = "DELETE FROM " + dataTable + " WHERE id <= (SELECT max(id) FROM " + dataTable + ") - (" + datasetConfig.MaxDatasets.ToString() + ")"; cmd.CommandText = delstr; cmd.ExecuteNonQuery(); } } dbTrans.Commit(); } } catch (SQLiteException ex) { if (ex.ErrorCode == SQLiteErrorCode.Busy || ex.ErrorCode == SQLiteErrorCode.Locked) //Locked || Busy { tryCounter++; if (tryCounter > 20) { throw new Exception("SQLLite-Datenbank nach 20 Versuchen immer noch locked oder busy!!"); } goto nomol; } else { throw ex; } } catch (Exception ex) { throw ex; } if (_newDataCallback != null) { _newDataCallback(datasetConfig.Name); } return(true); } }
protected override bool _internal_Write() { //Look if the Connection is still open.. try { string sql = "SELECT id FROM " + dataTable + " WHERE id = 0"; myCmd.Connection = myDBConn; myCmd.CommandText = sql; myCmd.ExecuteNonQuery(); } catch (Exception ex) { myDBConn.Close(); //Verbindung schließen! myDBConn.Open(); if (myDBConn.State != System.Data.ConnectionState.Open) { Logging.LogText("Error ReConnecting to Database! Dataset:" + datasetConfig.Name, Logging.LogLevel.Error); return(false); } } try { DbTransaction dbTrans = null; if (myConfig.CombineMultipleInsertsInATransaction) { dbTrans = myDBConn.BeginTransaction(); } { using (DbCommand cmd = myDBConn.CreateCommand()) { cmd.CommandText = insertCommand; if (myConfig.CombineMultipleInsertsInATransaction) { cmd.Transaction = dbTrans; } for (int n = 0; n < _maxAdd; n++) { cmd.Parameters.Clear(); IEnumerable <object> values = _intValueList[n]; var addDateTime = _intDateTimesList[n]; if (!string.IsNullOrEmpty(dateFieldName)) { cmd.Parameters.Add(new SqlParameter("@" + dateFieldName, System.Data.DbType.String) { Value = addDateTime }); } using (IEnumerator <DatasetConfigRow> e1 = fieldList.GetEnumerator()) using (IEnumerator <object> e2 = values.GetEnumerator()) { while (e1.MoveNext() && e2.MoveNext()) { DatasetConfigRow field = e1.Current; Object value = e2.Current; if (field.DatabaseFieldType == "text" || field.DatabaseFieldType == "varchar" || field.DatabaseFieldType == "ntext" || field.DatabaseFieldType == "nvarchar" || field.DatabaseFieldType == "char" || field.DatabaseFieldType == "nchar") { cmd.Parameters.Add(new SqlParameter() { ParameterName = "@" + field.DatabaseField, Value = value.ToString() }); } else { if (value is System.Single && field.DatabaseFieldType == "float") { cmd.Parameters.Add(new SqlParameter() { ParameterName = "@" + field.DatabaseField, Value = Convert.ToDouble(value) }); } else { cmd.Parameters.Add(new SqlParameter() { ParameterName = "@" + field.DatabaseField, Value = value }); } } } } cmd.ExecuteNonQuery(); } } if (myConfig.CombineMultipleInsertsInATransaction && dbTrans != null) { dbTrans.Commit(); } } //Ringpufferarchiv... if (datasetConfig.MaxDatasets > 0) { using (DbCommand cmd = myDBConn.CreateCommand()) { string delstr = "DELETE FROM " + dataTable + " WHERE id <= (SELECT max(id) FROM " + dataTable + ") - (" + datasetConfig.MaxDatasets.ToString() + ")"; cmd.CommandText = delstr; cmd.ExecuteNonQuery(); } } if (dbTrans != null) { dbTrans.Dispose(); } } catch (Exception ex) { throw; } return(true); }