| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 448 人关注过本帖
标题:额。。。高手们,解答下
取消只看楼主 加入收藏
ainiak110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2012-10-31
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
额。。。高手们,解答下
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
    public partial class Form4 : Form
    {
        SqlConnection con;
        
        SqlCommand cmd;
        public Form4()
        {
            InitializeComponent();
        }
 
        private void Form4_Load(object sender, EventArgs e)
        {
            this.InitData();
            
            cmd = new SqlCommand("select distinct ID from Book order by ID", con);
            SqlDataReader reader = cmd.ExecuteReader();
            id.Items.Clear();
            while (reader.Read())
                id.Items.Add(reader[0]);
            reader.Close();
            
        }
 
        private void InitData()
        {
            try
            {
                con = new SqlConnection("server=.;database=BookStore;uid=sa;pwd=saa");
                cmd = new SqlCommand();
                con.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库无法访问:" + ex.Message, "警告");
            }
           
            
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            
            if (id.Text.Trim() != "" && name.Text.Trim() != "" && press.Text.Trim() != "" && price.Text.Trim() != "" && barcode.Text.Trim() != "" && num.Text.Trim() != "" && isbn.Text.Trim() != "" && author.Text.Trim() != "" && time.Text.Trim() != "" && yeshu.Text.Trim() != "" && cmbCD.Text.Trim()!="")
            {
                  
            }
            
            else if(name.Text.Trim().Length>0)
            {
               
                Name from Book";
                SqlDataReader reader =  cmd.ExecuteReader();
                while (reader.Read())
                {
                    if (name.Text.Trim() == reader["Name"].ToString().Trim() && num.Text.Trim() == "")
                    {
                        MessageBox.Show("此图书已有,请直接输入入库数量!", "提示");
                        num.Focus();
                        return;
                    }
                    else if (name.Text.Trim() == reader["Name"].ToString().Trim() && num.Text.Trim().Length > 0)
                    {
                        StringBuilder sb = new StringBuilder("update Book set storage+='" + num.Text + "'where Name='" + name.Text + "'");
                         = sb.ToString();
                        try
                        {
                            if (cmd.ExecuteNonQuery() > 0)
                            {
                                MessageBox.Show("增加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 
                            }
                            else
                            {
                                MessageBox.Show("未增加任何新记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
 
                            }
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show("数据库访问错误:" + exp.Message);
                        }
                    }
                }
                reader.Close();
               
            }
        }      
    }
}


我输入入库数量后点击入库就报错了~

每次运行到红色标记处就出现: 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。 我关闭了的,郁闷死了....这是为什么啊

[ 本帖最后由 ainiak110 于 2012-11-28 08:35 编辑 ]
搜索更多相关主题的帖子: public private 
2012-11-28 08:32
ainiak110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2012-10-31
收藏
得分:0 
回复 2楼 lybh24
版主大大。按照你的方法还是错了
2012-11-28 10:49
ainiak110
Rank: 2
等 级:论坛游民
帖 子:16
专家分:20
注 册:2012-10-31
收藏
得分:0 
回复 4楼 lybh24
每次运行到红色标记处就出现: 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。

按照你的方法也一样~

我甚至重新在button1里面创建了一个新的

 SqlConnection conn = new SqlConnection("server=.;database=BookStore;uid=sa;pwd=saa");
                conn.Open();
                SqlCommand cmd =new SqlCommand("select Name from Book",conn);
                SqlDataReader reader = cmd.ExecuteReader();

但是还是报错: 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭。

我郁闷死了

[ 本帖最后由 ainiak110 于 2012-11-28 11:17 编辑 ]
2012-11-28 11:15
快速回复:额。。。高手们,解答下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013915 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved