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;
using System.Data.Sql;
namespace WindowsApplication1
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlconn = new SqlConnection("server=(local);database=xmgl;uid=sa;pwd=;Connect Timeout=30");
            sqlconn.Open();            if(checkBox1.Checked==true)
            {
                SqlCommand sqlcomm = new SqlCommand("update  money set hasmoney=hasmoney+1  where name='" + textBox1.Text + "'", sqlconn);
                sqlcomm.ExecuteNonQuery();
            }
            if (checkBox2.Checked == true)
            {
                SqlCommand sqlcomm = new SqlCommand("update money set hasmoney=hasmoney+2  where name='" + textBox1.Text + "'", sqlconn);
                sqlcomm.ExecuteNonQuery();
            }
            if(checkBox3.Checked==true)
            {
                SqlCommand sqlcomm = new SqlCommand("update money set hasmoney=hasmoney+4  where name='" + textBox1.Text + "'", sqlconn);
                sqlcomm.ExecuteNonQuery();
            }
            if(checkBox4.Checked==true)
            {
                SqlCommand sqlcomm = new SqlCommand("update money set hasmoney=hasmoneyr+8  where name='" + textBox1.Text + "'", sqlconn);
                sqlcomm.ExecuteNonQuery();
            }            sqlconn.Close();
           
            
        }        private void Form6_Load(object sender, EventArgs e)
        {
            int a = 0;
            SqlConnection sqlconn = new SqlConnection("server=(local);database=xmgl;uid=sa;pwd=;Connect Timeout=30");
            sqlconn.Open();
            SqlCommand sqlcomm = new SqlCommand("select hasmoney from money where name='小明'", sqlconn);
            SqlDataReader sqlread=sqlcomm.ExecuteReader();
            if(sqlread.Read())
            {
                a = Convert.ToInt32(sqlread[0].ToString());
                sqlread.Close();
                sqlconn.Close();
             }
               
                              }
    }
}

解决方案 »

  1.   

    if(checkBox4.Checked==true) 
                { 
                    SqlCommand sqlcomm = new SqlCommand("update money set hasmoney=hasmoneyr+8  where name='" + textBox1.Text + "'", sqlconn); 
                    sqlcomm.ExecuteNonQuery(); 
                } 
                if(sqlread.Read()) 
                { 
                    a = Convert.ToInt32(sqlread[0].ToString()); 
                    sqlread.Close(); 
                    sqlconn.Close(); 

                }                sqlread.Close(); 
                    sqlconn.Close(); 
                
    放在if结束后
      

  2.   

                    sqlread.Close();
                    sqlconn.Close(); 
    是不是放在if外面更好一些?起码sqlconn.Close(); 好像应该放在外面的
      

  3.   

                if(checkBox4.Checked==true) 
                { 
                    SqlCommand sqlcomm = new SqlCommand("update money set hasmoney=hasmoneyr+8  where name='" + textBox1.Text + "'", sqlconn); 
                    sqlcomm.ExecuteNonQuery(); 
                }hasmoney=hasmoneyr+8 是不是多个r
    还有sqlread时不要关闭连接(关了还连鸟?)
                if(sqlread.Read()) 
                { 
                    a = Convert.ToInt32(sqlread[0].ToString()); 
                    
                }                  sqlread.Close(); 
                    sqlconn.Close();