连接未关闭。连接的当前状态是开放的。 (错误) 那是我的错误。 这是一张照片: http://gyazo.com/182a9c61189d15aa0dff79674c4cf871 是的,我创建了一个连接字符串,但它在我的公共部分课程中。
如果有人想要使用什么语言:它是荷兰语:)
这是我的代码:
private void btnOpslaanwerknemergegevens_Click(object sender, EventArgs e)
{
SqlCommand scmdNaam, scmdVoornaam, scmdStraat, scmdHuisnr, scmdPostcode, scmdGemeente, scmdBurgerlijkestand, scmdGeboorte_datum, scmdKinderen_ten_laste, scmdAfdeling, scmdIn_dienst_sinds, scmdGewest, scmdMinder_valide, scmdMinder_valide_partner, scmdMinder_valide_kinderen, scmdInkomen_partner, scmdEmail, scmdTel_nr, scmdGsm_nr;
// new sqlcommands
scmdNaam = new SqlCommand();
scmdVoornaam = new SqlCommand();
scmdStraat = new SqlCommand();
scmdHuisnr = new SqlCommand();
scmdPostcode = new SqlCommand();
scmdGemeente = new SqlCommand();
scmdBurgerlijkestand = new SqlCommand();
scmdGeboorte_datum = new SqlCommand();
scmdKinderen_ten_laste = new SqlCommand();
scmdAfdeling = new SqlCommand();
scmdIn_dienst_sinds = new SqlCommand();
scmdGewest = new SqlCommand();
scmdMinder_valide = new SqlCommand();
scmdMinder_valide_kinderen = new SqlCommand();
scmdMinder_valide_partner = new SqlCommand();
scmdInkomen_partner = new SqlCommand();
scmdEmail = new SqlCommand();
scmdTel_nr = new SqlCommand();
scmdGsm_nr = new SqlCommand();
//sqlcommand.connections
scmdNaam.Connection = loonberekening;
scmdVoornaam.Connection = loonberekening;
scmdStraat.Connection = loonberekening;
scmdHuisnr.Connection = loonberekening;
scmdPostcode.Connection = loonberekening;
scmdGemeente.Connection = loonberekening;
scmdBurgerlijkestand.Connection = loonberekening;
scmdGeboorte_datum.Connection = loonberekening;
scmdKinderen_ten_laste.Connection = loonberekening;
scmdAfdeling.Connection = loonberekening;
scmdIn_dienst_sinds.Connection = loonberekening;
scmdGewest.Connection = loonberekening;
scmdMinder_valide.Connection = loonberekening;
scmdMinder_valide_kinderen.Connection = loonberekening;
scmdMinder_valide_partner.Connection = loonberekening;
scmdInkomen_partner.Connection = loonberekening;
scmdEmail.Connection = loonberekening;
scmdTel_nr.Connection = loonberekening;
scmdTel_nr.Connection = loonberekening;
scmdNaam.CommandText = "UPDATE werknemer SET naam = \'" + txtNaam.Text + "\' WHERE ID = \'" + _id + "\'";
scmdVoornaam.CommandText = "UPDATE werknemer SET voornaam = \'" + txtVoornaam.Text + "\' WHERE ID = \'" + _id + "\'";
scmdStraat.CommandText = "UPDATE werknemer SET straat = \'" + txtStraat.Text + "\' WHERE ID = \'" + _id + "\'";
scmdHuisnr.CommandText = "UPDATE werknemer SET huisnr = \'" + txtStraatnr.Text + "\' WHERE ID = \'" + _id + "\'";
scmdPostcode.CommandText = "UPDATE werknemer SET postcode = \'" + txtPostcode.Text + "\' WHERE ID = \'" + _id + "\'";
scmdGemeente.CommandText = "UPDATE werknemer SET gemeente = \'" + txtGemeente.Text + "\' WHERE ID = \'" + _id + "\'";
scmdBurgerlijkestand.CommandText = "UPDATE werknemer SET burgerlijkestand = \'" + cbxStand.Text + "\' WHERE ID = \'" + _id + "\'";
scmdGeboorte_datum.CommandText = "UPDATE werknemer SET geboorte_datum = \'" + txtGeboortedatum.Text + "\' WHERE ID = \'" + _id + "\'";
scmdKinderen_ten_laste.CommandText = "UPDATE werknemer SET kinderen_ten_laste = \'" + txtKinderentenlaste.Text + "\' WHERE ID = \'" + _id + "\'";
scmdAfdeling.CommandText = "UPDATE werknemer SET afdeling = \'" + txtAfdeling.Text + "\' WHERE ID = \'" + _id + "\'";
scmdIn_dienst_sinds.CommandText = "UPDATE werknemer SET in_dienst_sinds = \'" + txtIndien.Text + "\' WHERE ID = \'" + _id + "\'";
scmdGewest.CommandText = "UPDATE werknemer SET Gewest = \'" + cbxGewest.Text + "\' WHERE ID = \'" + _id + "\'";
scmdMinder_valide.CommandText = "UPDATE werknemer SET minder_valide= \'" + cbxValide.Text + "\' WHERE ID = \'" + _id + "\'";
scmdMinder_valide_kinderen.CommandText = "UPDATE werknemer SET minder_valide_kinderen = \'" + txtHoeveelvalidekinderen.Text + "\' WHERE ID = \'" + _id + "\'";
scmdMinder_valide_partner.CommandText = "UPDATE werknemer SET minder_valide_partner = \'" + cbxPartner.Text + "\' WHERE ID = \'" + _id + "\'";
scmdInkomen_partner.CommandText = "UPDATE werknemer SET inkomen_partner = \'" + txtInkomenpartner.Text + "\' WHERE ID = \'" + _id + "\'";
scmdEmail.CommandText = "UPDATE werknemer SET [e-mail] = \'" + txtEmail.Text + "\' WHERE ID = \'" + _id + "\'";
scmdTel_nr.CommandText = "UPDATE werknemer SET tel_nr = \'" + txtTelefoonnr.Text + "\' WHERE ID = \'" + _id + "\'";
scmdGsm_nr.CommandText = "UPDATE werknemer SET gsm_nr = \'" + txtGsmNr.Text + "\' WHERE ID = \'" + _id + "\'";
loonberekening.Open();
txtNaam.Text = Convert.ToString(scmdNaam.ExecuteScalar());
txtVoornaam.Text = Convert.ToString(scmdVoornaam.ExecuteScalar());
txtStraat.Text = Convert.ToString(scmdStraat.ExecuteScalar());
txtStraatnr.Text = Convert.ToString(scmdHuisnr.ExecuteScalar());
txtPostcode.Text = Convert.ToString(scmdPostcode.ExecuteScalar());
txtGemeente.Text = Convert.ToString(scmdGemeente.ExecuteScalar());
cbxStand.Text = Convert.ToString(scmdBurgerlijkestand.ExecuteScalar());
txtGeboortedatum.Text = Convert.ToString(scmdGeboorte_datum.ExecuteScalar());
txtKinderentenlaste.Text = Convert.ToString(scmdKinderen_ten_laste.ExecuteScalar());
txtAfdeling.Text = Convert.ToString(scmdAfdeling.ExecuteScalar());
txtIndien.Text = Convert.ToString(scmdIn_dienst_sinds.ExecuteScalar());
cbxGewest.Text = Convert.ToString(scmdGewest.ExecuteScalar());
cbxValide.Text = Convert.ToString(scmdMinder_valide.ExecuteScalar());
txtHoeveelvalidekinderen.Text = Convert.ToString(scmdMinder_valide_kinderen.ExecuteScalar());
cbxPartner.Text = Convert.ToString(scmdMinder_valide_partner.ExecuteScalar());
txtInkomenpartner.Text = Convert.ToString(scmdEmail.ExecuteScalar());
txtEmail.Text = Convert.ToString(scmdEmail.ExecuteScalar());
txtTelefoonnr.Text = Convert.ToString(scmdTel_nr.ExecuteScalar());
txtGsmNr.Text = Convert.ToString(scmdGsm_nr.ExecuteScalar());
loonberekening.Close();
答案 0 :(得分:3)
你的方法不正确。 UPDATE命令可以一次更新多个字段。因此,您需要将UPDATE命令文本更改为此类
注意:这是伪代码,你需要填写点......
private void btnOpslaanwerknemergegevens_Click(object sender, EventArgs e)
{
string cmdText = @"UPDATE werknemer
SET naam = @nam,
voornaam = @voornaam,
.... continue with other fields
.... and finish with
WHERE ID = @id";
using(SqlConnection con = new SqlConnection(.... connectionstring ....))
using(SqlCommand cmd = new SqlCommand(cmdText, con))
{
con.Open();
cmd.Parameters.Add("@naam", SqlDbType.NVarChar).Value = txtNaam.Text;
cmd.Parameters.Add("@vornaam", SqlDbType.NVarChar).Value = txtVoornaam.Text;
... continue adding a parameter for every field value .....
... and finally call ...
cmd.ExecuteNonQuery();
}
}
在此伪代码中,连接不是全局对象,而是使用Using语句在本地创建,该语句将在异常情况下关闭并处理Connection。然后使用参数占位符(@xxxx)构建命令文本,最后在填充参数集合后执行一个单独的命令,并将值传递给数据库引擎以替换占位符。
答案 1 :(得分:0)
if (loonberekening.State == ConnectionState.Closed)
{
loonberekening.Open();
}
答案 2 :(得分:0)
听起来好像在btnOpslaanwerknemergegevens_Click之外打开了loonberekening并且从未关闭过。没有更多的代码很难说,但我猜你有懒散的共享变量,因为btnOpslaanwerknemergegevens_Click似乎没有创建它。也许在调用btnOpslaanwerknemergegevens_Click之前它已经打开了。我建议在btnOpslaanwerknemergegevens_Click中创建一个新连接。