Required Connection that are open and available. The current state of the connection is closed.

Source: Internet
Author: User

C # using OLE DB to manipulate access
Provider=Microsoft.Jet.OLEDB.4.0;Data source= "+ NewFileName +"; User Id=uid; Jet oledb:database password=pwd; ";

Complete error message:
{System.InvalidOperationException:ExecuteScalar requires a Connection that is already open and available. The current state of the connection is closed.
In System.Data.OleDb.OleDbConnection.CheckStateOpen (String method)
In System.Data.OleDb.OleDbCommand.ValidateConnection (String method)
In System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction (String method)
In System.Data.OleDb.OleDbCommand.ExecuteReaderInternal (CommandBehavior behavior, String method)
In System.Data.OleDb.OleDbCommand.ExecuteScalar ()
In Public.DB.CheckTableIfExist (string tablename, string condition) position Public\db.cs: line number 136
In Myini.getcompanyname () position Program.cs: line number 293
In Program.main () position Program.cs: line number 734}

cause of error:
The current state of the connection is closed. or only the connection object is created, no call is made to
Connection.Open () method;
namely: Connection. State! = connectionstate.closed

Workaround:
Call the Connection.Open () method;
Or add a judgment, such as the following code:

if (MyIni.Gs_con.State != ConnectionState.Closed) {     new OleDbCommand(sqltext, MyIni.Gs_con);     //tools.MB(sqltext);     result = tools.fieldnullint(mysc.ExecuteScalar());     if0returnfalse; }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Required Connection that are open and available. The current state of the connection is closed.

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.