大家帮我看下这段代码,谢谢了

zhengbo19870409 2009-12-11 09:12:33
private void btnScore_Click(object sender, EventArgs e)
{
if (txtCard.Text == "")
{
MessageBox.Show("请输入你要查找的信用卡卡号");
}
else
{
string connstring = "server=WIN-8B49A4OVKBA\\SQLEXPRESS;database=Bank;uid=sa;pwd=19870409";
SqlConnection connection = new SqlConnection(connstring);
string sql = string.Format("select Sum(Cost) from Bank where CardNo='{0}'", txtCard.Text);
try
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader datareader = command.ExecuteReader();
if (datareader.HasRows)
{

float score = (float)datareader["Sum(Cost)"];
MessageBox.Show("您的信用卡积分为" + score + "分", "查询结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
}
在使用vs时,有点不会用聚合函数,请教大家下,看下这段代码有问题没?
...全文
137 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
好了,把完整代码贴出来,呵呵
private void btnScore_Click(object sender, EventArgs e)
{
if (txtCard.Text == "")
{
MessageBox.Show("请输入你要查找的信用卡卡号");
}
else
{
string connstring = "server=WIN-8B49A4OVKBA\\SQLEXPRESS;database=Bank;uid=sa;pwd=19870409";
SqlConnection connection = new SqlConnection(connstring);
string sql = string.Format("select Sum([Cost]) As cost from Bank where CardNo='{0}'", txtCard.Text.Trim());
try
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader datareader = command.ExecuteReader();
if (!datareader.HasRows)
{
MessageBox.Show("输入的卡号不存在");
}
else
{
if (datareader.Read())
{

int score = Convert.ToInt32(datareader["cost"]);
MessageBox.Show("您的信用卡积分为" + score + "分", "查询结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
datareader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();



}
}
}
datareader.hasrows和datareader.read()不能混用的,我用错了,并且类型转换的时候,只能
用convert.toint32,(int)和int.parse一用就出现无法转换
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
又出现了新的问题,出现对话框内容是,在没有任何数据时进行无效的读取尝试
wuyi8808 2009-12-11
  • 打赏
  • 举报
回复
-- 如果这样单个查询会对:
select [Cost] from Bank where ...

-- 那么这样求和就没问题,求和后列名还是 Cost:
select Sum([Cost]) as [Cost] from Bank where ...


gablfq 2009-12-11
  • 打赏
  • 举报
回复
提示你“什么什么聚合函数的错误”那应该是SQL语法错误
第一,你SQL语句 string sql = string.Format("select Sum(Cost) from Bank where CardNo='{0}'", txtCard.Text); 是没有错的,这样是可以用SUM函数的;

第二:
if (datareader.HasRows)
{

float score = (float)datareader["Sum(Cost)"];
MessageBox.Show("您的信用卡积分为" + score + "分", "查询结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

这里的语句:float score = (float)datareader["Sum(Cost)"]; 是有问题的,["Sum(Cost)"]并不是你表里的例,这是个函数;还有就是你上边的SQL语句求和后所出来的列并没有列名,所以你这里用列句是取不到值的,你可以这样改:
将上边的SQL语句改为:string sql = string.Format("select Sum(Cost) AS Cost from Bank where CardNo='{0}'", txtCard.Text);
然后将取值语句改为:float score = (float)datareader["Cost"];

zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wuyi8808 的回复:]
C# codestring sql=string.Format("select Sum([Cost]) as [Cost] from Bank where CardNo='{0}'", txtCard.Text.Trim());// ...float score= (float)datareader["Cost"];// 或者:float score=float.Parse(datareader[?-
[/Quote]
不对呀
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
不行呀
最后只弹出一个对话框只显示cost四个字母
wuyi8808 2009-12-11
  • 打赏
  • 举报
回复
string sql = string.Format("select Sum([Cost]) as [Cost] from Bank where CardNo='{0}'", txtCard.Text.Trim()); 
// ...
float score = (float)datareader["Cost"];
// 或者:
float score = float.Parse(datareader["Cost"].ToString());
小鹏 2009-12-11
  • 打赏
  • 举报
回复
CardNo='{0}',这里的单引号有问题吧!输入法不对!其它地方看不出错误来!
wuyi8808 2009-12-11
  • 打赏
  • 举报
回复
select Sum([Cost]) as [Cost] from Bank where CardNo='{0}'


(float)datareader["Cost"];
wuyi8808 2009-12-11
  • 打赏
  • 举报
回复
try:
select Sum([Cost]) from Bank where CardNo='{0}'
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
(float)datareader["Sum(Cost)"]; 这里面应该怎么写?
还要加sum么?
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
不行,最后弹出来一个对话框,内容是"sum(cost)",大家帮我看下
fengjian_428 2009-12-11
  • 打赏
  • 举报
回复
select Sum(Cost) from Bank where CardNo='{0}' group by CardNo"
你是说这个吧?
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
我又写了个循环结构的,问题可以解决,但是我想知道利用聚合函数应该怎么写,我先运行下看有没问题
平生我自如 2009-12-11
  • 打赏
  • 举报
回复
你还是运行一下吧! 这样很难看出来的! 有时候一个逗号就出错了
zhengbo19870409 2009-12-11
  • 打赏
  • 举报
回复
float score = (float)datareader["Sum(Cost)"];
这段代码不错吧?
mohugomohu 2009-12-11
  • 打赏
  • 举报
回复
没有
  • 打赏
  • 举报
回复
运行一下就知道了

111,120

社区成员

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

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

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