高分问一道题,请大家都来看一下
图在左边的,代码如下, 里面的正确选项则C都选中C的时候会弹出"成功",但是你马上去点A或者B或者C的时候也会弹出成功,请问这个怎么解决呀using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
private SqlConnection tion;
private SqlCommand and;
private string sql;
private SqlDataReader rd;
private int sum = 0;
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
sql = "select * from name where name='"+this.radioButton1.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();
}
}
private void Form1_Load(object sender, EventArgs e)
{
tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
//tion.Open();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
sql = "select * from name where name='" + this.radioButton2.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.Dispose();
and.Dispose();
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
sql = "select * from name where name='" + this.radioButton3.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();
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
//tion = new SqlConnection("server=.;database=ddt;uid=sa;pwd=");
sql = "select * from name where name='" + this.radioButton4.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();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = "你的成绩是" + sum + "分";
}
}
} --------------------编程问答-------------------- 什么意思? 代码中有四句
MessageBox.Show("成功");--------------------编程问答-------------------- 需求,你的需求是什么呢
写了四段重复的代码,为什么不提取重用呢
是根据名字在数据库中找选中名字的全部记录,找到一条记录显示“成功”,然后sum+25,但是你每算一个人应该把sum清零啊,要不算完张三,算李四,但是此时sum是张三的sum,没有清零啊
现在是表中有几个张三就显示几回“成功”,显示有什么用吗,应该是调试的时候写的吧
再label中显示结果就可以了吧
1、建议把计算总分提取重用
2、计算之前要清零 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主你的四段代码可以用下面这一段来代替
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
//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();
}
}
补充:.NET技术 , C#