Skip to main content
 首页 » 编程设计

c#之多用户连接并同时进行 CRUD

2024年02月23日14tuyile006

我正在使用 ASP.NET Framework 4、IIS 7 和 SQL Server 2008 R2。

我遇到了这样的错误: {column} 在选定的数据源中找不到, SQL 阅读器已关闭,....

它只发生在:

  1. 多个用户已连接。
  2. 他们同时进行 CRUD(创建、检索、更新、删除)调用。

奇怪的是,它逃脱了我的捕获:

try{ 
    Connexion_D.GetConnected(); 
    // doing CRUD 
} 
catch{ 
    // catching Error, avoid yellow page aspx 
} 
finally 
{ 
    Connexion_D.CloseConnection(); 
}  

还有我的连接类:

public class Connexion_D 
{ 
 
    static public SqlConnection conn; 
    static public SqlConnection GetConnected() 
    { 
        try 
        { 
            String strConnectionString = ConfigurationManager.ConnectionStrings["xxxxx"].ConnectionString; 
            conn = new SqlConnection(strConnectionString); 
        } 
        catch (Exception excThrown) 
        { 
            conn = null; 
            throw new Exception(excThrown.InnerException.Message, excThrown); 
        } 
 
        // Ouverture et restitution de la connexion en cours 
        if (conn.State == ConnectionState.Closed) conn.Open(); 
        return conn; 
    } 
    static public Boolean IsConnected 
    { 
        get { return (conn != null) && (conn.State != ConnectionState.Closed) && (conn.State != ConnectionState.Broken); } 
    } 
 
    static public void CloseConnection() 
    { 
        // Libération de la connexion si elle existe 
        if (IsConnected) conn.Close(); 
    } 
} 

所以我认为代码没有错误/有错误。

我觉得可能是IIS和SQL server的配置问题。

有什么想法吗?

提前致谢。

请您参考如下方法:

如果我理解你在做什么,那么这看起来很可疑:

static public SqlConnection conn;      
static public SqlConnection GetConnected() {          
    try          
    {              
        String strConnectionString = ConfigurationManager.ConnectionStrings["xxxxx"].ConnectionString;              
        conn = new SqlConnection(strConnectionString); 
    }  
} 
 
static public void CloseConnection() {            
    // Libération de la connexion si elle existe            
    if (IsConnected) conn.Close();        
}    

您正在使用静态连接变量,这意味着当您关闭它时,您将关闭最后一个打开的连接变量。

在多用户场景中,您可能会遇到这种情况:

  • 用户 A:创建连接(并返回连接 1)
  • 用户 A:执行查询(针对连接 1 运行)
  • 用户 B:创建连接(并返回连接 2)
  • 用户 A:关闭连接(最后打开的是 2,所以关闭)
  • 用户 B:执行查询(针对连接 2 运行,它已被关闭...砰)

顺便说一句,您可能应该重新考虑将您的连接作为公共(public)成员变量:

static public SqlConnection conn;      

这通常被认为是不好的做法,如果您的类之外的任何代码开始弄乱它的内部变量,将来可能会导致意外/难以追踪错误。

编辑:

最明显的解决方案似乎是阻止静态连接。您的客户端代码可能看起来像这样:

try{ 
    // use using block around connection, calls dispose automatically when 
    // block ends... 
    using(var connectionWrapper = new Connexion_D()) { 
        var connectedConnection = connectionWrapper.GetConnected();         
        // do CRUD 
    } 
} 
catch{ 
    // catching Error, avoid yellow page aspx 
    // Really you should probably be doing something with the exception (logging?) 
    // particularly since you go to the effort of throwing it from your Connection_D 
    // class. 
} 

类代码如下:

/* Implement IDisposable to cleanup connection */ 
public class Connexion_D : IDisposable  
{ 
    public SqlConnection conn; 
 
    public SqlConnection GetConnected() 
    { 
        try 
        { 
            String strConnectionString = ConfigurationManager.ConnectionStrings["xxxxx"].ConnectionString; 
            conn = new SqlConnection(strConnectionString); 
        } 
        catch (Exception excThrown) 
        { 
            conn = null; 
            throw new Exception(excThrown.InnerException.Message, excThrown); 
        } 
 
        // Ouverture et restitution de la connexion en cours 
        if (conn.State == ConnectionState.Closed) conn.Open(); 
        return conn; 
    } 
    public Boolean IsConnected 
    { 
        get { return (conn != null) && (conn.State != ConnectionState.Closed) && (conn.State != ConnectionState.Broken); } 
    } 
 
    public void CloseConnection() 
    { 
        // Libération de la connexion si elle existe 
        if (IsConnected) {  
            conn.Close(); 
            conn = null; 
        } 
    } 
 
    // Implement IDisposable. 
    // Do not make this method virtual. 
    // A derived class should not be able to override this method. 
    public void Dispose() 
    { 
        // Close connection 
    } 
} 

参见 this有关实现 IDisposable 的更多信息。