Redisdemo

Dal:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication_Redis.DAL
{
public static class DBHelper
{
//获取web.config里的连接字符串
public static string strCon = " Data Source=.;Initial Catalog=IOT1609B;Integrated Security=True";
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="str">连接字符串</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql)
{
//实例化连接对象
SqlConnection conn = new SqlConnection(strCon);
int result = 0;
try
{
conn.Open();
//实例化命令对象
SqlCommand cmd = new SqlCommand(sql, conn);
//执行命令
result = cmd.ExecuteNonQuery();
}
catch (Exception)
{

throw;
}
finally {
conn.Close();
}
return result;
}
/// <summary>
/// 获取表格
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="str">连接字符串</param>
/// <returns></returns>
public static DataTable GetDataTable(string sql)
{
//实例化连接对象
SqlConnection conn = new SqlConnection(strCon);
DataTable dt = new DataTable();

try
{
//实例化适配器
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
sda.Fill(dt);

}
catch (Exception)
{

throw;
}
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="str">连接字符串</param>
/// <returns></returns>
public static SqlDataReader GetDataReader(string sql)
{
//实例化连接对象
SqlConnection conn = new SqlConnection(strCon);
SqlDataReader sdr;
try
{
conn.Open();
//实例化命令对象
SqlCommand cmd = new SqlCommand(sql, conn);
sdr = cmd.ExecuteReader();

}
catch (Exception)
{

throw;
}
return sdr;
}
/// <summary>
/// 返回单行单列
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="str">连接字符串</param>
/// <returns></returns>
public static int ExecuteScalar(string sql)
{
//实例化连接对象
SqlConnection conn = new SqlConnection(strCon);
int result = 0;
try
{
conn.Open();
//实例化命令对象
SqlCommand cmd = new SqlCommand(sql, conn);
result = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (Exception)
{

throw;
}
finally {

conn.Close();
}
return result;
}
}
}

业务逻辑:

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication_Redis.Models;

namespace WebApplication_Redis.RedisDB
{
public class RedisBLL
{
RedisClient client = RedisHelper.GetRedisClient();
string querykey = "query";
string addkey = "add";
string updatekey = "update";
string deletekey = "delete";

public List<Student> GetStuList(string opname)
{
if (!client.ContainsKey(querykey))
{
var list = new List<Student>();
client.Set<List<Student>>(querykey, list);
}
return RedisHelper.GetRedisClient().Get<List<Student>>(querykey);
}

public void KeepSameWithDB()
{
var list = new List<Student>();
client.Set<List<Student>>("query", list);
}

public int Add(Student stu)
{
RedisHelper.GetRedisClient().Set<Student>(addkey,stu);
return 0;
}
public int Update(Student stu)
{
RedisHelper.GetRedisClient().Set<Student>(updatekey, stu);
return 0;
}

public int Delete(List<int> ids)
{
RedisHelper.GetRedisClient().Set<List<int>>(deletekey, ids);
return 0;
}

}
}

redishelper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceStack.Redis;

namespace WebApplication_Redis.RedisDB
{
public class RedisHelper
{
private RedisHelper()
{ }
private static RedisClient _RedisClient;

public static RedisClient GetRedisClient()
{
if (_RedisClient == null)
{
_RedisClient = new RedisClient("127.0.0.1", 6379);
_RedisClient.FlushAll();//清空数据库
}
return _RedisClient;
}
}
}

视图:

 

posted @ 2020-08-23 20:49  是谁来了  阅读(102)  评论(0)    收藏  举报