C # console application Invalid action Exception

using System; using System.Collections.Generic; using System.Text; using System.Data.Sql; using System.Data.SqlClient; namespace BissUpdater { class Program { static void Main(string[] args) { string connectionString = "Data Source=H....; Initial Catalog=LANDesk; Persist Security Info=True; User ID=Mainstc; Password=xxxxxxxx"; SqlConnection con = new SqlConnection(connectionString); con.Open(); } } } 

In SQL Connection, invalid operation exceptions occurred.

"Invalid operation. Connection closed."

This is my complete code. In another program, it works fine.

This is the second time, it does not work. Im working with VS2005 ... maybe my program is corrupted?

Stacktrace:

in System.Data.SqlClient.SqlConnection.GetOpenConnection ()
in System.Data.SqlClient.SqlConnection.get_ServerVersion ()

+6
source share
6 answers

The correct way to do this should look something like this:

 static void Main(string[] args) { string connectionString = "Data Source=H....; Initial Catalog=LANDesk;User ID=Mainstc; Password=xxxxxxxx"; // removed Persist Security Info=True; using(SqlConnection con = new SqlConnection(connectionString)) { if (con.State==ConnectionState.Closed) { con.Open(); } } } 

Using Using Statement , it will automatically remove your SQL connection.

Check this out as well: ADO.NET Recommendations on MSDN

Other actions. Use SQL Management Studio and try to use the credentials to log in to the sql account from the connection string, and if you successfully connected to your database using this account, the above code should work for you.

Best wishes

+14
source

Try adding this code. You probably have an open connection, and when you try again, you try to open the connection again or you have problems with the server or connection string

 con.Close(); 

See more information http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open.aspx

0
source

you can check the connection status before opening ittry this:

 SqlConnection con = new SqlConnection(connectionString); if (con.State==ConnectionState.Closed) { con.Open(); } // here your code goes for sql operations con.Close(); 
0
source

Try using using statements. Direct manual opening and closing of databases in the case of large databases is a bad idea.

 using(SqlConnection con = new SqlConnection(connectionString)) 

Try this for open and closed connections →

 public DB(string conStr):base() { con = new OracleConnection(conStr); con.Open(); } public void Close() { con.Close(); //con.Dispose(); } 

Hope that is helpful.

0
source

Code must read

 using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); ... } 
0
source

I had the same problem, my solution in VB:

 Dim db As New Database ' ... Some Work with EF without procedures (90 seconds) db.SaveChanges() For Each p In list If db.Database.Connection.State <> ConnectionState.Open Then ' This is only executed 1 time db.Database.Connection.Open() End If ' ... Some Work with EF but calling a mapped procedure (1 or 2 seconds each call) db.MyProcedure(p.FieldId) Next db.Dispose() 

But the total time was 200 seconds, so I had to change this in my WebConfig my WebService :

 <system.web> <httpRuntime executionTimeout="600" /> <!--10 min--> ... 
0
source

All Articles