🌺梦🌹魇💤💤 429450898
宠辱不惊,闲看庭前花开花落;去留无意,漫随天外云卷云舒。
关注数: 56 粉丝数: 112 发帖数: 1,905 关注贴吧数: 5
萌新求助,刚学了不久,写了点代码,点查询按钮没反应,求大佬看 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace 管理 { public partial class Form1 : Form { private OleDbConnection connection; private string connectionString; public Form1() { InitializeComponent(); InitializeDatabaseConnection(); PopulateComboBoxes(); connection.Close(); } private void InitializeDatabaseConnection() { connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Administrator\\Desktop\\Database.mdb;Persist Security Info=False;"; connection = new OleDbConnection(connectionString); try { connection.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); connection = null; } } private void PopulateComboBoxes() { // 填充队长(队长名称)到 comboBox1 string query = "SELECT 队长名称 FROM 队长"; using (OleDbCommand command = new OleDbCommand(query, connection)) { using (OleDbDataReader reader = command.ExecuteReader()) { List<string> names = new List<string>(); while (reader.Read()) { names.Add(reader["队长名称"].ToString()); } comboBox1.DataSource = names; comboBox1.DisplayMember = "队长名称"; } } // 填充工程(工程类型)到 comboBox3 query = "SELECT 工程类型 FROM 工程"; using (OleDbCommand command = new OleDbCommand(query, connection)) { using (OleDbDataReader reader = command.ExecuteReader()) { List<string> names = new List<string>(); while (reader.Read()) { names.Add(reader["工程类型"].ToString()); } comboBox3.DataSource = names; comboBox3.DisplayMember = "工程类型"; } } // 填充区域(区域)到 comboBox2 query = "SELECT 区域 FROM 区域"; using (OleDbCommand command = new OleDbCommand(query, connection)) { using (OleDbDataReader reader = command.ExecuteReader()) { List<string> names = new List<string>(); while (reader.Read()) { names.Add(reader["区域"].ToString()); } comboBox2.DataSource = names; comboBox2.DisplayMember = "区域"; } } // 填充用户(名称)到 comboBox4 query = "SELECT 名称 FROM 用户表"; using (OleDbCommand command = new OleDbCommand(query, connection)) { using (OleDbDataReader reader = command.ExecuteReader()) { List<string> names = new List<string>(); while (reader.Read()) { names.Add(reader["名称"].ToString()); } comboBox4.DataSource = names; comboBox4.DisplayMember = "名称"; } } } private void CloseDatabaseConnection() { if (connection != null && connection.State == ConnectionState.Open) { connection.Close(); connection = null; } } private void Button1_Click(object sender, EventArgs e) { MessageBox.Show("没做好"); } private void Button3_Click(object sender, EventArgs e) { string query = "SELECT * FROM 图纸信息 WHERE 1=1"; using (OleDbCommand cmd = new OleDbCommand(query, connection)) { try { // 根据文本框和下拉框的输入构建查询条件 if (!string.IsNullOrEmpty(textBox1.Text)) { query += " AND 图纸名称 = @图纸名称"; } if (!string.IsNullOrEmpty(comboBox3.Text)) { query += " AND 类型 = @类型"; } if (!string.IsNullOrEmpty(comboBox2.Text)) { query += " AND 区域 = @区域"; } if (!string.IsNullOrEmpty(comboBox1.Text)) { query += " AND 队长 = @队长"; } // ... 可以继续为其他文本框和下拉框添加条件 ... cmd.Parameters.AddWithValue("@图纸名称", textBox1.Text); cmd.Parameters.AddWithValue("@类型", comboBox3.Text); cmd.Parameters.AddWithValue("@区域", comboBox2.Text); cmd.Parameters.AddWithValue("@队长", comboBox1.Text); // ... 为其他参数添加值 ... connection.Open(); OleDbDataReader reader = cmd.ExecuteReader(); dataGridView1.DataSource = reader; // 将查询结果绑定到DataGridView reader.Close(); connection.Close(); } catch (Exception ex) { // 处理异常,例如显示错误消息或记录错误日志 MessageBox.Show("查询过程中出现错误: " + ex.Message); } } } } }
1 下一页