| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1742 人关注过本帖
标题:自己写的一个数据类,有异常,大家指点
只看楼主 加入收藏
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
 问题点数:0 回复次数:16 
自己写的一个数据类,有异常,大家指点
using System;
using System.Data ;
using System.Data.OleDb ;

namespace EIP.SQLconn
{
    public class DataAccess
    {
        private OleDbConnection OleConnString ;
        private OleDbTransaction trans ;
        /******************************************************************************
         * FunName    :    ExecSqlInTrans
         * Input    :    strSqls
         * Return    :    bool
         * Describe    :    
         *        This function is to execute a set of sql strings splitted by ";", such as
         * "insert into acl values('admin','UserMng');insert into acl values('Jason','LogMng')" ;
         *
         * In the example as up, the function can execute two sql strings in one transaction. when
         * one sql string executing is failure, the two executing will both rollback to the old
         * state.
         *
         * ****************************************************************************/
        public bool ExecSqlInTrans(string strSqls)
        {
            OleConnString = GetConnect() ;
            OleDbCommand OleCmd = new OleDbCommand("",OleConnString) ;
            OleConnString.Open() ;
            trans = OleConnString.BeginTransaction() ;
            OleCmd.Transaction = trans ;
            try
            {
                int i = 0 ;
                string[] strSql = strSqls.Split(';') ;
                for(i=0;i<strSql.Length;i++)
                {
                     = strSql[i] ;
                    if(OleCmd.ExecuteNonQuery() < 0)
                    {
                        OleCmd.Transaction.Rollback() ;
                        return false ;
                    }
                }
                OleCmd.() ;
                return true ;
            }
            catch
            {
                OleCmd.Transaction.Rollback() ;
                return false ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    GetConnect
         * Input    :
         * Return    :    OleDbConnection
         * Describe    :    
         *        Create the database connection and return OleDbConnection.
         *
         * ****************************************************************************/
        public OleDbConnection GetConnect()
        {
            string strOleSqlConn = System.Configuration.ConfigurationSettings.AppSettings["Connection"] ;
            OleConnString = new OleDbConnection(strOleSqlConn) ;
            return OleConnString ;
        }
        /******************************************************************************
         * FunName    :    GetDataReader
         * Input    :    strSql
         * Return    :    OleDbDataReader
         * Describe    :    
         *        Execute the query and return OleDbDataReader.
         *
         * ****************************************************************************/
        public OleDbDataReader GetDataReader(string strSql)
        {
            OleConnString = GetConnect() ;
            OleConnString.Open() ;
            try
            {
                OleDbCommand OleCmd = new OleDbCommand(strSql,OleConnString) ;
                OleDbDataReader OleDr = OleCmd.ExecuteReader() ;
                return OleDr ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    GetDataSet
         * Input    :    strSql
         * Return    :    DataSet
         * Describe    :    
         *        Execute the query and return DataSet.
         *
         * ****************************************************************************/
        public DataSet GetDataSet(string strSql)
        {
            OleConnString = GetConnect() ;
            OleConnString.Open() ;
            try
            {
                DataSet OleDs = new DataSet() ;
                OleDbDataAdapter OleDa = new OleDbDataAdapter(strSql,OleConnString) ;
                OleDa.Fill(OleDs) ;
                return OleDs ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    CreateDataView
         * Input    :    strSql
         * Return    :    DataView
         * Describe    :    
         *        Execute the query and return DataView.
         *
         * ****************************************************************************/
        public DataView CreateDataView(string strSql)
        {
            OleConnString = GetConnect() ;
            OleConnString.Open() ;
            try
            {
                DataSet DS = new DataSet() ;
                OleDbDataAdapter OleDa = new OleDbDataAdapter(strSql,OleConnString) ;
                OleDa.Fill(DS) ;
                DataView DV = new DataView() ;
                DV.Table = DS.Tables[0] ;
                return DV ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    CreateDataTable
         * Input    :    strSql
         * Return    :    CreateDataTable
         * Describe    :    
         *        Execute the query and return CreateDataTable.
         *
         * ****************************************************************************/
        public DataTable CreateDataTable(string strSql)
        {
            OleConnString = GetConnect() ;
            OleConnString.Open() ;
            try
            {
                DataSet DS = new DataSet() ;
                OleDbDataAdapter OleDa = new OleDbDataAdapter(strSql,OleConnString) ;
                OleDa.Fill(DS) ;
                DataTable DT = new DataTable() ;
                DT = DS.Tables[0] ;
                return DT ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    ExecSql
         * Input    :    strSql
         * Return    :    bool
         *    if ture, successful;if false, failure
         * Describe    :    
         *        Execute the query.
         *
         * ****************************************************************************/
        public bool ExecSql(string strSql)
        {
            OleConnString = GetConnect() ;
            OleConnString.Open();
            OleDbCommand OleCmd = new OleDbCommand(strSql,OleConnString) ;
            
            try
            {
                if( OleCmd.ExecuteNonQuery() > 0)
                {
                    return true ;
                }
                else
                {
                    return false ;
                }
            }
            catch
            {
                return false ;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
        /******************************************************************************
         * FunName    :    GetIntFromDataReader
         * Input    :    strSql
         * Return    :    int
         * Describe    :    
         *        Execute the query and return int.
         *
         * ****************************************************************************/
        public int GetIntFromDataReader(string strSql)
        {
//            string sql="";
//            sql="select count(*) from BBS";            
//            EIP.SQLconn.DataAccess DataAs = new EIP.SQLconn.DataAccess();
            OleConnString = GetConnect() ;
            OleConnString.Open() ;
            try
            {
                OleDbCommand OleCmd = new OleDbCommand(strSql,OleConnString) ;
                OleDbDataReader OleDr = OleCmd.ExecuteReader() ;
                OleDr.Read();
                int i = OleDr.GetInt32(0);
                OleDr.Close();
                return i;
            }
            finally
            {
                OleConnString.Close() ;
            }
        }
    }
}
搜索更多相关主题的帖子: 数据 using System Data private 
2008-04-25 15:16
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
得分:0 
public bool ExecSql(string strSql) 这个方法有点异常,在OleConnString.Open();和if( OleCmd.ExecuteNonQuery() > 0)有异常,帮忙看看 急!!!!
2008-04-25 15:21
chenny333
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-2-16
收藏
得分:0 
异常详情?
2008-04-25 15:27
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
得分:0 
OleConnString.Open(); 有下面两个异常
//System.InvalidOperationException;
//System.Data.OleDb.OleDbException;
if( OleCmd.ExecuteNonQuery() > 0):异常是//System.InvalidOperationException
2008-04-25 15:33
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
得分:0 
高手帮忙看看
2008-04-25 15:34
chenny333
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-2-16
收藏
得分:0 
public OleDbConnection GetConnect()
        {
            string strOleSqlConn = System.Configuration.ConfigurationSettings.AppSettings["Connection"] ;
            if (OleConnString == null)
            {
                OleConnString = new OleDbConnection(strOleSqlConn);
            }
            OleConnString = new OleDbConnection(strOleSqlConn) ;
            return OleConnString ;
        }

open这里判断下
if (OleConnString .State == System.Data.ConnectionState.Closed)
                OleConnString .Open();
2008-04-25 15:39
chenny333
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-2-16
收藏
得分:0 
public OleDbConnection GetConnect()
最好用void
本类操作不需要返回
2008-04-25 15:41
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
得分:0 
没用啊 ,还是一样的 ,晕倒啊
2008-04-25 16:00
huchao0911
Rank: 1
等 级:新手上路
帖 子:55
专家分:5
注 册:2008-4-25
收藏
得分:0 
苦恼啊
2008-04-25 16:01
chenny333
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-2-16
收藏
得分:0 
你断点设置看下异常catch到的是什么内容~还有你oledb操作的是什么?execl?
2008-04-25 16:19
快速回复:自己写的一个数据类,有异常,大家指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019672 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved