[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
- /// <Summary>
- /// Add an object
- /// </Summary>
- /// <Param name = "syscompany"> </param>
- /// <Param name = "sysuser"> </param>
- Public bool insertentities (tsyscompany syscompany, tsysuser sysuser)
- {
- Using (teacrmdbcontext DB = new teacrmdbcontext ())
- {
- If (db. Connection. State! = Connectionstate. open)
- {
- DB. Connection. open ();
- }
- VaR TRAN = dB. Connection. begintransaction ();
- Try
- {
- DB. tsyscompanies. insert (syscompany );
- DB. tsysusers. insert (sysuser );
- Tran. Commit ();
- Return true;
- }
- Catch (exception ex)
- {
- Tran. rollback ();
- Throw new exception (ex. Message );
- Return false;
- }
- Finally
- {
- If (db. Connection. State! = Connectionstate. Closed)
- {
- DB. Connection. Close ();
- }
- }
- }
- }