贡献自己以前在网上搜索和总结的两个通用的数据库公用操作类

类一(DBO.cs):主要用来操作存储过程
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Data;
using  System.Data.SqlClient;

namespace  ProductManager
{
    
//主要用来操作存储过程,当然直接操作SQL语句也行
      public class DBO
       
{
        
private static string ConStr = "server=localhost;database=ProductManager;uid=sa;pwd=";//或从web.config中读取,此句可变

        
private SqlConnection _conn;
        
private SqlCommand _comm;
        
private void CreateCon()//创建数据库连接。
        {
            _conn 
= new SqlConnection(ConStr);
        }


        
...
    }

}

类二(DB.CS):主要用来操作SQL语句字符串
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;

namespace  XieTong_lhf
{

    
/*********************************************************************
    **作    用:把一些有关数据库的基本操作,写在这里.一般定义成静态函数

    *********************************************************************
*/

    
/// <summary>
    
/// 数据库通用操作类
    
/// </summary>

    public class DB
    
{

        
protected static SqlConnection conn = new SqlConnection();

        
protected static SqlCommand comm = new SqlCommand();

        
public DB()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }



        
/// <summary>
        
/// 返回数据库的连接语句,string类型
        
/// </summary>

        public static string ConnectionString()
        
{
            
return ConfigurationManager.AppSettings["ConnectString"].ToString();
        }


        
/// <summary>
        
/// 打开数据库
        
/// </summary>

        private static void openConnection()
        
{
            
if (conn.State == ConnectionState.Closed)
            
{
                conn.ConnectionString 
= ConfigurationManager.AppSettings["ConnectString"].ToString();
                comm.Connection 
= conn;
                
try
                
{
                    conn.Open();
                }

                
catch (Exception e)
                
{
                    
throw new Exception(e.Message);
                }

            }


        }


        
/// <summary>
        
/// 关闭数据库
        
/// </summary>

        private static void closeConnection()
        
{
            
if (conn.State == ConnectionState.Open)
            
{
                conn.Close();
                conn.Dispose();
                comm.Dispose();
            }

        }


        
/// <summary>
        
/// 执行sql语句,用于增加,删除,修改这种无返回的操作
        
/// </summary>

        public static void excuteSql(string sqlstr)
        
{
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                comm.ExecuteNonQuery();
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{ closeConnection(); }
        }


        
/// <summary>
        
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭这个对象。用于查询语句
        
/// </summary>

        public static SqlDataReader dataReader(string sqlstr)
        
{
            SqlDataReader dr 
= null;
            
try
            
{
                openConnection();
                comm.CommandText 
= sqlstr;
                comm.CommandType 
= CommandType.Text;

                dr 
= comm.ExecuteReader(CommandBehavior.CloseConnection);
            }

            
catch
            
{
                
try
                
{
                    dr.Close();
                    closeConnection();
                }

                
catch { }
            }

            
return dr;
        }


        
/// <summary>
        
/// 返回指定sql语句的SqlDataReader对象,使用时请注意关闭。用于查询语句
        
/// </summary>

        public static void dataReader(string sqlstr, ref SqlDataReader dr)
        
{
            
try
            
{
                openConnection();
                comm.CommandText 
= sqlstr;
                comm.CommandType 
= CommandType.Text;
                dr 
= comm.ExecuteReader(CommandBehavior.CloseConnection);
            }

            
catch
            
{
                
try
                
{
                    
if (dr != null && !dr.IsClosed)
                        dr.Close();
                }

                
catch
                
{
                }

                
finally
                
{
                    closeConnection();
                }

            }

        }


        
/// <summary>
        
/// 返回指定sql语句的dataset
        
/// </summary>

        public static DataSet dataSet(string sqlstr)
        
{
            DataSet ds 
= new DataSet();
            SqlDataAdapter da 
= new SqlDataAdapter();
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                da.SelectCommand 
= comm;
                da.Fill(ds);

            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{
                closeConnection();
            }

            
return ds;
        }


        
/// <summary>
        
/// 返回指定sql语句的dataset
        
/// </summary>

        public static void dataSet(string sqlstr, ref DataSet ds)
        
{
            SqlDataAdapter da 
= new SqlDataAdapter();
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                da.SelectCommand 
= comm;
                da.Fill(ds);
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{
                closeConnection();
            }

        }


        
/// <summary>
        
/// 返回指定sql语句的datatable
        
/// </summary>

        public static DataTable dataTable(string sqlstr)
        
{
            DataTable dt 
= new DataTable();
            SqlDataAdapter da 
= new SqlDataAdapter();
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                da.SelectCommand 
= comm;
                da.Fill(dt);
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{
                closeConnection();
            }

            
return dt;
        }


        
/// <summary>
        
/// 返回指定sql语句的datatable
        
/// </summary>

        public static void dataTable(string sqlstr, ref DataTable dt)
        
{
            SqlDataAdapter da 
= new SqlDataAdapter();
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                da.SelectCommand 
= comm;
                da.Fill(dt);
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{
                closeConnection();
            }

        }


        
/// <summary>
        
/// 返回指定sql语句的dataview
        
/// </summary>

        public static DataView dataView(string sqlstr)
        
{
            SqlDataAdapter da 
= new SqlDataAdapter();
            DataView dv 
= new DataView();
            DataSet ds 
= new DataSet();
            
try
            
{
                openConnection();
                comm.CommandType 
= CommandType.Text;
                comm.CommandText 
= sqlstr;
                da.SelectCommand 
= comm;
                da.Fill(ds);
                dv 
= ds.Tables[0].DefaultView;
            }

            
catch (Exception e)
            
{
                
throw new Exception(e.Message);
            }

            
finally
            
{
                closeConnection();
            }

            
return dv;
        }




    }


}

由于这些也是我在网上总结的,所以不便藏私,出来混,迟早是要还的嘛,人人为我,我为人人,挥泪共享.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值