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 调度窗口 { public partial class Form1 : Form { public Form1() { InitializeComponent(); LoadData(); } private void button1_Click(object sender, EventArgs e) { string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;"; SqlConnection con = new SqlConnection(constr); con.Open(); string sql1 = "select * from Table_1"; SqlDataAdapter da = new SqlDataAdapter(sql1, con); DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; //listBox1.DataSource = dt; con.Close(); } private void button2_Click(object sender, EventArgs e) //开始查询 { string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;"; SqlConnection con = new SqlConnection(constr); con.Open(); string sql1 = null; if (comboBox1.Text == "条码") { sql1 = $"select * from Table_1 where 条码 ='" + textBox1.Text + "'"; } else if (comboBox1.Text == "调度任务索引") { sql1 = $"select * from Table_1 where 调度任务索引 ='" + textBox1.Text + "' "; } SqlDataAdapter da = new SqlDataAdapter(sql1, con); DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; con.Close(); } private SqlConnection c; private SqlDataAdapter DA; private DataTable DT; private DataSet dataSet2; private void LoadData() { string c = "Server=localhost;Database=Test;Integrated Security=True;"; SqlConnection c1 = new SqlConnection(c); dataSet2 = new DataSet(); c1.Open(); string query = "select* from Table_1"; DA = new SqlDataAdapter(query, c); DT = new DataTable(); DA.Fill(dataSet2, "Table_1"); dataGridView1.DataSource = DT; c1.Close(); } private DataSet dataSet1; private SqlDataAdapter dataAdapter1; private void 任务完成ToolStripMenuItem1_MouseUp(object sender, MouseEventArgs e) { string con = "Server=localhost;Database=Test;Integrated Security=True;"; SqlConnection conn = new SqlConnection(con); dataSet1 = new DataSet(); conn.Open(); dataAdapter1 = new SqlDataAdapter("select * from Table_1", conn); dataAdapter1.Fill(dataSet1, "Table_1"); dataGridView1.DataSource = dataSet1.Tables["Table_1"]; SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter1); if (dataGridView1.CurrentCell.RowIndex != null) //CurrentRow { // 获取选中行内容 DataRow row = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row; // 获取任务索引 String index = (String)row["条码"]; // 构建删除语句 string finstr = $"update Table_1 set 任务状态 = '已完成' where 条码 = '{index}';"; using (SqlConnection connection3 = new SqlConnection(con)) { SqlCommand command = new SqlCommand(finstr, conn); command.Parameters.AddWithValue("@条码", index); int rowsAffected = command.ExecuteNonQuery(); conn.Close(); connection3.Open(); if (rowsAffected > 0) { // 删除成功,移除 DataRow row.Delete(); // 重新加载数据 dataSet1.Tables["Table_1"].AcceptChanges(); dataGridView1.DataSource = dataSet1.Tables["Table_1"]; connection3.Close(); } } } } private DataSet dataSet; private SqlDataAdapter dataAdapter; private void 任务删除ToolStripMenuItem1_MouseUp(object sender, MouseEventArgs e) { // 初始化数据库连接和数据适配器 string connectionString = "Server=localhost;Database=Test;Integrated Security=True;"; SqlConnection connection = new SqlConnection(connectionString); dataSet = new DataSet(); connection.Open(); dataAdapter = new SqlDataAdapter("select * from Table_1",connection); dataAdapter.Fill(dataSet,"Table_1"); // 将数据绑定到 DataGridView dataGridView1.DataSource = dataSet.Tables["Table_1"]; // 使用 SqlCommandBuilder 生成 SQL 命令 SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); // 获取当前选中的行 if (dataGridView1.CurrentRow != null) { // 获取选中行的 DataRow DataRow row = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row; // 获取任务ID String id = (String)row["条码"]; // 构建删除语句 string deleteQuery = $"delete from Table_1 where 条码 = '{id}' "; using (SqlConnection connection1 = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(deleteQuery, connection); command.Parameters.AddWithValue("@条码", id); int rowsAffected = command.ExecuteNonQuery(); LoadData(); //if (rowsAffected > 0) //{ // // 删除成功,移除 DataRow // row.Delete(); // // 重新加载数据 // dataSet.Tables["Table_1"].AcceptChanges(); // dataGridView1.DataSource = dataSet.Tables["Table_1"]; // connection.Close(); //} } } } } }