我有這樣的代碼在我的WinForm C#應用程序:的MessageBox後提交的C#
public void InsertValidationDate(Validar v, string descanso)
{
bool insert = false;
try
{
bd.con.Open();
bdag.bd.trans = bdag.bd.con.BeginTransaction();
insert = InsertDate(v);
if (insert == true)
{
MessageBox.Show("Insert Ok");
pk = ReturnInsertDate(DateTime.Parse(v.Date), v.User);
insert = UpdateReference(DateTime.Parse(v.Date), pk);
if (insert == false)
{
Rollback();
MessageBox.Show("Update Error");
}
else
{
Commit();
MessageBox.Show("Update OK");
}
}
}
catch (Exception ex)
{
Rollback();
}
finally
{
con.Close();
}
}
顯示消息(「插入OK」)連接到數據庫關閉後,交易是不是更多鈔票COMMITED。爲什麼?因爲Insert函數不關閉數據庫連接。有任何想法嗎?謝謝!!。
public int ReturnInsertDate(DateTime date, int user)
{
int validate = -1;
try
{
bd.CadSQL = "select " + bd.Validar.id + " from " + bd.Validar.tabla + " where ";
bd.CadSQL += bd.Validar.date + " = '" + date.Year + "-" + date.Month.ToString("d2") + "-" + date.Day.ToString("d2") + "'";
bd.CadSQL += " and " + bd.Validar.user+ " = " + user;
bd.comandoSQL.CommandText = bd.CadSQL;
bd.reader = bd.comandoSQL.ExecuteReader();
while (bd.reader.Read())
{
if (!bd.reader.IsDBNull(0))
validate = bd.reader.GetInt32(0);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
bd.reader.Close();
}
return validate;
}
public bool UpdateReference(DateTime date, int validate)
{
bool updateR = false;
try
{
bd.CadSQL = "update " + bd.Fichar.tabla;
bd.CadSQL += " set " + bd.Fichar.validateId + " = " + validate;
bd.CadSQL += " where " + bd.Fichar.date + " = '" + date.Year + "-" + date.Month.ToString("d2") + "-" + date.Day.ToString("d2") + "'";
bd.comandoSQL.CommandText = bd.CadSQL;
bd.comandoSQL.ExecuteNonQuery();
updateR = true;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return updateR ;
}
什麼是插入(V)..... – pankeel
插入是插入到數據庫中,我在我的工作表中的驗證工作小時後,我更新這個ID驗證到其他表的功能。謝謝 – juanchoelx
您是否關閉了InsertDate,ReturnInsertDate或UpdateReference中的連接? – Steve