C# console application Invalid Operation Exception

无人久伴 提交于 2019-12-18 04:12:40

问题


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();
        }
    }
}

The SQL Connection threw a invalid operation exception.

"Invalid Operation. The connection is closed".

This is my complete Code. In a other program, it works perfect.

That is the second time, that doesnt work. Im working with VS2005...maybe my program is damaged?

Stacktrace:

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


回答1:


The correct way doing that should be something like:

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 dispose your SQL connection.

Check this also: Best Practices for Using ADO.NET on MSDN

Other things to do: Use SQL Management Studio and try to use your sql authentication login credential from your connection string and if you have successfully connected to your database using that account the above code should work for you.

Best Regards




回答2:


The code should read

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

    ...
}



回答3:


Try add this code. You probably has open connection and while rerun program you try to again open connection or you have problem with server or your connection string

con.Close();

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




回答4:


you can check connection state 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();



回答5:


Try to use using statements. Direct manually opening and closing data bases in case of large databases is bad idea.

using(SqlConnection con = new SqlConnection(connectionString))

Try to do like this for open and close connections>>

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


public void Close()
{
con.Close();
//con.Dispose();

} 

Hope Helpful.




回答6:


I was having same problem, my solution in VB is:

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 of my WebService:

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


来源:https://stackoverflow.com/questions/15566277/c-sharp-console-application-invalid-operation-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!