高分问一道题,请大家都来看一下
图在左边的,代码如下, 里面的正确选项则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 + "分";
}
}
} --------------------编程问答-------------------- 是不是SUM要清一下零 --------------------编程问答-------------------- 不需要,不管SUM的事 --------------------编程问答-------------------- 什么意思?代码中有四句
MessageBox.Show("成功");--------------------编程问答-------------------- this.radioButton4.Text
换成 radioButton.SelectedItems.Text呢 --------------------编程问答-------------------- radioButton是不包含SelectedItems的定义的 --------------------编程问答-------------------- 楼主你的四段代码可以用下面这一段来代替
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#