一个简单的关于datatable的问题

yixian2007 2009-03-04 09:10:01
string sql = "select * from g_grade where archment = '优'";
SqlDataAdapter da = new SqlDataAdapter(sql,con.conn);
da.Fill(ds,"grade");

DataTable dt = ds.Tables["grade"];
foreach (DataRow dr in dt.Rows)
{
if ( dr["archment"] == "优")
dr["archment"] = "90";
}

求大家帮忙!
背景:archment列为成绩列,nvarchar方式存储,其中有"优","良","中","及格","不及格",以及数字
目的:作比较,选择同门课成绩最高的记录.
我用的方法是将archment列中值为"优"的数据改为"90","良"的数据改为"80",以方便做比较.

现在红色字体部分,值为false,IF里面的语句无法执行.请帮忙看看.

如果谁有更好的为成绩比大小的方法,小弟感激不尽,另有加分.
...全文
97 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixian2007 2009-03-04
  • 打赏
  • 举报
回复
foreach ( DataRow drarch in dt2.Rows )
foreach (达式DataRow dr in dt.Rows )
{
if ( dr["archment"].ToString() == drarch["degreename"].ToString() )
dr["archment"] = drarch["realarch"].ToString();
}

dt2 11 优 90 2008-9-10 8:09:08 johnbean
12 良 80 2008-9-10 8:09:03 johnbean
13 中 70 2008-9-10 8:08:57 johnbean
14 及格 60 2008-9-10 8:08:50 johnbean
15 不及格 55 2007-7-24 21:45:05 johnbean
16 作弊 0 2000-7-10 9:16:24 johnbean
17 缺考 0 2000-7-10 9:18:04 johnbean
18 缓考 0 2000-7-10 9:18:33 johnbean

用这种方法解决了,*_*
iGouzy 2009-03-04
  • 打赏
  • 举报
回复
PS: 建议使用 dr["archment"].ToString().Equals("优");
yixian2007 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wolf1118baby 的回复:]
引用 2 楼 nextday 的回复:
dr["archment"].ToString()试一下,而且比成绩大小在数据库中用SQL来写应该是更方便。

照这个来,
但在Sql中就可以用语句直接修改呀,或者写一个简单的存储过程不就简单了吗?
[/Quote]

不能改动原有的数据库,所以我必须另建一个存储表
yixian2007 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ljhcy99 的回复:]

nvarchar 类型有关系,否则dr["archment"] 是能 取得值得。

要仅仅选择同门课成绩最高的记录
那么可以修改sql
select max(成绩),课程 from g_grade group by 课程
[/Quote]

我试过了,如果用SQL,

archment >= 60 会把"不及格""缓考""作弊"等数据也选上.

也就是说在nvarchar类型下,根本没法做比较,用SQL语言无法实现上述功能,我用SQL也是将其改变为数字类型后可得.

select studid,courid,reupflag,archment into #tmp0 from g_grade
update #tmp0 set archment='0' where archment='缺考' or archment='不及格' or archment='缓考' or archment='作弊'
update #tmp0 set archment='60' where archment='优' or archment='良' or archment='中' or archment='及格'

select studid,courid,cast(archment as float) as archment into #tmp3 from #tmp0
select distinct studid,courid,archment into #tmp4 from #tmp3 as a where not exists (select b.* from #tmp3 as b where a.studid=b.studid and a.courid=b.courid and a.archment<b.archment)
order by a.studid

select a.classname,a.studid,a.studname,b.courid,b.courname,b.courcredit,c.archment into #tmp5 from #tmp4 as c,e_student as a,r_course as b
where a.studid=c.studid and b.courid=c.courid and a.classname like 'KT5%' and c.archment<60
order by b.courid

select a.classname,a.studid,a.studname,b.courid,b.courname,b.courcredit,c.archment into #tmp6 from #tmp4 as c,e_student as a,r_course as b
where a.studid=c.studid and b.courid=c.courid and a.classname like 'KT654%' and c.archment<60
order by b.courid

select * from #tmp6 where not exists
(
select * from r_course where r_course.courid= #tmp6.courid and r_course.courid like '00%'
)

select * from #tmp5 where not exists
(
select * from r_course where r_course.courid= #tmp5.courid and r_course.courid like '00%'
)



以上可实现,但不能动态改变,我打算将其定性为动态控制
即"优"可以设定相当于多少分.

暂时来看,如果在内存中将记录改变,并作比较较好.

另回二楼:
dr["archment"].ToString() == "优"
无value属性.
尝试可用.
wolf1118baby 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nextday 的回复:]
dr["archment"].ToString()试一下,而且比成绩大小在数据库中用SQL来写应该是更方便。
[/Quote]
照这个来,
但在Sql中就可以用语句直接修改呀,或者写一个简单的存储过程不就简单了吗?
che2piaopiao 2009-03-04
  • 打赏
  • 举报
回复
if ( dr["archment"] == "优")
dr["archment"] = "90";




dr["archment"].tostring()


要转换字符串
ljhcy99 2009-03-04
  • 打赏
  • 举报
回复

nvarchar 类型有关系,否则dr["archment"] 是能 取得值得。

要仅仅选择同门课成绩最高的记录
那么可以修改sql
select max(成绩),课程 from g_grade group by 课程
ProjectDD 2009-03-04
  • 打赏
  • 举报
回复
DataRow[i] 索引返回的是object 象楼上说的 转成 ToString()就可以比较了.
pc_funning 2009-03-04
  • 打赏
  • 举报
回复
你已经selec出来优的了,直接去改就行了
nextday 2009-03-04
  • 打赏
  • 举报
回复
dr["archment"].ToString()试一下,而且比成绩大小在数据库中用SQL来写应该是更方便。
ztenv 2009-03-04
  • 打赏
  • 举报
回复
if(dr["archment"].value.tostring() == "优") //貌似.

111,126

社区成员

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

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

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