📄 jl_car_acc.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace CarManage
{
public partial class JL_Car_Acc : Form
{
public JL_Car_Acc()
{
InitializeComponent();
}
private void label19_Click(object sender, EventArgs e)
{
this.Close();
}
#region 创建gridviewcar()方法调用所有库中信息
private void gridviewcar()
{
//Car.connection(string sql);
//调用config配置文件对象dl连接对象
string dl = ConfigurationManager.AppSettings["sqlString"];
//创建connection对象con进行连接
SqlConnection con = new SqlConnection(dl);
//打开连接
con.Open();
//创建sql语句将信息从库中调出
string sql = "select accident_id as 序号,accident_number as 车牌号码,accident_time as 事故时间,accident_name as 肇事司机,accident_reason as 事故原因,accident_money as 赔偿金额,accident_remark as 备注 from db_accident";
//创建执行对象cmd
SqlCommand cmd = new SqlCommand(sql, con);
//创建读取器对象sdr
SqlDataAdapter sdr = new SqlDataAdapter();
//sdr的查询方法为cmd
sdr.SelectCommand = cmd;
//创建一个dataset容器
DataSet ds = new DataSet();
//填充数据ds
sdr.Fill(ds);
//绑定datagriview数据源为ds
this.GridViewCarAcc.DataSource = ds.Tables[0].DefaultView;
//关闭连接
con.Close();
this.textAccCarNumber.Clear();
this.textAccUserName.Clear();
this.textAccRepairReson.Clear();
this.textAccCarMoney.Clear();
this.textCarAccRemark.Clear();
GridViewCarAcc.Columns[0].Width = 80;
GridViewCarAcc.Columns[1].Width = 80;
GridViewCarAcc.Columns[2].Width = 80;
GridViewCarAcc.Columns[3].Width = 80;
GridViewCarAcc.Columns[4].Width = 80;
GridViewCarAcc.Columns[5].Width = 80;
GridViewCarAcc.Columns[6].Width = 80;
}
#endregion
#region 添加按钮
private void btnCarAccAdd_Click(object sender, EventArgs e)
{
string AccCarNumber = this.textAccCarNumber.Text.ToString().Trim();
DateTime TimeAcc = Convert.ToDateTime(this.dateTimeAcc.Value.ToShortDateString());
string AccUserName = this.textAccUserName.Text.ToString().Trim();
string AccRepairReson = this.textAccRepairReson.Text.ToString().Trim();
string AccCarMoney = this.textAccCarMoney.Text.ToString().Trim();
string CarAccRemark = this.textCarAccRemark.Text.ToString().Trim();
if (AccCarNumber != "")
{
if (AccUserName != "")
{
if (AccRepairReson != "")
{
if (AccCarMoney != "")
{
if (CarAccRemark != "")
{
string sql = "insert into db_accident(accident_number,accident_name,accident_reason,accident_money,accident_time,accident_remark)values('" + AccCarNumber + "','" + AccUserName + "','" + AccRepairReson + "','" + AccCarMoney + "','" + TimeAcc + "','" + CarAccRemark + "')";
Car.ADU(sql);
//调用gridviewcar方法
this.gridviewcar();
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("请将信息添加完整 ");
}
}
else
{
MessageBox.Show("请将信息添加完整 ");
}
}
else
{
MessageBox.Show("请将信息添加完整 ");
}
}
else
{
MessageBox.Show("请将信息添加完整");
}
}
else
{
MessageBox.Show("请将信息添加完整");
}
}
#endregion
#region 删除按钮
private void btnCarAccDelete_Click(object sender, EventArgs e)
{
string AccCarNumber = this.textAccCarNumber.Text.ToString().Trim();
DateTime TimeAcc = Convert.ToDateTime(this.dateTimeAcc.Value.ToShortDateString());
string AccUserName = this.textAccUserName.Text.ToString().Trim();
string AccRepairReson = this.textAccRepairReson.Text.ToString().Trim();
string AccCarMoney = this.textAccCarMoney.Text.ToString().Trim();
string CarAccRemark = this.textCarAccRemark.Text.ToString().Trim();
DialogResult drm = MessageBox.Show("确定要删除吗?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (AccCarNumber != "")
{
if (AccUserName != "")
{
if (AccRepairReson != "")
{
if (AccCarMoney != "")
{
if (CarAccRemark != "")
{
if (drm == DialogResult.OK)
{
//获取选中的行的信息
object row = (object)this.GridViewCarAcc.CurrentRow.Cells[0].Value.ToString().Trim();
//创建sql语句将信息从库中调出
string sql = "delete db_accident where accident_id = '" + row + "'";
Car.ADU(sql);
this.gridviewcar();
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("请选择要删除的选项 ");
}
}
else
{
MessageBox.Show("请选择要删除的选项 ");
}
}
else
{
MessageBox.Show("请选择要删除的选项 ");
}
}
else
{
MessageBox.Show("请选择要删除的选项");
}
}
else
{
MessageBox.Show("请选择要删除的选项");
}
}
}
#endregion
#region 显示选中行信息
private void GridViewCarAcc_CellClick(object sender, DataGridViewCellEventArgs e)
{
//获取选中的行的信息
this.textAccCarNumber.Text = GridViewCarAcc.CurrentRow.Cells[1].Value.ToString();
this.dateTimeAcc.Value = Convert.ToDateTime(GridViewCarAcc.CurrentRow.Cells[2].Value.ToString());
this.textAccUserName.Text = GridViewCarAcc.CurrentRow.Cells[3].Value.ToString();
this.textAccRepairReson.Text = GridViewCarAcc.CurrentRow.Cells[4].Value.ToString();
this.textAccCarMoney.Text = GridViewCarAcc.CurrentRow.Cells[5].Value.ToString();
this.textCarAccRemark.Text = GridViewCarAcc.CurrentRow.Cells[6].Value.ToString();
}
#endregion
private void JL_Car_Acc_Load(object sender, EventArgs e)
{
this.gridviewcar();
}
#region 修改按钮
private void btnCarAccUpdate_Click(object sender, EventArgs e)
{
string AccCarNumber = this.textAccCarNumber.Text.ToString().Trim();
DateTime TimeAcc = Convert.ToDateTime(this.dateTimeAcc.Value.ToShortDateString());
string AccUserName = this.textAccUserName.Text.ToString().Trim();
string AccRepairReson = this.textAccRepairReson.Text.ToString().Trim();
string AccCarMoney = this.textAccCarMoney.Text.ToString().Trim();
string CarAccRemark = this.textCarAccRemark.Text.ToString().Trim();
DialogResult drm = MessageBox.Show("确定要修改吗?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (AccCarNumber != "")
{
if (AccUserName != "")
{
if (AccRepairReson != "")
{
if (AccCarMoney != "")
{
if (CarAccRemark != "")
{
if (drm == DialogResult.OK)
{
//获取选中的行的信息
object row = (object)this.GridViewCarAcc.CurrentRow.Cells[0].Value.ToString().Trim();
//创建sql语句将信息从库中调出
string sql = "update db_accident set accident_number ='" + AccUserName + "', accident_name ='" + AccUserName + "' , accident_reason='" + AccRepairReson + "',accident_money = '" + AccCarMoney + "',accident_time = '" + TimeAcc + "',accident_remark ='" + CarAccRemark + "' where accident_id ='" + row + "'";
Car.ADU(sql);
this.gridviewcar();
MessageBox.Show("修改成功");
}
else
{
MessageBox.Show("请选择要修改的选项 ");
}
}
else
{
MessageBox.Show("请选择要修改的选项 ");
}
}
else
{
MessageBox.Show("请选择要修改的选项 ");
}
}
else
{
MessageBox.Show("请选择要修改的选项");
}
}
else
{
MessageBox.Show("请选择要修改的选项");
}
}
}
#endregion
#region 查询按钮
private void btnCarAccCheck_Click(object sender, EventArgs e)
{
string CheckAccCarNumber = this.textCheckAccCarNumber.Text.ToString().Trim();
DateTime TimeCheckAcc = Convert.ToDateTime(this.dateTimeCheckAcc.Value.ToShortDateString());
string CheckAccCarName = this.textCheckAccCarName.Text.ToString().Trim();
string sqlstr ="select accident_id as 序号,accident_number as 车牌号码,accident_time as 事故时间,accident_name as 肇事司机,accident_reason as 事故原因,accident_money as 赔偿金额,accident_remark as 备注 from db_accident where 1 = 1";
//调用config配置文件对象dl连接对象
string dl = ConfigurationManager.AppSettings["sqlString"];
//创建connection对象con进行连接
SqlConnection con = new SqlConnection(dl);
//打开连接
con.Open();
if (dateTimeCheckAcc.Checked)
{
sqlstr = sqlstr + "and accident_time = '" + TimeCheckAcc + "'";
}
if (CheckAccCarNumber != "")
{
sqlstr = sqlstr + "and accident_number = '" + CheckAccCarNumber + "'";
}
if (CheckAccCarName != "")
{
sqlstr = sqlstr + "and accident_name = '" + CheckAccCarName + "'";
}
SqlCommand cmd = new SqlCommand(sqlstr, con);
//创建读取器对象sdr
SqlDataAdapter sdr = new SqlDataAdapter();
//sdr的查询方法为cmd
sdr.SelectCommand = cmd;
//创建一个dataset容器
DataSet ds = new DataSet();
//填充数据ds
sdr.Fill(ds);
//绑定datagriview数据源为ds
this.GridViewAccCheckResult.DataSource = ds.Tables[0].DefaultView;
//关闭连接
con.Close();
}
#endregion
#region 清空按钮
private void btnCarAccClear_Click(object sender, EventArgs e)
{
this.textCheckAccCarNumber.Clear();
this.textCheckAccCarName.Clear();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -