需要帮助来使用MySqlTransaction(Need help in using MySqlTransaction)

   IT问题网   2021-01-18 00:00:00

问 题

大家好我想在我的要求中使用mysqltransaction。实际上我对此有疑问,即根据我的要求,我将不得不从数据库中删除不同的值。



我正在做的过程如下。假设我有2个empid,其中此empid将包含可能为多个的不同值。我将使用dictionary存储该特定empid的相应值,然后我将它们保存到与empid相对应的列表中。



假设我有list元素as关注



对于empid 1,我将有1,2。我将检查此列表中数据库的最大值(如果存在)我想从数据库中删除此empid。



对于empid 2,我将有1, 2。但在我的数据库中,我将有3个最大值。所以这个失败了。我想回滚之前删除的项目。



是否可以处理交易,如果有的话可以帮助我解决这个问题



示例i代码



 如果(findmax(lst,iempid)
{
obj.delete(" storeprocname"); // 当我的列表具有最大值时会发生这种情况
}
else
{
// 这里我想回滚我之前提到的类文件中的删除方法
}






  public   bool  deletepayroll(  string  storeproc, bool 结果)
{
if (result == true
{
m_bflag = false ;
this .m_oconn = utilities.getconnection();
m_ocmd = new mysqlcommand(storeproc,m_oconn);
m_ocmd.commandtype = commandtype.storedprocedure;

m_ocmd.parameters.addwithvalue(" _ empid",empid);
m_ocmd.parameters.addwithvalue(" _ fedtaxid",fedtaxid);
m_ocmd.parameters.addwithvalue(" _ payperiodnumber",payperiodnumber);
m_ocmd.parameters.addwithvalue(" _ payyear",payyear);
m_ocmd.parameters.addwithvalue(" _ paymentdate",paymentdate);

尝试
{
如果(m_oconn。 state!= connectionstate.open)
{
m_oconn.open();

}
if (m_ocmd.executenonquery()gt; 0
{
m_bflag = true ;
}
}
catch (mysqlexception osqlex)
{
m_sberrmsg.length = 0 ;
m_sberrmsg = utilities.sqlerrormessage(osqlex);
// db写入错误日志
m_oerrlog.add(m_sberrmsg.tostring (),datetime.now);
}
catch (例外oex)
{

m_sberrmsg = utilities.errormessage(oex);
// db写入错误日志
m_oerrlog.add(m_sberrmsg.tostring (),datetime.now);
}

最后
{
m_oconn.close();
}
} 其他
{
// 这里我想回滚它是正确的过程
}
return m_bflag;
}




嗯,我认为没有人能清楚地理解我的问题,所以在简单的场景中我说的是



i我有一个表格,当我点击按钮时会有2个文本框和按钮我想将文本框中输入的文本作为单独的行插入数据库。



假设插入时我将有一个bool值,如果我为它插入的第一个项目给出了真值,我将对我要插入的第二个项目给出错误。由于我有假,我不想插入这个,所以我必须回滚前一个



a示例代码



按钮点击
{
string str = " dora";
string str1 = " babu";
bool flag = true ;
i = 1 ,j = 1 ;
if (i == 1 amp;amp; flag == true
inser str;
flag = false ;
如果(j == 1 amp;amp; flag == false
rollback
}

解决方案

是 - 并且交易是正是你需要的。一旦启动事务,在提交事务之前,任何更改都不会是永久性的,这与正常操作不同,只要命令执行后它们就会不可逆转地发生。通过交易,您可以"改变主意"并回滚或取消您所采取的操作。



mysql中有交易指南: http://www.devshed.com/c/a/mysql/using- transactions-in-mysql-part-1 /[ ^ ]

正如originalgriff指出的那样,事务是执行此操作的正确方法。另请查看文档: mysqltransaction[ ^ ]。



另一件事,如果我理解你的情况正确,你是计算最大值,也许插入数据,以便你使用max + 1是类似的。如果您有这种结构,则不建议在数据库中维护此类订单号。每行应该有一个唯一的键,但是你不应该在程序中设置它。而是使用 auto_increment[ ^ ]

标签:需要帮助匡助来使



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

如何使用VBscript导航Windows注册表树(How to navigate Windows registry tree with VBscript)

problem does anyone know how to go about navigating the w ...

silverlight内存泄漏(silverlight memory leak)

problem hi, i was creating a dependencyproperty changed w ...

如何在json中获取单个元素(How to get single element in json)

problem in wcf service i am getting json response in that ...

双击文件打开程序,如何在c#中实现(double click on file opens program, how to implement in c#)

problem usually when a program in installed, you can clic ...

如何在我添加的现有数据表中添加新列,但它是bofore行的结尾(how add new column in existing datatable i added but its is come end of the bofore rows)

problem my datatable contans two columns id value ----- ...

如何在c#.net中的现有页面中打开新网址(How to open new url in existing page in c#.net)

problem hi to all, this is mdsiddiqali.i added new page n ...

DeleteObject方法不会删除任何内容 - 请帮忙。(DeleteObject method doesn't delete anything - please help.)

problem hello. i am trying to create method that will del ...

项目模板类型在C#中使用?(PROJECT TEMPLATE TYPE TO USE IN C#)

problem edit; okay i am creating a dock for programs to r ...

如何在我添加的现有数据表中添加新列,但它是bofore行的结尾(how add new column in existing datatable i added but its is come end of the bofore rows)

problem my datatable contans two columns ; id value ---- ...

在c#和asp.net中使用LinkedIn API?(Working with LinkedIn APIs in c# and asp.net )

problem hey i was trying to design an asp.net based webs ...

设计模式的案例研究(Case Studies for Design Pattern)

problem hi everyone, though i found "design patterns exp ...

返回所有组件(Retur all components)

problem dears, i need a method to return all named contro ...

Picturebox控件不会触发鼠标事件(Picturebox control not firing mouse events)

problem i have a picturebox control in a groupbox control ...

Windows 7中的VB.net + Crystall报告问题(VB.net + Crystall report issue in Windows 7)

problem i have created windows application in vb.net 2005 ...

如何将数据表中的数据保存回数据库?(How to save the data in a datatable back into the database)

problem i have filled my datatable with my sqladapter ass ...

测试ip和developpe ip扫描仪(Test ip and developpe ip scanner)

problem hi friends, i want help !!! i need c ...

在.Net中使用WCF的3层(3 tier using WCF in .Net)

problem hi, i have developed a 3 tier application in vb. ...

连续循环不循环(Continual Loop Not Looping)

problem j have code that is suppose to continue looping l ...

vb中的动态按钮控件(Dynamic button control in vb)

problem hi, i''ve a 10x10 matrix of buttons. i need to c ...

动态创建按钮的事件处理程序(event handler for dynamically created buttons)

problem i''ve created 100 buttons dynamically. but how to ...