C# 读取写入数据到ACCESS数据库基于FRAMEWORK2.0
2017-02-16 18:22
561 查看
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; } }
相关文章推荐
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- c# 向记事本中(.txt)写入/读取数据
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#读取word模版并对指定域写入数据保存为新word
- C#程序中:如何向xml文件中写入数据和读取数据
- ADO.NET 2.0:如何排除错误信息「当目前没有数据时,尝试读取无效」(C#)
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- C#中创建、打开、读取、写入、保存Exce,C#如何向EXCEL写入数据
- 如何利用书签向word文档读取或写入数据[C#](
- C#2.0中,SerialPort如何读取串口数据并显示在TextBox上
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- ADO.NET 2.0:如何排除错误信息「当目前没有数据时,尝试读取无效」(C#)
- C#读取修改配制文件(Framework2.0)
- C#读取Excel数据并重新写入Excel
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 使用C#进行基于PI的开发(三)——应用PISDK和PIAPI从PI数据库读取数据