private void pushdata_Click(object sender, EventArgs e) { if (db_tblist.SelectedItems.Count == 0) { MessageBox.Show("请选中数据表再上传!"); } else { if (MessageBox.Show("确定上传选中数据表吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { if (db_tblist.SelectedItems[0].Text == "运行数据表") { if (tb_count[0] == 0) { MessageBox.Show("数据表为空,不需要上传"); } else { db_progressBar.Value = 1; db_progressBar.Minimum = 0; db_progressBar.Maximum = tb_count[0]; for (int i = 0; tb_count[0] - 3 * i > 0; i++) { List <Data_Run_info> result; DataBase pdata = new DataBase(); /*MySqlDataReader to list */ string comm3 = "select id,cas,ccs,aload,aspd1,aspd2,aspd3,aspd4,aspd5,apst1,apst2,apst3,apst4,apst5,cpst1,cpst2,cpst3,cpst4,cpst5,load1,load2,load3,load4,load5,pd,pn,ps,pl,pm,time from tb_runstate limit 10 "; //获取前2000条数据,其中数据数据项名称必须与要转换的LIst数据项名称一样,否则无法正确赋值 MySqlConnection Conn = new MySqlConnection(pdata.M_str_sqlcon); Conn.Open(); MySqlCommand cmd = new MySqlCommand(comm3, Conn); MySqlDataReader reader = cmd.ExecuteReader(); //MySqlDataReader reader = pdata.Select(comm3); result = DataBase.Fabricate.FillList <Data_Run_info>(reader); /*close the connection*/ reader.Close(); Conn.Close(); /*list to json string*/ IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; String listStr = JsonConvert.SerializeObject(result, Formatting.None, timeConverter); /*进一步转换数据*/ Data_json data_info = new Data_json(); data_info.dt = listStr; //打包发送 data_info.did = Constants.RUNSTATE; /*push data*/ listStr = WebSreverce_PostJson.ConvertToJson(data_info); string reponce = WebSreverce_PostJson.Post_Jsonstr(WebSreverce_PostJson.urladd_inn, listStr); //url表示数据服务器的地址及接口名称,可以定义成全局变量 // Console.WriteLine(reponce); if (reponce == "ac") { //Console.WriteLine(rundata); //返回值正确,服务器收到了请求,执行删除数据库数据的操作 MySqlConnection sqlcon = new MySqlConnection(pdata.M_str_sqlcon); sqlcon.Open(); string comm4 = " delete from tb_runstate limit 10 "; //获取前2000条数据 pdata.Delete(sqlcon, comm4); sqlcon.Close(); /*操作Processbar*/ if (db_progressBar.Value + 3 <= db_progressBar.Maximum) { db_progressBar.Value += 3; this.pbar_state.Text = "进行中 [" + db_progressBar.Value.ToString() + "/" + db_progressBar.Maximum + "]...."; } else { this.pbar_state.Text = "上传已完成!"; if (MessageBox.Show("上传完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { return; } } } else { if (MessageBox.Show("与Web服务器连接异常,请检查后重试!", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { break; } } } } } if (db_tblist.SelectedItems[0].Text == "登入登出表") { if (db_tblist.SelectedItems[0].SubItems[1].Text == "0") { MessageBox.Show("数据表为空,不需要上传"); } else { db_progressBar.Value = 1; db_progressBar.Minimum = 0; db_progressBar.Maximum = tb_count[1]; for (int i = 0; tb_count[1] - i > 0; i++) { /*采用单条数据上传*/ Data_LogIn_info temp = new Data_LogIn_info(); DataBase pdata = new DataBase(); string comm3 = "select * from tb_login limit 1 "; //获取1条数据 MySqlDataReader reader = pdata.Select(comm3); temp.id = reader[1].ToString(); temp.ontime = (long)Int32.Parse(reader[2].ToString()); temp.runtime = (long)Int32.Parse(reader[3].ToString()); temp.time = reader[4].ToString(); /*close the connection*/ reader.Close(); pdata.con_close(); /*进一步打包数据,转json*/ string rundata = WebSreverce_PostJson.ConvertToJson(temp); Data_json data_info = new Data_json(); data_info.dt = rundata; data_info.did = Constants.LOGIN; rundata = WebSreverce_PostJson.ConvertToJson(data_info); string reponce = WebSreverce_PostJson.Post_Jsonstr(WebSreverce_PostJson.urladd_inn, rundata); //url表示数据服务器的地址及接口名称,可以定义成全局变量 if (reponce == "ac") { //Console.WriteLine(rundata); //返回值正确,服务器收到了请求 MySqlConnection sqlcon = new MySqlConnection(pdata.M_str_sqlcon); sqlcon.Open(); string comm4 = " delete from tb_login limit 1 "; //获取前1条数据 pdata.Delete(sqlcon, comm4); sqlcon.Close(); /*操作Processbar*/ if (db_progressBar.Value < db_progressBar.Maximum) { db_progressBar.Value += 1; this.pbar_state.Text = "上传进行中 [" + db_progressBar.Value.ToString() + "/" + db_progressBar.Maximum + "]...."; } else { this.pbar_state.Text = "上传已完成!"; if (MessageBox.Show("上传完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { return; } } } else { if (MessageBox.Show("与Web服务器连接异常,请检查后重试!", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { return; } } } } } if (db_tblist.SelectedItems[0].Text == "报警表") { if (db_tblist.SelectedItems[0].SubItems[1].Text == "0") { MessageBox.Show("数据表为空,不需要上传"); } else { db_progressBar.Value = 1; db_progressBar.Minimum = 0; db_progressBar.Maximum = tb_count[2]; for (int i = 0; tb_count[2] - i > 0; i++) { /*采用单条数据上传*/ Data_Err_info temp = new Data_Err_info(); DataBase pdata = new DataBase(); string comm3 = "select * from tb_error limit 1 "; //获取1条数据 MySqlDataReader reader = pdata.Select(comm3); temp.id = reader[1].ToString(); temp.f = (byte)Int16.Parse(reader[2].ToString()); temp.no = reader[3].ToString(); temp.time = reader[4].ToString(); temp.ctt = reader[5].ToString(); /*close the connection*/ reader.Close(); pdata.con_close(); /*进一步打包数据,转json*/ string rundata = WebSreverce_PostJson.ConvertToJson(temp); Data_json data_info = new Data_json(); data_info.dt = rundata; data_info.did = Constants.ERRORINFO; rundata = WebSreverce_PostJson.ConvertToJson(data_info); string reponce = WebSreverce_PostJson.Post_Jsonstr(WebSreverce_PostJson.urladd_inn, rundata); //url表示数据服务器的地址及接口名称,可以定义成全局变量 // Console.WriteLine(reponce); if (reponce == "ac") { //Console.WriteLine(rundata); //返回值正确,服务器收到了请求 MySqlConnection sqlcon = new MySqlConnection(pdata.M_str_sqlcon); sqlcon.Open(); string comm4 = " delete from tb_error limit 1 "; //获取前1条数据 pdata.Delete(sqlcon, comm4); sqlcon.Close(); /*操作Processbar*/ if (db_progressBar.Value < db_progressBar.Maximum) { db_progressBar.Value += 1; this.pbar_state.Text = "上传进行中 [" + db_progressBar.Value.ToString() + "/" + db_progressBar.Maximum + "]...."; } else { this.pbar_state.Text = "上传已完成!"; if (MessageBox.Show("上传完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { return; } } } else { if (MessageBox.Show("与Web服务器连接异常,请检查后重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK) { return; } } } } } if (db_tblist.SelectedItems[0].Text == "环境数据表") { if (db_tblist.SelectedItems[0].SubItems[1].Text == "0") { MessageBox.Show("数据表为空,不需要上传"); } else { db_progressBar.Value = 1; db_progressBar.Minimum = 0; db_progressBar.Maximum = tb_count[3]; for (int i = 0; tb_count[3] - i > 0; i++) { /*采用单条数据上传*/ Data_Env_info temp = new Data_Env_info(); DataBase pdata = new DataBase(); string comm3 = "select * from tb_env limit 1 "; //获取1条数据 MySqlDataReader reader = pdata.Select(comm3); temp.id = reader[1].ToString(); temp.u = Convert.ToSingle(reader[2].ToString()); temp.v = Convert.ToSingle(reader[3].ToString()); temp.w = Convert.ToSingle(reader[4].ToString()); temp.tep = Convert.ToSingle(reader[5].ToString()); temp.hmi = Convert.ToSingle(reader[6].ToString()); temp.time = reader[7].ToString(); /*close the connection*/ reader.Close(); pdata.con_close(); /*进一步打包数据,转json*/ string rundata = WebSreverce_PostJson.ConvertToJson(temp); Data_json data_info = new Data_json(); data_info.dt = rundata; data_info.did = Constants.ENVINFO; rundata = WebSreverce_PostJson.ConvertToJson(data_info); string reponce = WebSreverce_PostJson.Post_Jsonstr(WebSreverce_PostJson.urladd_inn, rundata); //url表示数据服务器的地址及接口名称,可以定义成全局变量 // Console.WriteLine(reponce); if (reponce == "ac") { //Console.WriteLine(rundata); //返回值正确,服务器收到了请求 MySqlConnection sqlcon = new MySqlConnection(pdata.M_str_sqlcon); sqlcon.Open(); string comm4 = " delete from tb_env limit 1 "; //获取前1条数据 pdata.Delete(sqlcon, comm4); sqlcon.Close(); /*操作Processbar*/ if (db_progressBar.Value < db_progressBar.Maximum) { db_progressBar.Value += 1; this.pbar_state.Text = "上传进行中 [" + db_progressBar.Value.ToString() + "/" + db_progressBar.Maximum + "]...."; } else { this.pbar_state.Text = "上传已完成!"; if (MessageBox.Show("上传完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { return; } } } else { if (MessageBox.Show("与Web服务器连接异常,请检查后重试!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK) { return; } } } } } } } }
/*登陆系统,发送相关信息*/ private void LogIn_Enqueue(Int16 num) { string updatecom = string.Empty; //string NC_ID = dtGridView_NC.SelectedCells[Constants.TB_ID].Value.ToString(); //id在数据tb_nc的第三列 string NC_ID = ds.Tables[0].Rows[num - 1][2].ToString(); long temp = 0; Data_LogIn_info login_info = new Data_LogIn_info(); #if (MARCO_DEBUG) int ret32 = HncApi.HNC_SystemGetValue((int)HncSystem.HNC_SYS_SN_NUM, ref NC_ID, clientno); if (ret32 == 0) { // value64 = value64 / Move_unit * 1000 * 60; login_info.strNC_ID = NC_ID; } else { MessageBox.Show("登入失败"); } #else login_info.id = NC_ID; #endif if (CGlbFunc.Power_time_today[num] != null) { TimeSpan span = DateTime.Now - DateTime.Parse(CGlbFunc.Power_time_today[num]); //读取数据库的历史值 string comm = "select id,ontime from tb_login_local as t where not exists(select 1 from tb_login_local where id=t.id and dt>t.dt )and id='" + NC_ID + "'"; if (CGlbFunc.LoginFlag[num] == Constants.LOGINFLAG) { updatecom = "update tb_nc set state= '已登录' where ID_selfinc='" + num + "'"; } if (CGlbFunc.LoginFlag[num] == Constants.LOGOFFLAG) { updatecom = "update tb_nc set state= '未登录' where ID_selfinc='" + num + "'"; } MySqlDataReader reader = null; MySqlConnection sqlcon = new MySqlConnection(FormMain.MyDataBase.M_str_sqlcon); MySqlCommand cmd2 = new MySqlCommand(updatecom, sqlcon); try { sqlcon.Open(); MySqlCommand cmd = new MySqlCommand(comm, sqlcon); reader = cmd.ExecuteReader(); if (reader.Read()) { //Console.WriteLine(string.Format("{0},{1}",reader[0],reader[1])); temp = (long)reader[1]; } else { temp = 0; } reader.Close(); cmd2.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show("连接数据库失败"); } finally { cmd2.Dispose(); //sqlcon.Close(); } login_info.runtime = temp + (long)span.TotalSeconds; login_info.ontime = login_info.runtime; login_info.time = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //时间戳,实验室环境下可以用做开机时间参考 #region //本地存储 string insertcom = "insert into tb_login_local(id,ontime,runtime,dt) values('" + login_info.id + "','" + login_info.ontime + "','" + login_info.runtime + "','" + login_info.time + "')"; MySqlCommand cmd3 = new MySqlCommand(insertcom, sqlcon); cmd3.ExecuteNonQuery(); cmd3.Dispose(); sqlcon.Close(); #endregion //SetNCData(); FormMain.queue_login.Enqueue(login_info); } else { MessageBox.Show("系统未登录"); } }