关于Sql连接 [英] Regarding Sql Connection

查看:54
本文介绍了关于Sql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sql server在C#.net中进行Project.在这里,我正在表单的每个Click事件中打开连接.它在执行时会延迟一点时间.因此,如何在表单加载事件中或使用Class概念编写代码以进行全局连接.

I am doing Project in C#.net with Sql server. Here I am Opening connection in each and every Click event of form. It''s deferring little bit while executing.. So how to write code to make global connection in form load event or using Class concept.

推荐答案

:不要这样做.

您可能无法在一个项目中使用它,但是SQLServer连接在现实世界中是稀缺资源,任何在全球范围内保持连接打开状态的应用程序都会严重困扰数据库管理员,因为该连接对其他用户不可用-这会变得昂贵! />
始终创建连接,使用连接,尽快关闭并处置连接.

您确定是花费时间的连接而不是命令吗?
The first thing is: Don''t do it.

You may get away with it for a project, but SQLServer connections are scarce resources in the real world, and any app holding a connection open globally will seriously annoy database administrators as the connection is not available for other users - this gets expensive!

Always create connection, use connection, close and dispose connection as quickly as possible.

Are you sure it is the connection that is taking time, not your command?


使用Class并创建用于创建数据连接的公共函数
这是代码

Use Class and create a public function for creating data connection
here is the code

public bool OpenConnection(MySqlConnection Conn)
    {
        String connStr = "your connection string"
        if (Conn == null)
        {
            errorMessage = "Connection Object doesn't exists. Internal Error";
            return false;
        }
        if (Conn.State != ConnectionState.Open)
        {
            try
            {
                Conn.ConnectionString = connStr;
                Conn.Open();
                return true;
            }
            catch (Exception objE)
            {
                
            }
        }
        else
        {
            return true;
        }
    }


这篇关于关于Sql连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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