注册 登录
编程论坛 C# 论坛

关于C#模糊查询结果无法显示

lukebc 发布于 2016-08-30 12:34, 1910 次点击
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication9
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "server=LUKE\\SQLEXPRESS;uid=sa;pwd=sa;database=luke";
            conn.Open();
            DataTable dt = new DataTable("resouce");
            dt.Clear();

            SqlDataAdapter adp = new SqlDataAdapter("select * from text1", conn);
            adp.Fill(dt);
            string sql ="";
            if (textBox1.Text == ""&& textBox2.Text=="")
            {
                sql = "select * from text1";
                label1.Text = sql;
            }
            else
            {
                sql = "select * from text1 where bh like '%" + textBox1.Text + "%'";
                label1.Text = sql;
            }


            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = command;
            ad.Fill(dt);
            dt.Clear();
            dataGridView1.DataSource = dt.DefaultView;
            
         
        }
查询的结果为空白  
只有本站会员才能查看附件,请 登录


当把第二个 dt.clear() 去掉后,可查询但其它内容也无法清空,结果不断累计,不清屏  
只有本站会员才能查看附件,请 登录

不知道怎么改
1 回复
#2
libin25198852016-08-31 11:27
给清空数据添加一个if()条件  当dgv中的内容不为空的时候,清空,当为空的时候  不执行if()结构中的条件
1