它成功地从表中加载项目和图像,但是当我尝试搜索时,没有任何内容出现
这是我在表单加载事件(工作)上的代码
public void IL()
{
imageList1.Images.Clear();
listView1.Items.Clear();
listBox1.Items.Clear();
SqlDataReader dr;
cn.Open();
SqlCommand cmd = new SqlCommand("Select * from EnteryItem ", cn);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
try
{
var imageBytes = (byte[])dr["ItemImage"];
MemoryStream memStm = new MemoryStream(imageBytes);
memStm.Seek(0, SeekOrigin.Begin);
Image image = Image.FromStream(memStm);
imageList1.Images.Add(image);
if (dr["ItemName"].ToString() == "")
{
listBox1.Items.Add(dr["ItemBarcode"].ToString());
}
else
{
listBox1.Items.Add(dr["ItemName"].ToString());
}
}
catch
{
Bitmap bmp = new Bitmap(78, 78);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.Clear(Color.FromKnownColor(KnownColor.ControlDarkDark));
}
imageList1.Images.Add(bmp);
if (dr["ItemName"].ToString() == "")
{
listBox1.Items.Add(dr["ItemBarcode"].ToString());
}
else
{
listBox1.Items.Add(dr["ItemName"].ToString());
}
}
}
dr.Close();
this.imageList1.ImageSize = new Size(100, 100);
imageList1.ColorDepth = ColorDepth.Depth32Bit;
this.listView1.LargeImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
item.Text = listBox1.Items[j].ToString();
this.listView1.Items.Add(item);
}
}
cn.Close();
}
这是关于文本更改事件的搜索代码(这是问题)
private void search()
{
if (textBox2.Text == "")
{
IL();
}
imageList1.Images.Clear();
listView1.Items.Clear();
listBox1.Items.Clear();
SqlDataReader dr;
cn.Open();
SqlCommand cmd = new SqlCommand("Select * from EnteryItem WHERE ItemName = N'%" + textBox2.Text + "%' Or ItemBarcode = N'" + textBox2.Text + "' ", cn);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
try
{
var imageBytes = (byte[])dr["ItemImage"];
MemoryStream memStm = new MemoryStream(imageBytes);
memStm.Seek(0, SeekOrigin.Begin);
Image image = Image.FromStream(memStm);
imageList1.Images.Add(image);
if (dr["ItemName"].ToString() == "")
{
listBox1.Items.Add(dr["ItemBarcode"].ToString());
}
else
{
listBox1.Items.Add(dr["ItemName"].ToString());
}
}
catch
{
Bitmap bmp = new Bitmap(78, 78);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.Clear(Color.FromKnownColor(KnownColor.ControlDarkDark));
}
imageList1.Images.Add(bmp);
if (dr["ItemName"].ToString() == "")
{
listBox1.Items.Add(dr["ItemBarcode"].ToString());
}
else
{
listBox1.Items.Add(dr["ItemName"].ToString());
}
}
}
dr.Close();
this.imageList1.ImageSize = new Size(100, 100);
imageList1.ColorDepth = ColorDepth.Depth32Bit;
this.listView1.LargeImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
item.Text = listBox1.Items[j].ToString();
this.listView1.Items.Add(item);
}
}
cn.Close();
}
答案 0 :(得分:1)
首先:每次在文本框中输入内容时,都不要设置新图像,而是将它们保存在一个集合中,并将其添加到列表框中,只过滤掉部分内容,例如:listBox1.DataSource = SomeLocalCollection.Where(i=>i.ItemName.Contains(textBox2.Text)).ToList()
另外,使用调试器检查代码中哪个部分出错。
答案 1 :(得分:1)
据我所知,%
之类的sql和通配符仅适用于LIKE
运算符。