SQL,SQLite多条件搜索

以下是核心代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 多条件搜索
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from biao ");
            string txt1 = textBox1.Text;
            string txt2 = textBox2.Text;
            string txt3 = textBox3.Text;
            if (txt1 != "")
            {
                list.Add("txt1=" + txt1);
            }
            if (txt2 != "")
            {
                list.Add("txt2=" + txt2);
            }
            if (txt3 != "")
            {
                list.Add("txt3="+txt3);
            }
            if (list.Count > 0)
            {
                sql.Append(" where ");
                string ss = string.Join(" and ", list.ToArray());
                sql.Append(ss);
            }
            //sql.ToString()是获得多条件搜索的SQL语句
            MessageBox.Show(sql.ToString());
        }
    }
}

评论