You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.7 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DailyWorkManagementSystem
{
public partial class Form_DWR : Form
{
private String connString = "Server=localhost;Database=DailyWorkRecordDB;Trusted_Connection=True;";
public Form_DWR()
{
InitializeComponent();
}
private void Form_DWR_Load(object sender, EventArgs e)
{
dateTimePicker1_ValueChanged(sender, e);
}
private void save_btn_Click(object sender, EventArgs e)
{
//获取日期年月日
string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
//读取文本框内容
string morning = rTB_morning.Text;
string afternoon = rTB_afternoon.Text;
//连接数据库
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = $"Select count(*) from DWR where Date='{date}'";
//判断是否存在该日期的记录
int count = (int)cmd.ExecuteScalar();
if (count == 0)
{
cmd.CommandText = $"insert into DWR(Date,Morning,Afternoon) values('{date}','{morning}','{afternoon}')";
int result_insert = cmd.ExecuteNonQuery();
if (result_insert > 0)
{
MessageBox.Show("保存成功!");
}
conn.Close();
}
else
{
cmd.CommandText = $"update DWR set Morning='{morning}',Afternoon='{afternoon}' where Date='{date}'";
int result_edit = cmd.ExecuteNonQuery();
if (result_edit > 0)
{
MessageBox.Show("保存成功!");
}
conn.Close();
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
//清空文本框
rTB_morning.Text = "";
rTB_afternoon.Text = "";
//获取日期年月日
string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
//连接数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=localhost;Database=DailyWorkRecordDB;Trusted_Connection=True;";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = $"select * from DWR where Date='{date}'";
SqlDataReader reader = cmd.ExecuteReader();
//读取数据
while (reader.Read())
{
rTB_morning.Text = reader["Morning"].ToString();
rTB_afternoon.Text = reader["Afternoon"].ToString();
}
conn.Close();
}
private void before_btn_Click(object sender, EventArgs e)
{
//更改日期到前一天
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
}
private void after_btn_Click(object sender, EventArgs e)
{
//更改日期到后一天
dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
}
//清空按键-上午
private void clear_btn1_Click(object sender, EventArgs e)
{
rTB_morning.Text = "";
}
//清空按键-下午
private void clear_btn2_Click(object sender, EventArgs e)
{
rTB_afternoon.Text = "";
}
}
}