最近做复旦国际交流学院的一个项目时,使用到了Access,自己写了一个AccessHelper...

网上估计有很多版本了,自己仿照SqlHelper实现了这个,用了感觉不错,欢迎提意见,欢迎介绍项目。谢谢大家了
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.OleDb;
None.gif
None.gif
namespace  DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// AccessHelper 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class AccessHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private AccessHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//静态构造函数,表示该类不可以被实例化
InBlock.gif            
//所以的成员都是static类型的
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private static string m_connection = "";
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// ConnectionString的属性
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static string ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_connection 
= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return m_connection;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 判断OleDbConnection是否已经连接
InBlock.gif        
/// </summary>
InBlock.gif        
/// <returns>true 表示OleDbConnection已经连接,或者设置连接成功; 
ExpandedSubBlockEnd.gif        
/// false 表示OleDbConnection尝试连接失败</returns>

InBlock.gif        public static bool IsConnected()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(m_connection);
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行一条Sql命令
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">Sql语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>表示影响的行数</returns>

InBlock.gif        public static int ExecuteText(string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd,CommandType.Text,cmdText);
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
catch (OleDbException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw (new Exception(e.Message));;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DisposeCmd(cmd);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行一条Sql命令(ExecuteScalar),返回一个结果值(int, string, float等类型)。如果不存在记录,返回为null。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">Sql语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回值(int, string, float等类型)。如果不存在记录,返回为null。</returns>

InBlock.gif        public static object ExecuteTextRet(string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, CommandType.Text, cmdText);
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return cmd.ExecuteScalar();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (OleDbException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw (new Exception(e.Message));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
            
InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DisposeCmd(cmd);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行一个Sql命令,返回一个DataSet结果集。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">Sql语句</param>
InBlock.gif        
/// <param name="TableName">Fill入DataSet中的表名</param>
ExpandedSubBlockEnd.gif        
/// <returns>DataSet结果集</returns>

InBlock.gif        public static DataSet ExecuteTextRet(string cmdText, string TableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            OleDbDataAdapter adp 
= new OleDbDataAdapter();
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, CommandType.Text, cmdText);
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    adp.SelectCommand 
= cmd;
InBlock.gif                    adp.Fill(ds,TableName);
InBlock.gif                    
return ds;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch (OleDbException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw (new Exception(e.Message));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DisposeCmd(cmd);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }
    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 把一个DataSet更新入数据库(可以修改,添加,删除)
InBlock.gif        
/// 但是,这个表的设计中必须要有主键
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">select语句,和要更新的表对应</param>
InBlock.gif        
/// <param name="ds">要更新入数据库的DataSet</param>
InBlock.gif        
/// <param name="TableName">DataSet中的源表名</param>
ExpandedSubBlockEnd.gif        
/// <returns>影响的行数</returns>

InBlock.gif        public static int ExecuteUpdate(string cmdText,DataSet ds,string TableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbDataAdapter adp 
= new OleDbDataAdapter();
InBlock.gif            OleDbCommand cmd 
= new OleDbCommand();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, CommandType.Text, cmdText);
InBlock.gif                adp.SelectCommand 
= cmd;
InBlock.gif                OleDbCommandBuilder builder 
= new OleDbCommandBuilder(adp);
InBlock.gif                
return adp.Update(ds,TableName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DisposeCmd(cmd);
InBlock.gif                adp.Dispose();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置OleDbCommand的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmd">OleDbCommand对象</param>
InBlock.gif        
/// <param name="cmdType">类型</param>
ExpandedSubBlockEnd.gif        
/// <param name="cmdText">Sql语句</param>        

InBlock.gif        private static void PrepareCommand(OleDbCommand cmd, CommandType cmdType, string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            OleDbConnection    conn 
= new OleDbConnection(m_connection);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif    
InBlock.gif                
if (cmd != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    cmd.Connection 
= conn;
InBlock.gif                    cmd.CommandText 
= cmdText;
InBlock.gif                    cmd.CommandType 
= cmdType;
ExpandedSubBlockEnd.gif                }
                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
catch (OleDbException e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw (new Exception(e.Message));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 处理OleDbCommand,关闭连接,并且释放内存。
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="cmd">要处理的OleDbCommand对象</param>

InBlock.gif        private static void DisposeCmd(OleDbCommand cmd)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (cmd.Connection != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Connection.Close();
InBlock.gif                cmd.Connection.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            cmd.Dispose();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
填加段#region 填加段
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据app.config获取连接字符串
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string GetConnectionStringFromConfigFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            ds.ReadXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);            
InBlock.gif            
string result = ds.Tables["appSettings"].Rows[0]["DateSource"].ToString();
InBlock.gif            ds.Clear();
InBlock.gif            ds.Dispose();
InBlock.gif
InBlock.gif            result 
= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + result;        
InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据app.config获取数据源的信息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static string GetDataSourceFromConfigFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            ds.ReadXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);            
InBlock.gif            
string result = ds.Tables["appSettings"].Rows[0]["DateSource"].ToString();
InBlock.gif            ds.Clear();
InBlock.gif            ds.Dispose();
InBlock.gif            
InBlock.gif            
return result;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置app.config的数据源信息
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="dataSource"></param>

InBlock.gif        public static void SetDataSourceToConfigFile(string dataSource)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            ds.ReadXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);            
InBlock.gif            ds.Tables[
"appSettings"].Rows[0]["DateSource"]=dataSource;
InBlock.gif            ds.AcceptChanges();
InBlock.gif            ds.WriteXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
InBlock.gif            ds.Clear();
InBlock.gif            ds.Dispose();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 测试能否连接(打开并关闭)
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool IsCanConnected()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbConnection conn 
= new OleDbConnection(m_connection);
InBlock.gif            
bool result = false;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Open();
InBlock.gif                result 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/neediss/archive/2005/07/13/192370.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值