额。。。高手们,解答下
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 编辑 ]