如何调用从一个模块到另一个模块的打开和关闭连接方法. [英] how to called open and closed methods of connection from one module to other.

查看:76
本文介绍了如何调用从一个模块到另一个模块的打开和关闭连接方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为模块编写了以下代码:

i have write this code for module:

Module Module1
Public Class cn
       Public Shared con As New SqlConnection

       Public Shared Sub openconnection()
           con = New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=PROJ;Integrated Security=True")
           con.Open()
       End Sub

       Public Shared Sub close()

           con.Close()
       End Sub
   End Class
end module


我的问题是我想使用此代码来打开和关闭用于多种形式的连接.
这段代码可以工作吗,我该如何调用该方法或使用这些代码呢?这是我的主要目的是最大程度地减少代码和代码的可重复使用性.我怎么能做到这一点.
我正在使用vb.net 2010和后端sql server2008.对于这种类型的实现,没有其他选择.
等待帮助..


my problem is i want to used this code for opening and closing connection that i used for multiple forms.
Can this code is work and how can i call this methods or used this codes.That is my main aim is to minimized codes and code re usability. how can i achieved this.
i am using vb.net 2010 and back end sql server 2008. There is any alternative for this types of implementation.
waiting for some help..

推荐答案

具有多种形式,您可以制作一个环绕您的连接的单例类.然后,在调用单例实例时,请检查该连接是否已经打开,如果已经打开,则不要再次打开它.

正如您今天似乎很幸运(我通常不发布代码,但正如我们所说的那样,我正在做这种工作,尽管我不需要保持跨表单的连接保持打开状态),我将发布给您一些基本注释:

Having multiple forms, you could make a singleton class that wraps around your connection. Then when calling your singleton instance, check if that connection is already open, and if it is, you don''t open it again.

As you seem to be in luck today (I don''t usually post code, but I am doing this kind of work as we speak, although I don''t need to keep connections open across forms), I''ll post you some stuff with basic comments:

class DatabaseServiceOracle {
    private static DatabaseServiceOracle instance;
    private static OracleConnection connection;

    //Empty private constructor, we don't want multiple instances of this class
    private DatabaseServiceOracle() {}

    /*Use this to acquire a reference to present class, and make sure 
    that reference never points to a null object*/
    public static DatabaseServiceOracle getInstance() {
    	if(instance == null) {
            instance = new DatabaseServiceOracle();
    	}
    
    	return instance;
    }

    public OracleConnection getConnection() {
        if(connection == null) {
            //Init your connection here
        }

        /*We don't normally open connections when acquiring them, but we
        do it on a per-need basis*/
        if(connection.State != ConnectionState.Open) {
            connection.Open();
        }

        return connection;
    }
}



另外,不要介意格式,尽管代码是C#,但我们有一个内部约定,说即使我们使用C#,我们也使用Java格式约定.

此外,数据库系统是Oracle,而不是SQL Server,但这很重要.

此外,这是针对MVC系统构想的,因此这里将处理所需的大多数数据库查询(它只是我们根据需要定制的基本模板)



Also, don''t mind the formatting, although the code is C#, we have an internal convention that says we use the Java formatting conventions even when we do C#.

Also, the database system is Oracle and not SQL Server, but the point stands.

Furthermore, this is conceived for an MVC system, so in here would go most of the DB queries needed (it''s just a basic template that we customize for our needs)


这篇关于如何调用从一个模块到另一个模块的打开和关闭连接方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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