数据加载中……
C# 连接ACCESS类

定义AccessDbClass类

变量声明处

       
string Dbpath= Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
        
//  string Dbpath = "D:\\我的文档\\Visual Studio 2005\\Projects\\hrmgl\\hrmgl\\database\\db.mdb";
        ///  构造函数与连接关闭数据库
        
/// 构造函数
        
/// 
        
///ACCESS数据库路径

        public AccessDbClass()
        
{
           ConnString 
= "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Dbpath + @"\database\db.mdb" ;
             
//  ConnString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Dbpath;
            Conn = new OleDbConnection(ConnString);
            Conn.Open();
        }

        
///
        
/// 打开数据源链接
        
/// 
        
/// 

        public OleDbConnection DbConn()
        
{
            Conn.Open();
            
return Conn;
        }


        
/// 
        
/// 请在数据传递完毕后调用该函数,关闭数据链接。
        
/// 

        public void Close()
        
{
            Conn.Close();
        }






        
/// 数据库基本操作
        
/// 根据SQL命令返回数据DataTable数据表,
        
/// 可直接作为dataGridView的数据源
        
/// 
        
/// 

        public DataTable SelectToDataTable(string SQL)
        
{
            OleDbDataAdapter adapter 
= new OleDbDataAdapter();
            OleDbCommand command 
= new OleDbCommand(SQL, Conn);
            adapter.SelectCommand 
= command;
            DataTable Dt 
= new DataTable();
            adapter.Fill(Dt);
            
return Dt;
        }

        
/// 
        
/// 根据SQL命令返回数据DataSet数据集,其中的表可直接作为dataGridView的数据源。
        
/// 
        
/// 


        
/// 在返回的数据集中所添加的表的名称
        
/// 

        public DataSet SelectToDataSet(string SQL, string subtableName)
        
{
            OleDbDataAdapter adapter 
= new OleDbDataAdapter();
            OleDbCommand command 
= new OleDbCommand(SQL, Conn);
            adapter.SelectCommand 
= command;
            DataSet Ds 
= new DataSet();
            Ds.Tables.Add(subtableName);
            adapter.Fill(Ds, subtableName);
            
return Ds;
        }

posted on 2010-06-20 17:06 seaship 阅读(696) 评论(0)  编辑 收藏 引用 所属分类: C#

只有注册用户登录后才能发表评论。