王彬 程序员 王彬 ASP PHP C# .NET AJAX 笔记

我的PHP,.NET开源之路

using System;
using System.Data;
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;
using System.Data.SqlClient;

/// <summary>
/// DataBase 的摘要说明
/// </summary>

public class DataBase
{
    
private static DataBase _instance;
    
public DataBase()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
/// <summary>
    
/// 获得一个DataBase实例
    
/// </summary>
    
/// <returns></returns>

    public static DataBase getInstance()
    
{
        
if (_instance == null)
        
{
            _instance 
= new DataBase();
        }

        
return _instance;
    }

    
/// <summary>
    
/// 返回SqlConnection对象实例
    
/// </summary>
    
/// <returns></returns>

    public static SqlConnection ReturnCon()
    
{
        
string connstr=ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection con 
= new SqlConnection(connstr);
        
if (con.State.Equals(ConnectionState.Closed))
        
{
            con.Open();
        }

        
return con;
    }

    
/// <summary>
    
/// 返回SqlCommand对象实例
    
/// </summary>
    
/// <param name="procName"></param>
    
/// <returns></returns>

    public static SqlCommand CreateCmd(string procName)
    
{
        SqlConnection con 
= ReturnCon();
        SqlCommand cmd 
= new SqlCommand(procName,con);
        cmd.CommandType 
= CommandType.StoredProcedure;
        
return cmd;
    }

    
public static SqlCommand CreateCmd(string procName, SqlParameter[] prams)
    
{
        SqlConnection con 
= ReturnCon();
        SqlCommand cmd 
= new SqlCommand(procName,con);
        cmd.CommandType 
= CommandType.StoredProcedure;
        
foreach (SqlParameter parameter in prams)
        
{
            cmd.Parameters.Add(parameter);
        }

        
return cmd;
    }

    
public static SqlCommand CreateCmd(string procName, SqlConnection con)
    
{
        SqlConnection sqlcon 
= con;
        
if (sqlcon.State.Equals(ConnectionState.Closed))
        
{
            sqlcon.Open();
        }

        SqlCommand cmd 
= new SqlCommand(procName,sqlcon);
        cmd.CommandType 
= CommandType.StoredProcedure;
        
return cmd;
    }

    
public static SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlConnection con)
    
{
        SqlConnection sqlcon 
= con;
        
if (sqlcon.State.Equals(ConnectionState.Closed))
        
{
            sqlcon.Open();
        }

        SqlCommand cmd 
= new SqlCommand(procName,sqlcon);
        cmd.CommandType 
= CommandType.StoredProcedure;
        
foreach (SqlParameter parameter in prams)
        
{
            cmd.Parameters.Add(parameter);
        }

        
return cmd;
    }

    
/// <summary>
    
/// 返回SqlDataReader对象实例
    
/// </summary>
    
/// <param name="procName"></param>
    
/// <returns></returns>

    public static SqlDataReader RunProcGetReader(string procName)
    
{
        SqlCommand cmd 
= CreateCmd(procName);
        SqlDataReader sdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
return sdr;
    }

    
public static SqlDataReader RunProGetReader(string procName, SqlParameter[] prams)
    
{
        SqlCommand cmd 
= CreateCmd(procName, prams);
        SqlDataReader sdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
return sdr;
    }

    
public static SqlDataReader RunProGetReader(string procName, SqlConnection con)
    
{
        SqlCommand cmd 
= CreateCmd(procName, con);
        SqlDataReader sdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
return sdr;
    }

    
public static SqlDataReader RunProGetReader(string procName, SqlParameter[] prams, SqlConnection con)
    
{
        SqlCommand cmd 
= CreateCmd(procName, prams, con);
        SqlDataReader sdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
        
return sdr;
    }

    
/// <summary>
    
/// 返回DataTable对象实例
    
/// </summary>
    
/// <param name="procName"></param>
    
/// <param name="prams"></param>
    
/// <param name="con"></param>
    
/// <returns></returns>

    public static DataTable RunProTable(string procName, SqlParameter[] prams, SqlConnection con)
    
{
        SqlCommand cmd 
= CreateCmd(procName, prams, con);
        SqlDataAdapter sda 
= new SqlDataAdapter(cmd);
        DataSet ds 
= new DataSet();
        sda.Fill(ds);
        DataTable dt
=ds.Tables[0];
        con.Close();
        
return dt;
    }

    
/// <summary>
    
/// 返回执行结果受影响的行数
    
/// </summary>
    
/// <param name="procName"></param>
    
/// <returns></returns>

    public static int RunProExecute(string procName)
    
{
        SqlCommand cmd 
= CreateCmd(procName);
        
int intResult = cmd.ExecuteNonQuery();
        
return intResult;
    }

    
/// <summary>
    
/// 返回首行首列
    
/// </summary>
    
/// <param name="procName"></param>
    
/// <returns></returns>

    public static int RunProScalar(string procName)
    
{
        SqlCommand cmd 
= CreateCmd(procName);
        
int intResult = Convert.ToInt32(cmd.ExecuteScalar());
        
return intResult;
    }

    
public static int RunProScalar(string procName, SqlParameter[] prams)
    
{
        SqlCommand cmd 
= CreateCmd(procName, prams);
        
int intResult = Convert.ToInt32(cmd.ExecuteScalar());
        
return intResult;
    }

}

posted on 2008-01-11 06:34  wangbin  阅读(298)  评论(0编辑  收藏  举报