Datalogging SQL Server With CSharp WinForms
Datalogging SQL Server With CSharp WinForms
blog
Sensor System
Windows Forms App #1
Hans-Petter Halvorsen
Sensor System Windows Forms App
DECLARE
@SensorTypeId int
INSERT INTO SENSOR (SensorName, SensorTypeId, Unit) VALUES (@SensorName, @SensorTypeId, @Unit)
GO
Test Data
We insert the folllowing Data using the SQL Server Management Studio:
SENSOR_TYPE:
insert into SENSOR_TYPE (SensorType) values ('Temperature')
insert into SENSOR_TYPE (SensorType) values ('Pressure')
insert into SENSOR_TYPE (SensorType) values ('Level')
insert into SENSOR_TYPE (SensorType) values ('Proximity ')
using System;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using System.Configuration; SensorType.cs
namespace SensorSystem.Classes
{
class SensorType
{
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
public int SensorTypeId { get; set; }
public string SensorTypeName { get; set; }
SqlDataReader dr = cmd.ExecuteReader();
if (dr != null)
{
while (dr.Read())
{
SensorType sensorType = new SensorType();
sensorType.SensorTypeId = Convert.ToInt32(dr["SensorTypeId"]);
sensorType.SensorTypeName = dr["SensorType"].ToString();
sensorTypeList.Add(sensorType);
}
}
con.Close();
return sensorTypeList;
}
}
}
using System.Data;
using System.Windows.Forms;
using
using
Microsoft.Data.SqlClient;
System.Configuration;
Sensor.cs
namespace SensorSystem.Classes
{
class Sensor
{
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
MessageBox.Show("Error Writing Data to Database");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SensorSystem.Classes; Form1.cs
namespace SensorSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FillSensorTypeComboBox();
}
sensorTypeList = sensorType.GetSensorTypes();
Datalogging
Windows Forms App #2
Hans-Petter Halvorsen
Windows Forms App #1 Datalogging Windows Forms App #2
DECLARE
@SensorId int
if @SensorDateTime is null
set @SensorDateTime = getdate()
INSERT INTO SENSOR_DATA (SensorId, SensorValue, SensorDateTime) VALUES (@SensorId, @SensorValue, @SensorDateTime)
GO
Test Data
SENSOR_TYPE:
insert into SENSOR_TYPE (SensorType) values ('Temperature')
insert into SENSOR_TYPE (SensorType) values ('Pressure')
insert into SENSOR_TYPE (SensorType) values ('Level')
insert into SENSOR_TYPE (SensorType) values ('Proximity ')
SENSOR:
insert into SENSOR (SensorName, SensorTypeId, Unit)
values ('Temperature1', (select SensorTypeId from
SENSOR_TYPE where SensorType='Temperature'), 'C')
using System;
using System.Data;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;
using System.Configuration;
namespace Datalogging.Classes
SensorData.cs
{
class SensorData
{
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr != null)
{
unitName = dr["Unit"].ToString();
}
con.Close();
return unitName;
}
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
MessageBox.Show("Error Writing Data to Database");
}
}
}
}
using System;
using System.Windows.Forms;
using Datalogging.Classes;
namespace Datalogging
{
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtSensorName.Text = "Temperature1";
timer1.Interval = 10000; //milliseconds
}
void Datalogging()
{
string sensorName;
var rand = new Random();
int minValue = 20, maxValue = 30;
double sensorValue;
sensorName = txtSensorName.Text;
// Generate SensorValue
sensorValue = rand.NextDouble() * (maxValue-minValue) + minValue;
txtSensorValue.Text = sensorValue.ToString("#.##");
DateTime sensorDateTime = DateTime.Now;
txtTimeStamp.Text = sensorDateTime.ToString("yyyy-MM-dd HH:mm:ss");
sensorName = txtSensorName.Text;
// Generate SensorValue
sensorValue = rand.NextDouble() * (maxValue-minValue) + minValue;
txtSensorValue.Text = sensorValue.ToString("#.##");
DateTime sensorDateTime = DateTime.Now;
txtTimeStamp.Text = sensorDateTime.ToString("yyyy-MM-dd HH:mm:ss");
//Update GridView
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dgvLoggedData);
loggingIndex++;
row.Cells[0].Value = loggingIndex;
row.Cells[1].Value = sensorValue.ToString("#.##");
row.Cells[2].Value = sensorDateTime.ToString("yyyy-MM-dd HH:mm:ss");
dgvLoggedData.Rows.Add(row);
E-mail: [email protected]
Web: https://www.halvorsen.blog