牛骨文教育服务平台(让学习变的简单)
博文笔记

数据库连接自动释放

创建时间:2017-04-18 投稿人: 浏览次数:868
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NewTest.Dao;
using Models;
using OracleSugar;


namespace NewTest
{
    public partial class Form1 : Form, NewTest.Demos.IDemos
    {
        public Form1()
        {
            InitializeComponent();
        }




        public void Init()
        {
            var db = SugarDao.GetInstance();
            try
            {
                using (db)     //自动控制数据连接释放
                { 
                    object maxId = db.Queryable<Student>().Max(it => it.id);


                     int a = 2 / Convert.ToInt16("0");
          
                    List<Student> list = db.Queryable<Student>().Where(it => (it.name.ToString().Contains("小") && it.id > 6)).ToList();


                    Console.WriteLine(db.Queryable<Student>().ToSql<Student>().Key[0]);
                    System.Diagnostics.Debug.WriteLine(db.GetConnection().State);
                }

            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("当前连接:"+db.GetConnection());
            }
           
        }
    }
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。