Рис. 5 – Алгоритм добавления трансформатора в БД
Рис. 5 – Алгоритм отображения трансформаторов из БД
Рис. 6 – Алгоритм удаления замера из БД
Рис. 7 – Расчёта нагрузочных потерь электроэнергии
Приложение
Исходный код программы. Приведены наиболее значимые участки.
//для записи в log-файл
public static void LogInFile(string text)
{
LogStream.WriteLine(DateTime.Now.ToString());
LogStream.WriteLine(text);
LogStream.Flush();
}
//Подключаемся к БД
private void подключитьсяКБДToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
db = new DataClasses1DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\SVPP\\TransformerDataBase\\TransformerDataBase\\Transformers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
db.Log = LogStream;
toolStripStatusLabel1.Text = "Состояние: подключено к БД";
LogInFile("Подключение к БД");
toolStripMenuItem1.Enabled = true;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
LogInFile(Ex.Message);
}
}
//добавляем трансформатор
private void ввестиДанныеToolStripMenuItem1_Click(object sender, EventArgs e)
{
LogInFile("Меню ввести данные");
TransForm NewTransForm = new TransForm();
NewTransForm.ShowDialog();
if (NewTransForm.DialogResult == DialogResult.OK)
{
try
{
TransPassport NewTransPassport = new TransPassport();
NewTransPassport.ReadFromForm(NewTransForm);
db.TransPassports.InsertOnSubmit(NewTransPassport);
db.SubmitChanges();
dataGridView1.DataSource = db.TransPassports.Where(p=>true);
button_add_sub.Enabled = button_dell_sub.Enabled = button_edit_sub.Enabled = true;
button_add_repair.Enabled = button_dell_repair.Enabled = button_edit_repair.Enabled = true;
button_add_mesure.Enabled = button_dell_mesure.Enabled = button_edit_mesure.Enabled = true;
toolStripButton1.Enabled = toolStripButton2.Enabled = true;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
LogInFile(Ex.Message);
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
int CurrentIndex = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
dataGridView2.DataSource = db.Mesurements.Where(p => p.TransformerID == CurrentIndex);
dataGridView3.DataSource = db.Repairs.Where(p => p.TransformerID == CurrentIndex);
TransPassport NewTransPassport=db.TransPassports.Where(p => p.TransformerID == CurrentIndex).FirstOrDefault<TransPassport>();
textBox1.Text = "";
|
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
//проверка данных
#region
if (NewTransPassport.dPxx.HasValue)
{
textBox1.Text = NewTransPassport.dPxx.Value.ToString();
}
if (NewTransPassport.dPkz.HasValue)
{
textBox2.Text = NewTransPassport.dPkz.Value.ToString();
}
if(NewTransPassport.Ixx_.HasValue)
{
textBox3.Text = NewTransPassport.Ixx_.Value.ToString();
}
if(NewTransPassport.Ukz_.HasValue)
{
textBox4.Text = NewTransPassport.Ukz_.Value.ToString();
}
#endregion
textBox5.Text = NewTransPassport.Produser.ToString();
if (NewTransPassport.SubstationID.HasValue)
{
textBox6.Text = String.Format("{0}{1}",NewTransPassport.Substation.Name+Environment.NewLine, NewTransPassport.Substation.Adress);
}
//Выбор данных для диаграммы
diagram1.chart1.Series[0].Name = "Потери";
diagram1.DiagramBuild(NewTransPassport, db);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "Error");
MessageBox.Show(Ex.Message, "Error!!!!");
LogInFile(Ex.Message);
}
}
//Удалить трансформатор
private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
{
LogInFile("Меню удалить");
try
{
var SelectedCells = dataGridView1.SelectedCells;
int iSelectedRow = SelectedCells[0].RowIndex;
int i = int.Parse(dataGridView1.Rows[iSelectedRow].Cells[0].Value.ToString());
TransPassport TransSelected = db.TransPassports.Where(p => p.TransformerID == i).FirstOrDefault<TransPassport>();
foreach (Mesurement a in TransSelected.Mesurements)
{
db.Mesurements.DeleteOnSubmit(a);
}
foreach (Repair a in TransSelected.Repairs)
{
db.Repairs.DeleteOnSubmit(a);
}
db.TransPassports.DeleteOnSubmit(TransSelected);
db.SubmitChanges();
dataGridView1.DataSource = db.TransPassports.Where(p => true);
}
catch (Exception Ex)
{
LogInFile(Ex.Message);
}
}
//экспорт в файл
private void ToXml_Click(object sender, EventArgs e)
{
try
{
LogInFile("Меню экспорт");
saveFileDialog1.ShowDialog();
var SelectedCells = dataGridView1.SelectedCells;
int iSelectedRow = SelectedCells[0].RowIndex;
int i = int.Parse(dataGridView1.Rows[iSelectedRow].Cells[0].Value.ToString());
TransPassport TransSelected = db.TransPassports.Where(p => p.TransformerID == i).FirstOrDefault<TransPassport>();
XDocument xDoc = new XDocument(TransSelected.ToXElement());
xDoc.Save(saveFileDialog1.FileName);
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
|
LogInFile(Ex.Message);
}
}
//импорт из файла
private void импортИзXMLToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
LogInFile("Меню импорт");
openFileDialog1.ShowDialog();
if (openFileDialog1.OpenFile()!= null)
{
XElement XTransPassport = XElement.Load(openFileDialog1.OpenFile());
MessageBox.Show(XTransPassport.Attribute("TransformerID").Value.ToString());
if (XTransPassport.HasElements)
{
TransPassport NewTransPassport = new TransPassport();
int NewID=0;
NewID=db.TransPassports.Max(p=>p.TransformerID)+1;
NewTransPassport.TransformerID=NewID;
NewTransPassport.Type = XTransPassport.Element("Type").Value.ToString();
NewTransPassport.Produser = XTransPassport.Element("Produser").Value.ToString();
#region
try
{
NewTransPassport.Power = float.Parse(XTransPassport.Element("Power").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.PrimVoltage = float.Parse(XTransPassport.Element("PrimVoltage").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.SecVoltage = float.Parse(XTransPassport.Element("SecVoltage").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.dPxx = float.Parse(XTransPassport.Element("dPxx").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.dPkz = float.Parse(XTransPassport.Element("dPkz").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.Ixx_ = float.Parse(XTransPassport.Element("Ixx").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try{
NewTransPassport.Ukz_ = float.Parse(XTransPassport.Element("Ukz").Value.ToString());
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
try
{
int Sub = int.Parse(XTransPassport.Element("Substation").Value.ToString());
if (db.Substations.Where(p => p.SubstationID == Sub).FirstOrDefault<Substation>()!= null)
{
NewTransPassport.SubstationID = Sub;
NewTransPassport.Substation = db.Substations.Where(p => p.SubstationID == Sub).FirstOrDefault<Substation>();
}
else
{
MessageBox.Show("Указанной подстанции не обнаружено");
}
}
catch
{ LogInFile("Не критичная ошибка чтения"); }
|
#endregion
db.TransPassports.InsertOnSubmit(NewTransPassport);
if (XTransPassport.Element("Mesurements").HasElements)
{
int i = 1;
foreach (XElement a in XTransPassport.Element("Mesurements").Elements("Mesurement"))
{
Mesurement NewMesure = new Mesurement();
NewMesure.MesurmentID = db.Mesurements.Max(p => p.MesurmentID) + i;
i++;
#region
try
{
NewMesure.Load_ = float.Parse(a.Attribute("Load").Value.ToString());
}
catch{}
try
{
NewMesure.Voltage = float.Parse(a.Attribute("Voltage").Value.ToString());
}
catch { }
try
{
NewMesure.TransTemperature = float.Parse(a.Attribute("TransTemperature").Value.ToString());
}
catch { }
try
{
NewMesure.OutTemperature = float.Parse(a.Attribute("OutTemperature").Value.ToString());
}
catch { }
#endregion
NewMesure.DateTime = DateTime.Parse(a.Attribute("Date").Value.ToString());
NewMesure.TransformerID = NewID;
NewMesure.TransPassport = NewTransPassport;
db.Mesurements.InsertOnSubmit(NewMesure);
}
}
db.SubmitChanges();
dataGridView1.DataSource = db.TransPassports.Where(p => true);
button_add_sub.Enabled = button_dell_sub.Enabled = button_edit_sub.Enabled = true;
button_add_repair.Enabled = button_dell_repair.Enabled = button_edit_repair.Enabled = true;
button_add_mesure.Enabled = button_dell_mesure.Enabled = button_edit_mesure.Enabled = true;
toolStripButton1.Enabled = toolStripButton2.Enabled = true;
}
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
LogInFile(Ex.Message);
}
}
partial class TransPassport
{
public void ReadFromForm(TransForm NewTransForm)
{
TransformerID = int.Parse(NewTransForm.textBox1.Text);
Type = NewTransForm.textBox2.Text;
Power = float.Parse(NewTransForm.textBox4.Text);
PrimVoltage = float.Parse(NewTransForm.textBox5.Text);
#region//чтение
try
{ Produser = NewTransForm.textBox3.Text; }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ SecVoltage = float.Parse(NewTransForm.textBox6.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ dPxx = float.Parse(NewTransForm.textBox7.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ dPkz = float.Parse(NewTransForm.textBox8.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ Ixx_ = float.Parse(NewTransForm.textBox9.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ Ukz_ = float.Parse(NewTransForm.textBox10.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
try
{ SubstationID = int.Parse(NewTransForm.textBox11.Text); }
catch (Exception Ex)
{ Form1.LogInFile(Ex.Message); }
#endregion
}
public XElement ToXElement()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
XElement NewMesureElements=new XElement("Mesurements",
Mesurements.Select(p=>
new XElement("Mesurement",
new XAttribute("MesureID", p.MesurmentID.ToString()),
new XAttribute("Load",p.Load_.ToString()),
new XAttribute("Voltage",p.Voltage.ToString()),
new XAttribute("TransTemperature",p.TransTemperature.ToString()),
new XAttribute("OutTemperature",p.TransTemperature.ToString()),
new XAttribute("Date", p.DateTime))));
XElement NewElement = new XElement("Transformer",
new XAttribute("TransformerID", TransformerID),
new XElement("Type", Type),
new XElement("Produser", Produser),
new XElement("Power", Power.ToString()),
new XElement("PrimVoltage", PrimVoltage.ToString()),
new XElement("SecVoltage", SecVoltage.ToString()),
new XElement("dPxx", dPxx.ToString()),
new XElement("dPkz", dPkz.ToString()),
new XElement("Ixx", Ixx_.ToString()),
new XElement("Ukz", Ukz_.ToString()),
new XElement("Substation", SubstationID),
NewMesureElements);
return NewElement;
}
}