相关在C#中进行连接 [英] Related making Connection in C#

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

问题描述

嗨...朋友们

我以前每次都做连接对象,然后
我打开它,然后关闭它.这需要很多时间.

Hi... Friends

I used to make connection object every time, then
I open it and after doing it I Close it. This take lot of time. Is there anyway to get rid off this.

推荐答案

在web.config页面中建立全局连接..添加此代码


< appSettings>
<添加key ="mas" value =数据源= ARSHAD \ SQLEXPRESS;初始目录= ug;集成安全性= true;"/>
</appSettings>
< connectionStrings/>


在C#页面中.声明连接&在页面加载之前将其写入,即使其宽泛..

SqlConnection con =新的SqlConnection(ConfigurationManager.AppSettings ["mas"]);

之后,仅在需要连接的地方使用con(即connection obj),这样就无需再次建立连接&再次..

如果这是您的解决方案,请投票给我或标记为解决方案.
makes a global Connection in web.config page.. Add this code


<appSettings>
<add key="mas" value="data source=ARSHAD\SQLEXPRESS; initial catalog=ug; integrated security=true;"/>
</appSettings>
<connectionStrings/>


In C# page.. Declare Connection & write it before page load i.e. makes it lobally..

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["mas"]);

after that use only con (i.e. connection obj) where u need for connection.. by this there is no need to makes connection again & again..

Plz vote me or mark as solution if this is ur solution.


创建一个静态类

make a static class

class ClsMain
    {
        
        public static SqlConnection Con = null; 

    }



建立连接的功能



make a fuction to make connection

public static SqlConnection GetConnection(string connectionString)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();           

            return connection;
        }



使用此行初始化连接



use this line initialize connection

clsMain.Con=GetConnection("connection string"); 


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

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