⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlbaseclass.cs

📁 订阅报刊的企业员工用户希望通过企业的系统更方便、快捷地订阅报刊
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

using System.Data;      //添加引用
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 报刊管理.类
{
    class SqlBaseClass
    {
        #region        代码中用到的变量
        SqlDataAdapter SDA;   //声明数据适配器对象
        DataSet DS;          //声明数据集对象
        SqlCommand SCd;
        string ConnectionString = "SERVER=localhost;DATABASE=newsmanagement;UID=sa;PWD=123";
        SqlConnection SCn;  //声明链接对象
        #endregion

        #region  构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public SqlBaseClass()
        {

        }
        #endregion

        #region   连接数据库
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns></returns>
        public SqlConnection GetCon()
        {
            SCn = new SqlConnection(ConnectionString);
            SCn.Open();
            return SCn;
        }
        #endregion

        #region   执行SQL语句
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sqlcmd">要执行的SQL语句</param>
        /// <returns></returns>
        public bool GetExecute(string sqlcmd)
        {
            SCd = new SqlCommand(sqlcmd, GetCon());
            try
            {
                SCd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return false;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    GetCon().Dispose();
                }
            }
        }
        #endregion

        #region    返回数据集类型
        /// <summary>
        /// 返回数据集类型
        /// </summary>
        /// <param name="sqlcmd">需要查询的SQL语句</param>
        /// <returns></returns>
        public DataSet GetDS(string sqlcmd)
        {
            try
            {
                SDA = new SqlDataAdapter(sqlcmd, GetCon());
                DS = new DataSet();
                SDA.Fill(DS);
                return DS;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return null;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    GetCon().Dispose();
                }
            }
        }
        #endregion

        #region    返回数据表类型
        /// <summary>
        /// 返回数据表
        /// </summary>
        /// <param name="sqlcmd"></param>
        /// <param name="tablename"></param>
        /// <returns></returns>
        public DataTable GetTable(string sqlcmd, string tablename)
        {
            DataTable DTbl;   //声明一个DataTable对象
            try
            {
                SDA = new SqlDataAdapter(sqlcmd, tablename);
                DTbl = new DataTable(tablename);
                SDA.Fill(DTbl);   //将表中对象放入DTbl中
                return DTbl;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return null;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    GetCon().Dispose();
                }
            }
        }
        #endregion

        #region   返回SqlDaraReader类型数据
        /// <summary>
        /// 返回SqlDataReader类型数据
        /// </summary>
        /// <param name="sqlcmd">要执行的SQL语句</param>
        /// <returns></returns>
        public SqlDataReader GetReader(string sqlcmd)
        {
            SCd = new SqlCommand(sqlcmd, GetCon());  //声明SqlCommand对象
            SqlDataReader SDR;
            try
            {
                SDR = SCd.ExecuteReader();
                return SDR;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return null;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    GetCon().Dispose();
                }
            }
        }
        #endregion

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -