zoukankan      html  css  js  c++  java
  • 后台数据使用datareader方法

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    /// <summary>
    /// Summary description for DataManager
    /// </summary>
    public sealed class DataManager
    {
        private SqlConnection _connection;

        #region Constructors
        private static DataManager instance = new DataManager();
        public static DataManager Default
        {
            get
            {
                return instance;
            }
        }

        private DataManager()
        {
        }
        #endregion Constructor

        private SqlConnection RequestConnection()
        {
            // TODO: Add logic
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[Parameters.DefaultConnectionName].ConnectionString);
            connection.Open();
            return connection;
        }
        private void ReleaseConnect(SqlConnection connection)
        {
            // TODO Here
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            //connection.Close();
        }

        public int ExecuteCommand(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            SqlConnection connection = null;
            try
            {
                connection = this.RequestConnection();
                SqlCommand command = new SqlCommand(text, connection);
                return command.ExecuteNonQuery();
            }
            catch (SqlException exception)
            {
                return -1;
            }
            finally
            {
                this.ReleaseConnect(connection);
            }


        }

        public SqlDataReader ExecuteCommandAndReturnReader(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            SqlConnection connection = null;
            try
            {
                connection = this.RequestConnection();
                SqlCommand command = new SqlCommand(text, connection);
                return command.ExecuteReader();
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                this.ReleaseConnect(connection);
            }
        }

        public SqlDataReader ExecuteSpCommandAndReturnReader(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            SqlConnection connection = null;
            try
            {
                connection = this.RequestConnection();
                SqlCommand command = new SqlCommand(text, connection);
                command.CommandType = CommandType.StoredProcedure;
                return command.ExecuteReader();
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                this.ReleaseConnect(connection);
            }
        }

        public SqlDataReader ExecuteSpCommandAndReturnReader(string text,string parameter, int id)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            SqlConnection connection = null;
            try
            {
                connection = this.RequestConnection();
                SqlCommand command = new SqlCommand(text, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(parameter, SqlDbType.Int).Value = id;
                return command.ExecuteReader();
            }
            catch (Exception e)
            {
                return null;
            }
            finally
            {
                this.ReleaseConnect(connection);
            }
        }


    }


  • 相关阅读:
    iOS7上在xib中使用UITableViewController设置背景色bug
    Android 转载一篇.9图片详解文章
    Android 中4种屏幕尺寸
    网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
    防火墙简介
    Makefile有三个非常有用的变量。分别是$@,$^,$
    makefile简单helloworld
    Python异常处理try except
    shell 读取配置文件的方法
    ubuntu 添加开机启动服务
  • 原文地址:https://www.cnblogs.com/shilei/p/1028043.html
Copyright © 2011-2022 走看看