//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd="); sql = "select * from name where name='"+ (sender as RadioButton).Text+"'"; try { tion.Open(); and = new SqlCommand(sql, tion); rd = and.ExecuteReader(); if (rd.Read()) { MessageBox.Show("成功"); sum = sum + 25; } } catch (SqlException ex) { MessageBox.Show(ex.Message); } finally { tion.Close(); rd.Close(); and.Dispose(); } }
写了四段重复的代码,为什么不提取重用呢
是根据名字在数据库中找选中名字的全部记录,找到一条记录显示“成功”,然后sum+25,但是你每算一个人应该把sum清零啊,要不算完张三,算李四,但是此时sum是张三的sum,没有清零啊
现在是表中有几个张三就显示几回“成功”,显示有什么用吗,应该是调试的时候写的吧
再label中显示结果就可以了吧1、建议把计算总分提取重用
2、计算之前要清零
{
//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
sql = "select * from name where name='"+ (sender as RadioButton).Text+"'"; try
{
tion.Open();
and = new SqlCommand(sql, tion);
rd = and.ExecuteReader();
if (rd.Read())
{
MessageBox.Show("成功");
sum = sum + 25;
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally {
tion.Close();
rd.Close();
and.Dispose();
}
}