本文介绍了我怎样才能减少代码........的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人请提出想法如何减少搜索代码......Hi ,anyone please give idea How can i reduce search code......private void cmdsearch_Click(object sender, EventArgs e) { if (comboBox1.Text=="Name") { SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where name='" + textBox3.Text + "'", conn); DataTable tbl = new DataTable(); adp.Fill(tbl); dataGridView1.DataSource = tbl; } else if (comboBox1.Text == "ID") { SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id='" + int.Parse(textBox3.Text) + "'", conn); DataTable tbl = new DataTable(); adp.Fill(tbl); dataGridView1.DataSource = tbl; }else if (comboBox1.Text == "Contactno") { SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where contactno='" + textBox3.Text + "'", conn); DataTable tbl = new DataTable(); adp.Fill(tbl); dataGridView1.DataSource = tbl; }else if (comboBox1.Text == "Post") { SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where post='" + textBox3.Text+ "'", conn); DataTable tbl = new DataTable(); adp.Fill(tbl); dataGridView1.DataSource = tbl; }推荐答案private void cmdsearch_Click(object sender, EventArgs e){SqlDataAdapter adp = new SqlDataAdapter(string.Format("select * from test1 where {0}='{1}'", comboBox1.Text, textBox3.Text), conn);//DataTable tbl = new DataTable();adp.Fill(tbl);dataGridView1.DataSource = tbl;}private void cmdsearch_Click(object sender, EventArgs e) { switch (comboBox1.Text.ToLower()) { case "name": case "id": case "contactno": case "post": SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM test1 WHERE " + comboBox1.Text + "=@PAR", conn); adp.SelectCommand.Parameters.AddWithValue("@PAR", textBox3.Text); DataTable tbl = new DataTable(); adp.Fill(tbl); dataGridView1.DataSource = tbl; break; } }SqlDataAdapter adp = new SqlDataAdapter(sql, conn);DataTable tbl = new DataTable();adp.Fill(tbl);dataGridView1.DataSource = tbl; 这篇关于我怎样才能减少代码........的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 01:12
查看更多