C# 查询EXCEL出了问题。。来帮忙解决下!!!

jackipzz 2012-04-27 11:00:22
我想做个查EXCEL表的程序,但是当我用我输入的数据查询时总是查不到。麻烦路过的高手帮个忙。!
运行的结果:





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;
using System.Data.OleDb;

namespace 自助单号
{
public partial class Form1 : Form
{
string dd ;
string path;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();

}

private void button1_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection Conn = new
System.Data.OleDb.OleDbConnection
(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'");
string strcom="select 姓名 from [Sheet1$] where 单号= '"+dd+"'";
Conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(strcom, Conn);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "[Sheet1$]");
// Clipboard.SetText(ds.Tables[0].Rows[0][0].ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Conn.Close();
return;
}
dataGridView1.DataSource = ds.Tables[0];
Conn.Close();

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{
IDataObject data = Clipboard.GetDataObject();
dd = (string)data.GetData(DataFormats.Text);
textBox2.Text = dd;
}

private void button2_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
path = ofd.FileName;
textBox1.Text = path;
}

private void button3_Click(object sender, EventArgs e)
{
Clipboard.Clear();
}
}
}

...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
熙风 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

终于找到答案了!!! string strcom="select 单号 from [Sheet1$] where 姓名= '"+textBox2.Text.Trim()+"'";
查询的语句要这样处理下!!!晕。搞了一个上午。。谢谢啦!!
[/Quote]

貌似这个sql跟你上面sql不一样,,查询条件和查询字段弄反了
jackipzz 2012-04-27
  • 打赏
  • 举报
回复
终于找到答案了!!! string strcom="select 单号 from [Sheet1$] where 姓名= '"+textBox2.Text.Trim()+"'";
查询的语句要这样处理下!!!晕。搞了一个上午。。谢谢啦!!
jackipzz 2012-04-27
  • 打赏
  • 举报
回复
我这个TIMER是不断来监听剪切板的。。
loveoflife0411 2012-04-27
  • 打赏
  • 举报
回复
private void timer1_Tick(object sender, EventArgs e)
{
IDataObject data = Clipboard.GetDataObject();
dd = (string)data.GetData(DataFormats.Text);
textBox2.Text = dd;
}

你的这个time不设置停止吗? 无限循环?
不是很明白,帮你顶下吧,求楼下吧

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧