为什么会出现 ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭的错误 ,求解答 谢谢谢谢谢。
SqlConnectioncon=newSqlConnection(strConn);SqlCommandcmd=newSqlCommand("SELECTGoodsID...
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("SELECT GoodsID FROM KCB WHERE GoodsID='" + this.txtGoodsID.Text.Trim() + "'", con);
if (cmd.ExecuteScalar() != null)
{
SqlCommand Sqlcmd = new SqlCommand("UPDATE KCB SET Number=Number+" + Convert.ToInt32(txtSL.Text.Trim()) + "");
Sqlcmd.ExecuteNonQuery();
}
else
{
string SAVE = "insert into KCB(DH,GoodsID,SL,DJ,ZJ,GHS,stockstaff,dateTimePicker)values('" + txtDH.Text.Trim() +
"','" + txtGoodsID.Text.Trim() + "','" + txtSL.Text.Trim() + "','" + txtDJ.Text.Trim() + "','" + txtZJ.Text.Trim() +
"','" + cmbGHS.Text.Trim() + "','" + cmbstockstaff.Text.Trim() + "','" + cmbdateTimePicker.Text.Trim() + "')";
cmd.CommandText = SAVE;
}
string sql = string.Format("INSERT INTO JHXX( DH,GoodsID,SL,DJ,ZJ,GHS,stockstaff,dateTimePicker) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", txtDH.Text.Trim(), txtGoodsID.Text.Trim(), txtSL.Text.Trim(), txtDJ.Text.Trim(), txtZJ.Text.Trim(), cmbGHS.Text.Trim(), cmbstockstaff.Text.Trim(), cmbdateTimePicker.Text.Trim());
SqlCommand cmd1 = new SqlCommand(sql, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ShowInfo();
} 展开
SqlCommand cmd = new SqlCommand("SELECT GoodsID FROM KCB WHERE GoodsID='" + this.txtGoodsID.Text.Trim() + "'", con);
if (cmd.ExecuteScalar() != null)
{
SqlCommand Sqlcmd = new SqlCommand("UPDATE KCB SET Number=Number+" + Convert.ToInt32(txtSL.Text.Trim()) + "");
Sqlcmd.ExecuteNonQuery();
}
else
{
string SAVE = "insert into KCB(DH,GoodsID,SL,DJ,ZJ,GHS,stockstaff,dateTimePicker)values('" + txtDH.Text.Trim() +
"','" + txtGoodsID.Text.Trim() + "','" + txtSL.Text.Trim() + "','" + txtDJ.Text.Trim() + "','" + txtZJ.Text.Trim() +
"','" + cmbGHS.Text.Trim() + "','" + cmbstockstaff.Text.Trim() + "','" + cmbdateTimePicker.Text.Trim() + "')";
cmd.CommandText = SAVE;
}
string sql = string.Format("INSERT INTO JHXX( DH,GoodsID,SL,DJ,ZJ,GHS,stockstaff,dateTimePicker) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", txtDH.Text.Trim(), txtGoodsID.Text.Trim(), txtSL.Text.Trim(), txtDJ.Text.Trim(), txtZJ.Text.Trim(), cmbGHS.Text.Trim(), cmbstockstaff.Text.Trim(), cmbdateTimePicker.Text.Trim());
SqlCommand cmd1 = new SqlCommand(sql, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ShowInfo();
} 展开
3个回答
展开全部
1。 SqlConnection con = new SqlConnection(strConn);
----->这里最好加上con.open()打开数据库连接
SqlCommand cmd = new SqlCommand("SELECT GoodsID FROM KCB WHERE GoodsID='" + this.txtGoodsID.Text.Trim() + "'", con);
if (cmd.ExecuteScalar() != null)
2。另外这里try
{
con.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
con.Close();---〉不应该放在这里关闭,而应该在finally里面写
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ShowInfo();
}
----->这里最好加上con.open()打开数据库连接
SqlCommand cmd = new SqlCommand("SELECT GoodsID FROM KCB WHERE GoodsID='" + this.txtGoodsID.Text.Trim() + "'", con);
if (cmd.ExecuteScalar() != null)
2。另外这里try
{
con.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
con.Close();---〉不应该放在这里关闭,而应该在finally里面写
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ShowInfo();
}
展开全部
SqlConnection con = new SqlConnection(strConn);
con.open();
con.open();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-06-15
展开全部
1. 在使用连接之前要先打开连接
con.Open();
2. 连接这种资源最好使用using (SqlConnection con = new SqlConnection(strConn)) 来使用
con.Open();
2. 连接这种资源最好使用using (SqlConnection con = new SqlConnection(strConn)) 来使用
参考资料: MSDN
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询