Serverversion exception. Invalid operation. The connection is closed. Solution

Source: Internet
Author: User

[Copyright Disclaimer: This article is the original topic of the tewei blog and is not reprinted without permission. If you need to repost the message, make sure to enter the address of this blog .]

Problem: State closed system. Data. connectionstate
The connection is not open ().
Add in try
If (oledbconnection1.state! = Connectionstate. open)
Oledbconnection1.open ();
Add
Finally
{
If (oledbconnection1.state! = Connectionstate. Close)
{
Oledbconnection1.close ();
}
}

 

See http://bbs.csdn.net/topics/90347542

 

 
  1. /// <Summary>
  2. /// Add an object
  3. /// </Summary>
  4. /// <Param name = "syscompany"> </param>
  5. /// <Param name = "sysuser"> </param>
  6. Public bool insertentities (tsyscompany syscompany, tsysuser sysuser)
  7. {
  8. Using (teacrmdbcontext DB = new teacrmdbcontext ())
  9. {
  10. If (db. Connection. State! = Connectionstate. open)
  11. {
  12. DB. Connection. open ();
  13. }
  14. VaR TRAN = dB. Connection. begintransaction ();
  15. Try
  16. {
  17. DB. tsyscompanies. insert (syscompany );
  18. DB. tsysusers. insert (sysuser );
  19. Tran. Commit ();
  20. Return true;
  21. }
  22. Catch (exception ex)
  23. {
  24. Tran. rollback ();
  25. Throw new exception (ex. Message );
  26. Return false;
  27. }
  28. Finally
  29. {
  30. If (db. Connection. State! = Connectionstate. Closed)
  31. {
  32. DB. Connection. Close ();
  33. }
  34. }
  35. }
  36. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.