ASP.NET[C#]的ACCESS数据库操作类

access|asp.net|数据|数据库

ASP.NET[C#]的ACCESS数据库操作类

      //网上很多都是操作SQL SER 的,整理了一下,不是很完善,但不影响使用,呵呵……
       //private string datapatch = ConfigurationSettings.AppSettings["acessconn"];//数据库地址
                                   
        private string datapatch = "db/global.asa";//数据库地址
        ///
        /// 取得dataset
        ///
        /// 查询语句
        ///
        public  DataSet GetDataSet(string Commandtext)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                   "Data Source=" + Server.MapPath(datapatch);
            string strCommandText = Commandtext;

            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();

            myAdpater.Fill(myDataset);
           
            myConnection.Close();
            return myDataset;
        }
        ///
        /// 取得表
        ///
        /// 查询语句
        ///
        public  DataTable GetDataTable(string Commandtext)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);
            string strCommandText =Commandtext;

            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();

            myAdpater.Fill(myDataset);

            DataTable mytable = myDataset.Tables[0];

            myConnection.Close();
            return mytable;
        }
        ///
        /// 取得某行的某列的值
        ///
        /// 列的名称
        /// 所属表名
        /// 表的主键
        /// 列所属的主键值
        ///
        public  string GetDataColum(string strColumnName,string strTableName,string strColumnkey,string strColumnValue)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);
            string strCommandText = "select "+strColumnName+","+strColumnkey+" from "+strTableName;
           
            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();

            myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
           
            myAdpater.Fill(myDataset);
           
            myConnection.Close();
           
            DataTable mytable = myDataset.Tables[0];
            DataRow mydr = mytable.Rows.Find(strColumnValue);
            string mydc = mydr[strColumnName].ToString();
           
           
            return mydc;
        }
       

        ///
        /// 更新某个字段
        ///
        /// 要更新字段名称
        /// 要更新的值
        /// 所属表名称
        /// 表中KEY
        /// 表中KEY的值
        public  void UpdateColum(string strColumnName,string strValue,string strTableName,string strColumnKey,string strColumnValue)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);
            string strCommandText = "select "+strColumnKey+","+strColumnName+" from "+strTableName;
           
            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();
           
           
            myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            myAdpater.Fill(myDataset,strTableName);
           
            DataRow drFindRow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
 
            drFindRow[strColumnName] = strValue;
           
           
           
            myAdpater.Update(myDataset,strTableName);
           
            myConnection.Close();
           
        }

        ///
        /// 添加行
        ///
        /// 此行中的字段集合
        /// 此行中的字段集合的对应值
        /// 所属表名称
        /// 表中主键

        public  void AddRow(string[]columns,string[]columnvalue,string strTableName,string strColumnKey)
        {       
           
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);

            string strColumnCount = "";
            foreach(string thiscolunmname in columns)
            {
                strColumnCount = thiscolunmname+","+strColumnCount;
            }

            string strCommandText = "select "+strColumnCount+strColumnKey+" from "+strTableName;
           
            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();
           
           
            myAdpater.Fill(myDataset,strTableName);
       
            DataRow newrow = myDataset.Tables[strTableName].NewRow();
           
            for(int i=0;i<columns.Length;i++)
            {
                string strColumnName = columns[i].ToString();

                newrow[strColumnName] = columnvalue[i].ToString();
            }
               
           
            myDataset.Tables[strTableName].Rows.Add(newrow);
           
            myAdpater.Update(myDataset,strTableName);
           
            myConnection.Close();
           
        }
///
/// 更新数据行
///
/// 要更新的列集合
/// 要更新的列集合的对应值
/// 所属表名称
/// 表主键
/// 行所属的ID
        public  void updateRow(string[]columns,string[]columnvalue,string strTableName,string strColumnKey,string strColumnValue)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);

            string strColumnCount = "";
            foreach(string thiscolunmname in columns)
            {
                strColumnCount = thiscolunmname+","+strColumnCount;
            }

            string strCommandText = "select "+strColumnCount+strColumnKey+" from "+strTableName;
           
            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();
           
            myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            myAdpater.Fill(myDataset,strTableName);
       
            DataRow updaterow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
           
            for(int i=0;i<columns.Length;i++)
            {
                string strColumnName = columns[i].ToString();

                updaterow[strColumnName] = columnvalue[i].ToString();
            }
           
           
            myAdpater.Update(myDataset,strTableName);
           
            myConnection.Close();
           
        }

 
        ///
        /// 删除行
        ///
        /// 所属表
        /// 主键字段名
        /// 此行主键值
        public  void delectRow(string strTableName,string strColumnKey,string strColumnValue)
        {       

            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +
                "Data Source=" + Server.MapPath(datapatch);

            string strCommandText = "select "+strColumnKey+" from "+strTableName;
           
            OleDbConnection myConnection = new OleDbConnection(strConnection);
            myConnection.Open();

            OleDbDataAdapter myAdpater = new OleDbDataAdapter(strCommandText,myConnection);

            OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdpater);

            DataSet myDataset = new DataSet();
           
            myAdpater.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            myAdpater.Fill(myDataset,strTableName);
       
            DataRow newrow = myDataset.Tables[strTableName].Rows.Find(strColumnValue);
           
            newrow.Delete();
           
            myAdpater.Update(myDataset,strTableName);
           
            myConnection.Close();
           
        }
//----------------------------------------------------------------------------------
     
   

时间: 2024-08-01 13:57:29

ASP.NET[C#]的ACCESS数据库操作类的相关文章

Access数据库操作类

Access数据库操作类AccessHelper 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.HtmlControl

一个ASP.NET的MYSQL的数据库操作类自己封装的_实用技巧

/** * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility { /// <summary>对MYSQL数据库的操作类 /// /// </summary> public class MYSQLHelper { private MySqlConnection conn = null; private MyS

自己封装的ASP.NET的MYSQL的数据库操作类

代码 /** * 作者:牛腩 * 创建时间:2010年3月7日17时35分 * 类说明:对MYSQL数据库的操作类 */ using System;using System.Data;using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility{    /// <summary>对MYSQL数据库的操作类    ///     /// </summary>    public class MYSQLHelper    {  

access-wniform怎样对Access数据库操作

问题描述 wniform怎样对Access数据库操作 public static DataTable GetConnection(string sql) { string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:pdacss.mdb;Persist SecurityInfo=False"; OleDbConnection mycon = new OleDbConnection(strcon); mycon.Ope

asp Access和ms SQL2000 2005共用数据库操作类

<% '==================================== 'H----------Access和SQL共用数据库操作类,所有SQL语句以SQL为准 '==================================== Class DbClass     Public Conn  Private ConnStr  Private ErrorInfo     '初始化类     Public Sub Class_Initialize   Select Case Cfg.

[原创] EasyASP v1.5简化ASP开发,包含数据库操作类

EasyASP v1.5简化ASP开发,包含数据库操作类 EasyASP是一个方便快速开发ASP的类,其中包含了一个数据库控制类(原clsDbCtrl.asp,对原代码作了优化和修改,包含对数据库的各类操作及存储过程的调用,全部封装在Easp.db中,使用起来会更方便,调用也更简单).而Easp类中提供了大量实用的ASP通用过程及方法,可以简化大部分的ASP操作.目前只提供了VBScript版,JScript版将来可能会提供.详细说明请下载帮助手册,里面有非常详细的使用方法说明及源码范例. 源码

ASP通用数据库操作类源代码

<%'=========================================================================='文件名称:clsDbCtrl.asp'功 能:数据库操作类'作 者:coldstone (coldstone[在]qq.com)'程序版本:v1.0.5'完成时间:2005.09.23'修改时间:2007.10.30'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息.'          如果你修改了程序中的代码并得到更好的应用,

asp数据库操作类

<%'=========================================================================='文件名称:clsDbCtrl.asp'功 能:数据库操作类'作 者:coldstone (coldstone[在]qq.com)'程序版本:v1.0.5'完成时间:2005.09.23'修改时间:2007.10.30'版权声明:可以在任意作品中使用本程序代码,但请保留此版权信息.' 如果你修改了程序中的代码并得到更好的应用,请发送一份给我,谢

asp.net Oracle 数据库操作类

操作类 using System; using System.Data; using System.Configuration; using System.Data.OracleClient; using System.Text; using System.Windows.Forms; using System.Xml; using Transactions; /// <summary> /// DB 的摘要说明 Written By Luos.Luo ,the creator of Sale