C#对Access数据库的增删改查

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
 
/// 
///Y_DBClass 的摘要说明  对Access数据库的操作
/// 
public class Y_DBClass
{
    public Y_DBClass()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static readonly string conStr 
    = System.Configuration.ConfigurationManager.AppSettings["AccessConnString"].ToString() 
    + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"]) 
    + ";";
 
 
    /// 
    ///获取连接
    /// 
    public static OleDbConnection getCon()
    {
        OleDbConnection conn = new OleDbConnection(conStr);
        return conn;
    }
 
    /// 
    ///输入sql语句  获取dataset 参数 sqlStr:sql语句,tablename:表名 
    /// 
    public static DataSet getDataSet(string sqlStr, string tablename)
    {
        try
        {
            OleDbConnection conn = getCon();
            conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(sqlStr,conn);
            adapter.Fill(ds,tablename);
            conn.Close();
            conn.Dispose();
            return ds;
 
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
    /// 
    ///输入sql语句 执行插入,删除,更新操作,返回true(成功) false(失败)  参数 sqlStr:sql语句
    /// 
    public static bool indeupData(string sqlStr)
    {
        OleDbConnection conn = getCon();
        try
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
            int i = cmd.ExecuteNonQuery();
            if (i <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }
    }
    /// 
    ///输入sql语句 验证数据是否存在,存在返回true 不存在返回false  
    ///参数 sqlStr:sql语句:select count(*) from tablename
    /// 
    public static bool isHaveData(string sqlStr)
    {
        OleDbConnection conn = getCon();
        try
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(sqlStr,conn);
            int i =Convert.ToInt16(cmd.ExecuteScalar());
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }
    }
    /// 
    ///输入sql语句 获取首行首列值 类型string  参数 sqlStr:sql语句
    /// 
    public static string getFirstString(string sqlStr)
    {
        OleDbConnection conn = getCon();
        try
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
            string firstStr= cmd.ExecuteScalar().ToString();
            return firstStr;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }
    }
}



转载请注明:http://itsshq.com/article-231.html

你可能感兴趣的:(编程语言)