DataSet读取数据时,有出错,请帮忙看看!

qinzugan 2009-07-10 01:16:04
DataSet读取数据时,有出错,
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;
namespace ConsoleDataSetTest
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=WONDER_TAN_XP00;Initial Catalog=product;Persist Security Info=True;User ID=sa;Password=123");

SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from userss",thisConnection);

productDataSet thisDataSet = new productDataSet();

thisAdapter.Fill(thisDataSet,"userss");

foreach (DataRow theRow in thisDataSet.Tables["userss"].Rows)
{
Console.WriteLine(theRow["id"]+"\t"+theRow["name"]);
}
}
}
}


出错的信息是如下:
Error 1 The type or namespace name 'DataRow' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\agan_qin\My Documents\Visual Studio 2005\Projects\ConsoleDataSetTest\ConsoleDataSetTest\Program.cs 20 22 ConsoleDataSetTest

Error 2 Cannot apply indexing with [] to an expression of type 'DataRow' C:\Documents and Settings\agan_qin\My Documents\Visual Studio 2005\Projects\ConsoleDataSetTest\ConsoleDataSetTest\Program.cs 22 35 ConsoleDataSetTest

Error 3 Cannot apply indexing with [] to an expression of type 'DataRow' C:\Documents and Settings\agan_qin\My Documents\Visual Studio 2005\Projects\ConsoleDataSetTest\ConsoleDataSetTest\Program.cs 22 53 ConsoleDataSetTest



...全文
256 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhongmeiliuqing 2009-07-10
  • 打赏
  • 举报
回复
引入 System.Data命名空间,
zhiwenyue 2009-07-10
  • 打赏
  • 举报
回复
对啊,引入命名空间就好了!如果还不行的话,可以调试看看thisDataSet.Tables["userss"].Rows.Count 是不是有值。或者用thisDataSet.Tables0].Rows.Count 也可以!
hanhanmj 2009-07-10
  • 打赏
  • 举报
回复
引用using System.Data;命名空间就行了..

1楼正解!
fanlose2 2009-07-10
  • 打赏
  • 举报
回复
如果链接数据库没有错误的话,你可以改成这样-------
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=WONDER_TAN_XP00;Initial Catalog=product;Persist Security Info=True;User ID=sa;Password=123");

SqlDataAdapter thisAdapter = new SqlDataAdapter("select * from userss", thisConnection);

//productDataSet thisDataSet = new productDataSet();
System.Data.DataSet thisDataSet = new System.Data.DataSet();

thisAdapter.Fill(thisDataSet, "userss");

//foreach (DataRow theRow in thisDataSet.Tables["userss"].Rows)
foreach (System.Data.DataRow theRow in thisDataSet.Tables["userss"].Rows)
{
Console.WriteLine(theRow["id"] + "\t" + theRow["name"]);
}
}
dean615 2009-07-10
  • 打赏
  • 举报
回复
加上Using System.Data;
不过,这个错应该是编译的时候报的错吧
wangdaoren 2009-07-10
  • 打赏
  • 举报
回复
没有引用编译都应该不会通过的呀...
youlanbeilei 2009-07-10
  • 打赏
  • 举报
回复
lz用的不是vs环境吗,这个对命名空间应该有智能感知的
mathieuxiao 2009-07-10
  • 打赏
  • 举报
回复
没有引入System.Data;命名空间,所以系统不认识你的DataRow这个类,你可以像1楼说的那样做。或者在DataRow前面加上System.Data.DataRow
蒋晟 2009-07-10
  • 打赏
  • 举报
回复
没有using System.Data

110,546

社区成员

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

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

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