dataaccessor.cs

来自「本系统是基于三层架构和Ajax控件结合的酒店预订系统」· CS 代码 · 共 72 行

CS
72
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DBaoBookingManagement
{
    /// <summary>
    /// 所有数据操作类的父类
    /// </summary>
    public class DataAccessor
    {
        //获取sql连接
        private SqlConnection GetSqlConnection()
        {
            string strConn="Data Source=.;Initial Catalog=DBaoBooking;User ID=sa;Password=";
            return new SqlConnection(strConn);
        }

        //执行对sql server数据库的编录操作,成功返回true,失败为false
        protected bool ExecuteSqlNoneQuery(string sql)
        {
            SqlConnection conn = null;
            try
            {
                conn = GetSqlConnection();
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        protected DataTable Query(string sql)
        {
            SqlConnection conn = null;
            try
            {
                conn = GetSqlConnection();
                conn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}

⌨️ 快捷键说明

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