返回顶部

sql server数据库封装DBHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class DBHelper
    {

       // static string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=four;Integrated Security=True");

        //增删改
        public int ExeNonQuery(string sql)
        { 
            //打开数据库
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql,conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            return i;
        }

        //聚合函数查询
        public object ExeScalar(string sql)
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            object i = cmd.ExecuteScalar();
            conn.Close();
            return i;  
        }

        //多条数据
        public DataTable GetTable(string sql)
        {
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        
        }


    }
}

 

posted @ 2019-04-01 18:48  世界欠绘梨衣一场婚礼  阅读(142)  评论(0编辑  收藏  举报