初学者:查询语句报错!请大家帮忙改错!

herohero55 2005-10-30 02:44:36
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient; 
 
namespace ConnectAccess
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

private void button8_Click(object sender, System.EventArgs e)
{
string source="server=localhost;uid=sa;pwd=;database=student";
string select="select * from score";
SqlConnection conn=new SqlConnection(source);
conn.Open();
SqlCommand cmd =new SqlCommand(select,conn);
SqlDataReader reader=cmd.ExecuteReader();
this.dataGrid1.DataSource = reader;
}

     }
}    
我在datagrid中显示,但是报错,错误信息如下:
编译通过了,运行时出对话框

未处理的“System.Exception”类型的异常出现在 system.windows.forms.dll 中。
其他信息: 复杂的 DataBinding 接受 IList 或 IListSource 作为数据源


请大家帮忙!
...全文
108 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
herohero55 2005-10-30
  • 打赏
  • 举报
回复
OK,谢谢大有,结帖!
xlfeiyu 2005-10-30
  • 打赏
  • 举报
回复
this.dataGrid1.DataSource = ds;
herohero55 2005-10-30
  • 打赏
  • 举报
回复
并不包含对Table的定义。


报此错
xlfeiyu 2005-10-30
  • 打赏
  • 举报
回复
private void button8_Click(object sender, System.EventArgs e)
{
string source="server=localhost;uid=sa;pwd=;database=student";
string select="select * from score";
SqlConnection conn=new SqlConnection(source);
SqlDataAdapter da = new SqlDataAdapter(select, conn);
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource = ds.Table[0];
}

     }
}    
herohero55 2005-10-30
  • 打赏
  • 举报
回复
应该怎么写呢?
我是初学,不是很会
zhy0101 2005-10-30
  • 打赏
  • 举报
回复
WinForm当中下列数据源有效:

DataTable
DataView
DataSet
DataViewManager
一维数组
任何实现 IListSource 接口的组件
任何实现 IList 接口的组件

不包括DataReader,使用DataSet
herohero55 2005-10-30
  • 打赏
  • 举报
回复
“System.Windows.Forms.DataGrid”并不包含对“DataBind”的定义

加上就出这个错误了。
jxufewbt 2005-10-30
  • 打赏
  • 举报
回复
在this.dataGrid1.DataSource = reader;后面加上
this.dataGrid1.DataBind();
看看

110,571

社区成员

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

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

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