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

📄 getdatabase.cs

📁 酒店管理系统,您酒店物业管理的好帮手
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Hotel.DAO
{
    public class GetDatabase
    {
        //数据库连接对象
        private SqlConnection con = null;
        private SqlDataAdapter adp = null;
        private DataSet ds = null;
        private SqlCommand cmd = null;
        //private SqlParameter para = null;

        //打开数据库连接的方法
        public GetDatabase()
        {
            this.con = new SqlConnection("server=.;database=hotelDB;uid=sa;pwd=;");
            con.Open();
        }

        //执行数据库增加、修改、删除等操作的方法
        public int ExecuteSql(string sql)
        {
            int i = 0;
            try
            {
                this.cmd = new SqlCommand(sql, this.con);
                i = cmd.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                Console.WriteLine(ex.Message);          
            }
            return i;
        }

        //返回数据集DataSet查询操作
        public DataSet GetDataSet(string sql,string nTableName)
        {
            try 
            {
                this.adp = new SqlDataAdapter(sql,this.con);
                ds = new DataSet();
                adp.Fill(ds, "nTableName");
            }
            catch(SqlException er)
            {
                Console.WriteLine(er.Message);
            }
            return ds;
        }

        //执行数据库只读操作的方法
        public SqlDataReader GetDataReader(string sql)
        {
            SqlDataReader dr = null;
            try
            {
                this.cmd= new SqlCommand(sql, this.con);
                dr = cmd.ExecuteReader();
            }
            catch (SqlException er)
            {
                Console.WriteLine(er.Message);
            }
            return dr;
        }
        public void DrClose(SqlDataReader dr)
        {
            if (dr != null)
            {
                dr.Close();
            }
 
        }

        //数据库读取每一行数据的方法
        //public int GetReaDNum(string sql)
        //{
        //    int t = 0;
        //    SqlDataReader dr = null;
        //    try
        //    {
        //        this.cmd = new SqlCommand(sql, this.sqlcon);
        //        dr = cmd.ExecuteReader();
        //        if (dr.HasRows)
        //        {
        //            t = 1;
        //        }
        //    }
        //    catch (SqlException e)
        //    {
        //        Console.WriteLine(e.Message);
        //    }
        //    return t;
        //}

        //使用储存过程
        public int ExecuteProc(string name,SqlParameter [] para)
        {
            int k = 0;
            using (cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = name;
                foreach (SqlParameter pa in para)
                {
                    cmd.Parameters.Add(pa);
                }
                k = cmd.ExecuteNonQuery();

            }
            return k;
        }
      


        //关闭数据库连接的方法,释放数据库资源
        public void DataClose()
        {
            if (this.con != null)
            {
                this.con.Close();
            }
        }
    }
}

⌨️ 快捷键说明

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