数据库查询结果输出到datagridview(Winform/mdb/C#)

whydothis007 2011-07-15 10:19:15
(C# WinForm和Access .mdb)在做一个报表程序时遇到问题,想实现下图功能,将数据库中查询结果输出到datagridview中,下面也是从网上找的方法,但是不行。求教各位,怎么解决?直接在datagridview中查询也可,欢迎其他方法。


private void button1_Click(object sender, EventArgs e)
{
String timeid = textBox1.Text;
String sql = "select * from Details where TimeID=timeid";
OleDbCommand command = DB.getOleDbCommand(sql);
command.Parameters.AddWithValue("@TimeID",timeid);

try
{
command.Connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataSet DS = new DataSet();

adapter.SelectCommand = command;
adapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
}
catch (OleDbException o)
{
MessageBox.Show(o.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
command.Connection.Close();
}
}
...全文
165 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whydothis007 2011-07-25
  • 打赏
  • 举报
回复
不胜感激
feng9330 2011-07-18
  • 打赏
  • 举报
回复
...晕 老错

String timeid = textBox1.Text;
String sql = "select * from Details where TimeID=@TimeID";
OleDbCommand command = DB.getOleDbCommand(sql);
command.Parameters.AddWithValue("@TimeID",timeid);


feng9330 2011-07-18
  • 打赏
  • 举报
回复

String timeid = textBox1.Text;
String sql = "select * from Details where TimeID=TimeID";
OleDbCommand command = DB.getOleDbCommand(sql);
command.Parameters.AddWithValue("@TimeID",timeid);
feng9330 2011-07-18
  • 打赏
  • 举报
回复

String timeid = textBox1.Text;
String sql = "select * from Details where TimeID=@TimeID";
OleDbCommand command = DB.getOleDbCommand(sql);
command.Parameters.AddWithValue("@TimeID",timeid);


rlef 2011-07-18
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e)
{
String timeid = textBox1.Text;
String sql = "select * from Details where TimeID=timeid";
OleDbCommand command = DB.getOleDbCommand(sql);
command.Parameters.AddWithValue("@TimeID",timeid);

注意红色部分,你写错了
whydothis007 2011-07-15
  • 打赏
  • 举报
回复
小弟初来乍到,分数不多

111,119

社区成员

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

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

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