C#控制台应用程序无效的操作异常 [英] C# console application Invalid Operation Exception

查看:281
本文介绍了C#控制台应用程序无效的操作异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
using System.Collections.Generic;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;

命名空间BissUpdater
{
class Program
{
static void Main(string [] args)
{
string connectionString =Data Source = H ....;
初始目录= LANDesk; Persist Security Info = True;
用户ID = Mainstc;密码= xxxxxxxx

SqlConnection con = new SqlConnection(connectionString);
con.Open();
}
}
}

SQL连接操作异常。


无效的操作。连接已关闭。




这是我的完整代码。在另一个程序中,它工作完美。



这是第二次,这不工作。我使用VS2005 ...也许我的程序已损坏?



Stacktrace:


在System.Data.SqlClient.SqlConnection.GetOpenConnection()

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



解决方案

正确的做法应该是:

  static void Main(string [] args){
string connectionString =Data Source = H ....;
Initial Catalog = LANDesk; User ID = Mainstc; Password = xxxxxxxx
// removed Persist Security Info = True;


使用(SqlConnection con = new SqlConnection(connectionString))
{
if(con.State == ConnectionState.Closed)
{
con.Open();
}
}


}

使用使用语句它将自动处理您的SQL连接。



还要检查:在MSDN上使用ADO.NET的最佳做法



其他要做的事情:使用SQL Management Studio并尝试从连接字符串使用您的sql认证登录凭据,如果您已使用该帐户成功连接到您的数据库,上述代码应该为您工作。 p>

最好的问候


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

解决方案

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

这篇关于C#控制台应用程序无效的操作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆