C# 读取写入数据到ACCESS数据库基于FRAMEWORK2.0

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class unsubscribe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string guid = Request.QueryString["guid"];
        string email = Request.QueryString["email"];
        if(guid!=null&&guid!=""&&email!=null&&email!=""){

            string filePath = ConfigurationManager.AppSettings["filePath"].ToString();
            bool res = checkData(filePath, guid, email);
            if(!res){//如果不存在则写入
                 saveData(filePath, guid, email);
                 lblresult.Text = "退订成功!";
            }
            else
            {
                lblresult.Text = "请不要重复提交!";
            }
           
        }
        else
        {
            lblresult.Text = "无效的参数!";
        }
        
    }


    static void Main(string[] args)
    {

    }



    /// <summary>
    /// 获取ACCESS文件,写数据到.mdb
    /// </summary>
    /// <returns></returns>
    public static OleDbConnection getConn(String filePath)
    {
        string connstr = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="+filePath;
        OleDbConnection tempconn = new OleDbConnection(connstr);
        return tempconn;
    }


    /// <summary>
    /// 去重复数据
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="guid"></param>
    /// <param name="email"></param>
    /// <returns></returns>
    public static bool checkData(string filePath, string guid, string email)
    {

        bool res = false; //定义返回值,并设置初值

        try
        {
            OleDbConnection conn = getConn(filePath); //getConn():得到连接对象
            conn.Open();
            string sql = "select * from unsubscribe  where EUD_GUID='" + guid + "' and EUD_EMAIL = '" + email+"'";
            OleDbCommand myCommand = new OleDbCommand(sql, conn);
            OleDbDataReader reader = myCommand.ExecuteReader(); //执行command并得到相应的DataReader
            //下面把得到的值
            if (reader!=null&&reader.Read())
            {
                if (!reader["id"].ToString().Equals(""))
                res = true;
            }
            else //如没有该记录,则返回false!
            {
                res = false;
            }
            reader.Close();
            conn.Close();
        }
        catch (Exception e)
        {
            throw(new Exception("数据库出错:" + e.Message)) ;
        }


        return res;
    }




    /// <summary>
    /// 写入数据到ACCESS中
    /// </summary>
    /// <returns></returns>
    public static bool saveData(string filePath, string guid, string email)
    {


        bool tempvalue = false; //定义返回值,并设置初值
        //下面把note中的数据添加到数据库中!
        try
        {

            OleDbConnection conn = getConn(filePath); //getConn():得到连接对象
            conn.Open();

            //设置SQL语句
            String sql = "INSERT INTO unsubscribe(EUD_GUID,EUD_EMAIL,EUD_CREATE_DATE)";
            sql += " VALUES(";
            sql += "'" + guid+ "',";
            sql += "'" + email + "',";
            sql += "now()";
            sql += ")";

            OleDbCommand insertcmd = new OleDbCommand(sql, conn);
            insertcmd.ExecuteNonQuery();

            conn.Close();
            tempvalue = true;
        }
        catch (Exception e)
        {
            throw (new Exception("数据库出错:" + e.Message));
        }
        return tempvalue;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值