asp.net 操作Access数据库

3 篇文章 0 订阅
2 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web.SessionState;

/// <summary>
///Method 的摘要说明
/// </summary>
public class Method
{
 public Method()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }


    public static readonly string cnnstring=ConfigurationManager.ConnectionStrings["con"].ConnectionString;
    /// <summary>
    /// olecmd为要执行的命令
    /// commandtype是命令的类型
    /// para是参数集合
    /// 返回类型为DataTable
    /// </summary>
    /// <param name="sqlcmd"></param>
    /// <param name="commandtype"></param>
    /// <param name="para"></param>
    /// <returns></returns>
    public static DataTable Returm(string olecmd,CommandType commandtype,params OleDbParameter[] para)
    {

        using (OleDbConnection con = new OleDbConnection(cnnstring))
        {
            using (OleDbCommand cmd = new OleDbCommand(olecmd,con))
            {
                cmd.Parameters.AddRange(para);
                using (OleDbDataAdapter oda = new OleDbDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    oda.Fill(dt);
                    return dt;
                }
            }
        }

 

    }
    /// <summary>
    /// olecmd要执行的命令
    /// commandtype命令类型
    /// parar为命令的参数
    /// 返回类型为void
    /// 执行的操作为,增删除,修改,
    /// </summary>
    /// <param name="sqlcmd"></param>
    /// <param name="commandtype"></param>
    /// <param name="parar"></param>
    public static void OleExecute(string olecmd,CommandType commandtype,params OleDbParameter[] parar)
    {
        using (OleDbConnection con = new OleDbConnection(cnnstring))
        {
            using (OleDbCommand cmd = new OleDbCommand(olecmd,con))
            {
                cmd.Parameters.AddRange(parar);
                con.Open();
                cmd.ExecuteNonQuery();
            }
        }   
   
   
    }
    /// <summary>
    /// olecmd要执行的命令
    /// commandtype命令类型
    /// parar为命令的参数
    /// 返回类型为string
    /// 判断记录是否存在
    /// 以及返回符合条件的总记录数
    /// 适用于用户登录
    /// </summary>
    /// <param name="olecmd"></param>
    /// <param name="commantye"></param>
    /// <param name="para"></param>
    /// <returns></returns>
    public static string OleYnRecord(string olecmd,CommandType commantye, params OleDbParameter[] para)
    {
        using (OleDbConnection con = new OleDbConnection(cnnstring))
        {
            using (OleDbCommand cmd = new OleDbCommand(olecmd,con))
            {
                DataSet ds = new DataSet();
                cmd.Parameters.AddRange(para);
                using (OleDbDataAdapter ada = new OleDbDataAdapter(cmd))
                {
                    ada.Fill(ds);
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        return "no";                              //无找到记录返回no
                    }
                    else if (ds.Tables[0].Rows.Count == 1)
                    {
                        return "yes";                            //存在一条记录返回yes
                    }
                    else
                    {
                        return ds.Tables[0].Rows.Count.ToString(); //返回总记录数
                    }
                }
            }
        }
   
    }

   
      


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值