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.