private void dateTimePicker1_ValueChanged(object sender, EventArgs e) //日期选择控件 { string date = dateTimePicker1.Value.ToShortDateString(); //取年月日 year = dateTimePicker1.Value.Year.ToString(); month = dateTimePicker1.Value.Month.ToString(); day = dateTimePicker1.Value.Day.ToString(); year1 = dateTimePicker1.Value.AddDays(-1).Year.ToString(); month1 = dateTimePicker1.Value.AddDays(-1).Month.ToString(); day1 = dateTimePicker1.Value.AddDays(-1).Day.ToString(); string date_t = year + "-" + month + "-" + day; string date_y = year1 + "-" + month1 + "-" + day1; gt.setToday(date_t); gt.setYesToday(date_y); gt.setWorkFace(working_face); //MessageBox.Show(date); }
private void button1_Click(object sender, EventArgs e) //基本信息保存预览 { string date = year + "-" + month + "-" + day; string date_y = year1 + "-" + month1 + "-" + day1; string constr = "server=192.168.1.111;database=UPRESSURE;uid=sa;pwd=sdkjdx"; string constr_test = "server=.;database=UPRESSURE;uid=sa;pwd=sakjdx"; //插入主键时间+工作面 string sqlString_ins = string.Format(@"IF NOT EXISTS(SELECT 1 FROM [UPRESSURE].[dbo].[基本数据表] WHERE [日期] = '{0}' AND [工作面] = N'{1}') BEGIN INSERT INTO [UPRESSURE].[dbo].[基本数据表]([日期], [工作面]) VALUES('{0}', N'{1}') END", date, working_face); //寻找插入数据的上一条数据 string sqlString_find = string.Format(@"SELECT TOP 1 * FROM [UPRESSURE].[dbo].[基本数据表] WHERE [工作面] LIKE N'{0}' AND [日期] < '{1}' ORDER BY [日期] DESC", working_face, date); using (SqlConnection sqlConnection = new SqlConnection(constr_test)) { bool conok = SqlExtensions.QuickOpen(sqlConnection, 5000); //连接到数据库 if (conok) { SqlCommand cmd_ins = new SqlCommand(sqlString_ins, sqlConnection); cmd_ins.ExecuteNonQuery(); DataTable datatable = new DataTable(); SqlCommand cmd_find = new SqlCommand(sqlString_find, sqlConnection); using (SqlDataAdapter da = new SqlDataAdapter(cmd_find)) { da.Fill(datatable); } //上一条记录进尺 double auxiliary_y = Convert.ToDouble(datatable.Rows[0][2].ToString()); double rubber_y = Convert.ToDouble(datatable.Rows[0][3].ToString()); //总进尺 auxiliary = (textBox1.Text == "") ? auxiliary_y : Convert.ToDouble(textBox1.Text); //辅 rubber = (textBox2.Text == "") ? rubber_y : Convert.ToDouble(textBox2.Text); //胶 double transport_avg = Math.Round((auxiliary + rubber) / 2, 1); //当日进尺 double auxiliary_td = Math.Round(auxiliary - auxiliary_y, 1); double rubber_td = Math.Round(rubber - rubber_y, 1); double transport_td_avg = Math.Round((auxiliary_td + rubber_td) / 2, 1); //涌水量 double water = (textBox3.Text == "") ? 0.0 : Convert.ToDouble(textBox3.Text); //DataRow dr = datatable.NewRow(); //object[] objs = { date, working_face, auxiliary, rubber, transport_avg, auxiliary_td, rubber_td, transport_td_avg, water, textBox4.Text, textBox5.Text, textBox6.Text, (2077 - transport_avg), textBox15.Text }; //dr.ItemArray = objs; //datatable.Rows.Add(dr); //datatable写入数据库 string sqlString_insdata = string.Format(@"UPDATE [UPRESSURE].[dbo].[基本数据表] SET [辅运顺槽总进尺] = '{0}', [胶运顺槽总进尺] = '{1}', [总进尺平均] = '{2}', [辅运当日进尺] = '{3}', [胶运当日进尺] = '{4}', [当日平均] = '{5}', [工作面涌水量] = '{6}', [初采时间] = N'{7}', [实测倾斜长度] = '{8}', [平均采高] = '{9}', [剩余推进长度] = '{10}', [时空关系] = N'{11}' WHERE [日期] = '{12}' AND [工作面] = N'{13}'", auxiliary, rubber, transport_avg, auxiliary_td, rubber_td, transport_td_avg, water, textBox4.Text, textBox5.Text, textBox6.Text, (2077 - transport_avg), textBox15.Text, date, working_face); SqlCommand cmd_insdata = new SqlCommand(sqlString_insdata, sqlConnection); cmd_insdata.ExecuteNonQuery(); Properties.Settings.Default.Save(); } } //charu gt.setToday(date); gt.setYesToday(date_y); bdt.Start(gt); MessageBox.Show("完成"); }