ArrayList绑定问题,代码如下

罗卜基斯 2011-08-08 11:09:28
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "select * from authors";

conn.Open();
cmd.Connection = conn;
SqlDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection);
ArrayList list = new ArrayList();
while (read.Read())
{
Author au = new Author();
au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
read.Close();
cmd.Dispose();
conn.Close();


但是显示的结果是这样的


AuthorID AuthorLastName AuthorFirstName AuthorPhone
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
求解答,是原因
...全文
145 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗卜基斯 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 tomegm 的回复:]

Author au = new Author();//为啥不这么写
for (int i = 0; i < dt.Rows.Count; i++)
{

au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2……
[/Quote]

汗~~~~一样的据结果,你那种方式虽说是放在循环外面,作为对象始终只有一个啊。在循环中是重新分配了内存空间来存储的。
罗卜基斯 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wxr0323 的回复:]

引用 12 楼 darrren2185 的回复:

List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
……

如果你数据库里的记录是不重复的 那么这个值必须改变。
[/Quote] 我用的Sql 2000 pubs数据库中的authors,没有重复的数据啊
学会思考 2011-08-09
  • 打赏
  • 举报
回复
Author au = new Author();//为啥不这么写
for (int i = 0; i < dt.Rows.Count; i++)
{

au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
学会思考 2011-08-09
  • 打赏
  • 举报
回复
哇哈哈,LZ英明啊,跟我犯同一个错误,把实例化的对象放在FOR循环里面,每一次循环都是一个新的对象,你说里面能有几条数据?
子夜__ 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 darrren2185 的回复:]

List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
……
[/Quote]
如果你数据库里的记录是不重复的 那么这个值必须改变。
罗卜基斯 2011-08-09
  • 打赏
  • 举报
回复
估计没有下文呢
罗卜基斯 2011-08-09
  • 打赏
  • 举报
回复
怎么没有解决啊
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = dt.Rows[i][0].ToString();
au.AuthorLastName = dt.Rows[i][1].ToString();
au.AuthorFirstName = dt.Rows[i][2].ToString();
au.AuthorPhone = dt.Rows[i][3].ToString();
list.Add(au); //每执行到这步的时候先前存的值也改变呢??
}
子夜__ 2011-08-08
  • 打赏
  • 举报
回复


直接复制你的

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string sql = "select * from authors";
dt = ReturnDataTable(sql);
List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = dt.Rows[i][0].ToString();
au.AuthorLastName = dt.Rows[i][1].ToString();
au.AuthorFirstName = dt.Rows[i][2].ToString();
au.AuthorPhone = dt.Rows[i][3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
}

}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
public static int ExecuteNonQuery(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "链接字符串";
cn.Open();
int value;
try
{
SqlCommand cmd = new SqlCommand(cmdtext, cn);
value = cmd.ExecuteNonQuery() > 0 ? 1 : 0;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
return value;
}


这样
子夜__ 2011-08-08
  • 打赏
  • 举报
回复
晕死。。。

Author au = new Author();
au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);

写错了 直接复制你的。。
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wxr0323 的回复:]

C# code
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string sql = "select * from authors";
dt = ReturnDataTable(sql);
L……
[/Quote]

你给的代码结局是一样的,也是

AuthorID AuthorLastName AuthorFirstName AuthorPhone
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
998-72-3567 Ringer Albert 801 826-0752
。。。。。。。。。。。。。。
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 q107770540 的回复:]

单步调试查看 SqlDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection);
的返回值
[/Quote]
返回值是对的,只是没执行循环的时候,list.add(author)中已经存储的author对象也发生改变
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
问题我读取Read对象时,就会显示重复的一条记录啊,上面显示的那样
q107770540 2011-08-08
  • 打赏
  • 举报
回复
单步调试查看 SqlDataReader read = cmd.ExecuteReader(CommandBehavior.CloseConnection);
的返回值
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();

此处代码中,read对象已经关闭,无法读取meta data数据,故会显示错误
子夜__ 2011-08-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 darrren2185 的回复:]

为什么不能直接从DataReader读取呢,而是要换成DataTable去做呢?
[/Quote]
都可以
罗卜基斯 2011-08-08
  • 打赏
  • 举报
回复
为什么不能直接从DataReader读取呢,而是要换成DataTable去做呢?
子夜__ 2011-08-08
  • 打赏
  • 举报
回复
确保数据库里没重复数据
子夜__ 2011-08-08
  • 打赏
  • 举报
回复
 protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string sql = "select * from authors";
dt = ReturnDataTable(sql);
List<Author> lt = new List<Author>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
Author au = new Author();
au.AuthorID = read[0].ToString();
au.AuthorLastName = read[1].ToString();
au.AuthorFirstName = read[2].ToString();
au.AuthorPhone = read[3].ToString();
list.Add(au);
}
GridView1.DataSource = list;
GridView1.DataBind();
}

}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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