📄 access.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Collections;
using Model;
using IDAL;
namespace DAL
{
/// <summary>
/// Access留言本数据访问层
/// </summary>
public class AccessGuestBook : Iguestbook //继承Iguestbook接口
{
private string connectionString = System.Configuration.ConfigurationSettings.AppSettings["accessConectionString"] + System.Web.HttpContext.Current.Server.MapPath("/App_Data/myPage.mdb");
/// <summary>
/// 读取数据库中所有留言
/// </summary>
/// <returns>返回guestbool数组</returns>
public guestbook[] getListGuestBook()
{
using (OleDbConnection oleConn = new OleDbConnection(connectionString))
{
OleDbCommand oleCmd = new OleDbCommand("select * from guestbook order by id desc", oleConn);
oleConn.Open();
ArrayList guest = new ArrayList();
OleDbDataReader oleDdr = oleCmd.ExecuteReader();
while (oleDdr.Read())
{
guestbook gb = new guestbook();
gb.Id = oleDdr["id"].ToString();
gb.Name = oleDdr["name"].ToString();
gb.Time =DateTime.Parse (oleDdr["times"].ToString());
gb.Ip = oleDdr["ip"].ToString();
gb.Content = oleDdr["content"].ToString();
gb.Recontent = oleDdr["recontent"].ToString();
gb.Pic = oleDdr["pic"].ToString();
gb.Email = oleDdr["email"].ToString();
guest.Add(gb);
}
oleDdr.Close();
return (guestbook[])guest.ToArray(typeof(guestbook));
}
}
/// <summary>
/// 读取一条留言内容
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public guestbook[] getOnly(string id)
{
using (OleDbConnection oleConn = new OleDbConnection(connectionString))
{
OleDbCommand oleCmd = new OleDbCommand("select * from guestbook where id=" + id, oleConn);
oleConn.Open();
ArrayList guest = new ArrayList();
OleDbDataReader oleDdr = oleCmd.ExecuteReader();
while (oleDdr.Read())
{
guestbook gb = new guestbook();
gb.Id = oleDdr["id"].ToString();
gb.Name = oleDdr["name"].ToString();
gb.Time =DateTime .Parse ( oleDdr["times"].ToString());
gb.Ip = oleDdr["ip"].ToString();
gb.Content = oleDdr["content"].ToString();
gb.Recontent = oleDdr["recontent"].ToString();
gb.Pic = oleDdr["pic"].ToString();
gb.Email = oleDdr["email"].ToString();
guest.Add(gb);
}
oleDdr.Close();
return (guestbook[])guest.ToArray(typeof(guestbook));
}
}
/// <summary>
/// 添加留言内容
/// </summary>
/// <param name="gb">留言实体对像</param>
public void setGuestBook(guestbook gb)
{
using(OleDbConnection oleCon=new OleDbConnection (this.connectionString ))
{
string sql = "insert into guestbook (name,times,ip,content,pic,email) values ( @name , @times , @ip , @content , @pic ,@email)";
OleDbParameter oleParam0 = new OleDbParameter("@name", gb.Name);
OleDbParameter oleParam1 = new OleDbParameter("@times", gb.Time);
OleDbParameter oleParam2 = new OleDbParameter("@ip", gb.Ip);
OleDbParameter oleParam3 = new OleDbParameter("@content", gb.Content );
OleDbParameter oleParam4 = new OleDbParameter("@pic",gb.Pic );
OleDbParameter oleParam5 = new OleDbParameter("@email", gb.Email );
OleDbCommand oleCmd = new OleDbCommand();
oleCmd.CommandText = sql;
oleCmd.Parameters.Add(oleParam0);
oleCmd.Parameters.Add(oleParam1);
oleCmd.Parameters.Add(oleParam2);
oleCmd.Parameters.Add(oleParam3);
oleCmd.Parameters.Add(oleParam4);
oleCmd.Parameters.Add(oleParam5);
oleCon.Open();
oleCmd.Connection = oleCon;
oleCmd.ExecuteNonQuery();
}
}
/// <summary>
/// 回复留言
/// </summary>
/// <param name="Regb"></param>
public void setReGuestBook(guestbook Regb)
{
using (OleDbConnection oleCon = new OleDbConnection(this.connectionString))
{
string sql = "update guestbook set recontent=@recontent where id=" + Regb.Id;
OleDbParameter oleParam0 = new OleDbParameter("@recontent", Regb.Recontent);
OleDbCommand oleCmd = new OleDbCommand();
oleCmd.CommandText = sql;
oleCmd.Parameters.Add(oleParam0);
oleCon.Open();
oleCmd.Connection = oleCon;
oleCmd.ExecuteNonQuery();
}
}
/// <summary>
/// 删除留言
/// </summary>
/// <param name="id">留言id号</param>
/// <returns></returns>
public bool delGuestBook(string id)
{
using (OleDbConnection oleCon = new OleDbConnection(this.connectionString))
{
string sql = "delete from [guestbook] where id=@id";
OleDbParameter oleParam0 = new OleDbParameter("@id", id);
OleDbCommand oleCmd = new OleDbCommand();
oleCmd.CommandText = sql;
oleCmd.Parameters.Add(oleParam0);
oleCon.Open();
oleCmd.Connection = oleCon;
try
{
oleCmd.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -