📄 landing.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace simular_data
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Landing : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
public Landing()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 152);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 0;
this.button1.Text = "登 录";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(64, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 32);
this.label1.TabIndex = 1;
this.label1.Text = "用 户 名:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(64, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 32);
this.label2.TabIndex = 2;
this.label2.Text = "密 码:";
//
// textBox1
//
this.textBox1.AutoSize = false;
this.textBox1.Location = new System.Drawing.Point(184, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 26);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.AutoSize = false;
this.textBox2.Location = new System.Drawing.Point(184, 88);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = '*';
this.textBox2.Size = new System.Drawing.Size(120, 26);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// button2
//
this.button2.Location = new System.Drawing.Point(248, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(136, 32);
this.button2.TabIndex = 5;
this.button2.Text = "退 出";
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// Landing
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(456, 214);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Landing";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Landing";
this.Load += new System.EventHandler(this.Landing_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Landing());
}
// public int dbTime_Hour =DateTime.Now.Hour ; //生成表的时间
// public int dbTime_Minute=DateTime.Now.Minute + 1;
// public int dbTime_Second=0;
public int SensorCount; //传感器数量
public SqlConnection connection1=new System.Data.SqlClient.SqlConnection ();
public DataSet dtset=new System.Data.DataSet() ;
public DataSet custDS = new DataSet();
public static Sensor[] sensors = new Sensor[80];
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private void Landing_Load(object sender, System.EventArgs e)
{
textBox1.Focus();
}
private void button1_Click(object sender, System.EventArgs e)
{
//核查用户密码是否正确代码
timer1.Enabled =true;
connection1.ConnectionString = "workstation id=local;integrated security=SSPI;" + "persist security info=False;initial catalog=Marine_network";
//打开数据连接
connection1.Open();
//定义查询变量并初始化
SqlCommand selectCMD1 = new SqlCommand("SELECT * FROM 传感器表", connection1);
//定义数据适配器,并用查询变量初始化
SqlDataAdapter custDA1 = new SqlDataAdapter();
custDA1.SelectCommand = selectCMD1;
//填充数据集
custDA1.Fill(custDS, "传感器表");
//使用数据显示控件显示数据集中的表
//定义查询变量并初始化
SqlCommand selectCMD2 = new SqlCommand("SELECT * FROM 子系统表", connection1);
//定义数据适配器,并用查询变量初始化
SqlDataAdapter custDA2 = new SqlDataAdapter();
custDA2.SelectCommand = selectCMD2;
//填充数据集
custDA2.Fill(custDS, "子系统表");
//定义查询变量并初始化
SqlCommand selectCMD3 = new SqlCommand("SELECT * FROM 结点表", connection1);
//定义数据适配器,并用查询变量初始化
SqlDataAdapter custDA3 = new SqlDataAdapter();
custDA3.SelectCommand = selectCMD3;
//填充数据集
custDA3.Fill(custDS, "结点表");
SensorCount=custDS.Tables[0].Rows.Count;
try
{
//创建表
foreach(DataRow myRow in custDS.Tables[0].Rows ) //遍历传感器表中的每条记录
{
string day ,ArchiveName,Item,Unit;
string NodeName="";
string SubSysName="";
double Precision;
day = DateTime.Now.Year.ToString () + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString () ; //记录当天年月日,创建表时用
ArchiveName = myRow[1].ToString().Trim () + "_" + day; //要创建的表名
Item=myRow[2].ToString().Trim ();
foreach(DataRow myRow1 in custDS.Tables[1].Rows) //遍历子系统表中的每条记录
{
foreach(DataRow myRow2 in custDS.Tables[2].Rows) //遍历结点表中的每条记录
{
if(myRow1[3].GetHashCode()==myRow2[0].GetHashCode())
{
NodeName=myRow2[2].ToString ();
break;
}
}
if(myRow[3].GetHashCode()==myRow1[0].GetHashCode())
{
SubSysName=myRow1[4].ToString();
break;
}
}
switch(Item)
{
case "温度":
Unit= "celsius" ;
Precision = 0.001;
break;
case "水流速度":
Unit=" cm/s" ;
Precision =1 ;
break;
case "盐度":
Unit= "‰" ;
Precision =0.001 ;
break;
case "PH值":
Unit= "ph" ;
Precision =0.01 ;
break;
case "F离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "Br离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "Cl离子含量":
Unit=" mg/l" ;
Precision = 0.001;
break;
case "NO4离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "SO4离子含量":
Unit=" mg/l" ;
Precision =0.1 ;
break;
default:
Unit= "unknown" ;
Precision =0 ;
break;
}
SqlCommand cmd = connection1.CreateCommand();
cmd.CommandText = "CREATE TABLE "+ ArchiveName +"(Time DATETIME PRIMARY KEY, Value FLOAT )";
cmd.ExecuteNonQuery();
SqlCommand cmd1 = connection1.CreateCommand();
cmd1.CommandText = "INSERT INTO MetaData (日志文件名,结点名,子系统名,监测项目,单位,精度) VALUES('" + ArchiveName+ "','" +NodeName + "','" +SubSysName + "','" +Item + "','" +Unit +"'," + Precision + ")";
cmd1.ExecuteNonQuery();
}
}
catch (SqlException ex1)
{
MessageBox.Show("今天的表已经存在!");
}
catch (Exception ex2)
{
MessageBox.Show (ex2.Message );
}
connection1.Close();
//传感器生成数据代码
int i=1;
foreach(DataRow myRow in custDS.Tables[0].Rows) //遍历子系统中的每条记录
{
sensors[i] = new Sensor(myRow[1].ToString().Trim());
sensors[i].Se_ID = myRow[0].GetHashCode();
sensors[i].Se_Item = myRow[2].ToString().Trim();
sensors[i].Se_Sub_ID = myRow[3].GetHashCode();
sensors[i].Se_Start();
// MessageBox.Show(sensors[i].Se_ID.ToString ());
i++;
}
//
this.Hide ();
main frm = new main();
frm.Show();
}
//
private void timer1_Tick(object sender, System.EventArgs e)
{
//
if ( DateTime.Now.Hour==0 && DateTime.Now.Minute== 0 && DateTime.Now.Second==0)
{
connection1.ConnectionString = "workstation id=local;integrated security=SSPI;" + "persist security info=False;initial catalog=Marine_network";
//打开数据连接
connection1.Open();
SqlCommand selectCMD = new SqlCommand("SELECT * FROM 传感器表", connection1);
// selectCMD.CommandTimeout = 30;
//定义数据适配器,并用查询变量初始化
SqlDataAdapter adp= new SqlDataAdapter ();
adp.SelectCommand = selectCMD;
//填充数据集
adp.Fill(custDS, "传感器表");
try
{
foreach(DataRow myRow in custDS.Tables[0].Rows ) //遍历子系统中的每条记录
{
string day ,ArchiveName,Item,Unit;
double Precision;
day = DateTime.Now.Year.ToString () + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString () ; //记录当天年月日,创建表时用
ArchiveName = myRow[1].ToString().Trim () + "_" + day; //要创建的表名
Item=myRow[2].ToString().Trim ();
SensorCount=custDS.Tables[0].Rows.Count;
switch(Item)
{
case "温度":
Unit= "celsius" ;
Precision = 0.001;
break;
case "水流速度":
Unit=" cm/s" ;
Precision =1 ;
break;
case "盐度":
Unit= "‰" ;
Precision =0.001 ;
break;
case "PH值":
Unit= "ph" ;
Precision =0.01 ;
break;
case "F离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "Br离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "Cl离子含量":
Unit=" mg/l" ;
Precision = 0.001;
break;
case "NO4离子含量":
Unit=" mg/l" ;
Precision =0.01 ;
break;
case "SO4离子含量":
Unit=" mg/l" ;
Precision =0.1 ;
break;
default:
Unit= "unknown" ;
Precision =0 ;
break;
}
SqlCommand cmd = connection1.CreateCommand();
cmd.CommandText = "CREATE TABLE "+ ArchiveName +"(Time DATETIME PRIMARY KEY, Value FLOAT )";
cmd.ExecuteNonQuery();
SqlCommand cmd1 = connection1.CreateCommand();
cmd1.CommandText = "INSERT INTO MetaData (日志文件名,监测项目,单位,精度) VALUES('" + ArchiveName+ "','" +Item + "','" +Unit +"'," + Precision + ")";
cmd1.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show (ex.Message );
}
connection1.Close();
}
}
private void button2_Click_1(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -