连接池与数据库


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 连接池
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {

            //string connStr = @"server=.;database=MyDB;uid=sa;pwd=123;integrated security=true";
            //// string sql = "insert into Table_loginTest(uid,pwd)values('"+uid+"','"+pwd+"')";
            //// string sql = string.Format("insert into Table_loginTest(uid,pwd)values('{0}','{1}')", uid, pwd);
            //Stopwatch sp = new Stopwatch();
            //sp.Start();

            //for (int i = 0; i < 1000; i++)
            //{
            //    SqlConnection conn = new SqlConnection(connStr);
            //    conn.Open();
            //    conn.Close();
            //}
            //sp.Stop();
            //txtTime.Text = sp.Elapsed.ToString();

            string connStr = @"server=.;database=MyDB;uid=sa;pwd=123;integrated security=true;pooling=true";
            // string sql = "insert into Table_loginTest(uid,pwd)values('"+uid+"','"+pwd+"')";
            // string sql = string.Format("insert into Table_loginTest(uid,pwd)values('{0}','{1}')", uid, pwd);
            Stopwatch sp = new Stopwatch();
            sp.Start();

            for (int i = 0; i < 1000; i++)
            {
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                conn.Close();
            }
            sp.Stop();
            txtTime.Text = sp.Elapsed.ToString();
          


        }
    }
}
=>要点说明,

1.数据库连接池的开启跟关闭
当我们有多个对象,需要连接数据库的时候,这个时候会涉及到连接池,因为开启连接池,会提高连接速度。
具体的参见:
string connStr = @"server=.;database=MyDB;uid=sa;pwd=123;integrated security=true;pooling=true";
pooling=true 打开连接池</strong></span>
pooling=false 关闭连接池</strong></span>
注意:数据库的连接需要晚点打开,早点关闭
conn.Open();
conn.Close();

2.代码中,如何监视一段代码的执行时间
Stopwatch sp = new Stopwatch();
sp.Start();
。。。
sp.Stop();
 sp.Elapsed 就是所需的代码执行时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值