Asp.net实现点赞功能
2020-07-20 04:21
1366 查看
在数据库中用一组数据来存放已经点赞过的人名,用半角符号分开,用ajax实现点赞的人名插入,并规定只能点赞一次。
$.ajax({ type: "GET", cache: false, url: 'GiveLikes.ashx', dataType: 'text', data: { UID: uid }, beforeSend: function () { }, success: function (data) { var a = ($('#Likes_No').text())*1+1*1; $('#Likes_No').html(a); $("#Likes_IMG").attr('disabled', true); }, error: function (XmlHttpRequest, textStatus, errorThrown) { console.log("点赞错误!"); } });
后端GiveLikes.ashx代码中的代码
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Web; using System.Windows.Forms; namespace Arena { /// <summary> /// GiveLikes 的摘要说明 /// </summary> public class GiveLikes : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string LogUID = Environment.UserName; string UID = context.Request["UID"]; if (UID == "") { UID= Environment.UserName; } using (SqlConnection conn = new SqlConnection("Server=.;Database=Arena;User ID=sa;Password=yymm7010212")) { string Sql= string.Empty; conn.Open(); SqlCommand comm = new SqlCommand(); comm.CommandType = CommandType.Text; comm.CommandText = "SELECT Likes from Arena.dbo.PersonalInfo WHERE UID='"+ UID + "'"; comm.Connection = conn; using (SqlDataReader DataReader = comm.ExecuteReader()) { DataReader.Read(); if ((DataReader["Likes"] is System.DBNull))//如果目前还没有任何人点赞 { Sql = "Update Arena.dbo.PersonalInfo set Likes='" + LogUID + ",' WHERE UID='" + UID + "'"; } else { Sql = "Update Arena.dbo.PersonalInfo set Likes=Likes+'" + LogUID + ",' WHERE UID='" + UID + "'"; } } comm.CommandText = Sql; comm.ExecuteNonQuery(); comm.Dispose(); } } public bool IsReusable { get { return false; } } } }
并用sql查询语句查询到数据库中点赞的人数
comm.CommandText = "SELECT LEN(Likes)-len(replace(Likes,',','')) AS num FROM Arena.dbo.PersonalInfo WHERE UID='" + UID + "'";
数据库中
相关文章推荐
- 用asp.net实现的把本文推荐给好友功能
- asp.net 实现下拉框只读功能
- 用asp.net实现的把本文推荐给好友功能
- 一个实现查询功能的asp.net控件
- ASP.NET使用WebService实现天气预报功能
- [Unity3d]unity+asp.net实现动态搜索加载模型并且能够实现模型拖动缩放的功能
- ASP.NET中实现MSN通知消息功能
- asp.net jQuery Ajax用户登录功能的实现
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- ASP.NET实现word文档在线预览功能代码
- Asp.net MVC模式实现登录功能
- Asp.net使用SignalR实现聊天室的功能
- ASP.NET实验五:实现输入内容提示的功能(仿google_百度输入框提示)
- ASP.NET实现MSN通知消息功能
- 在 Asp.NET MVC 中使用 SignalR 实现推送功能
- C# ASP.NET 最常用的通用权限的3个方法例子展示(每个功能一行代码实现)
- Jquery+ASP.NET 实现开心网上传头像剪裁功能
- ASP.NET实现微信功能(1)(创建菜单,验证,给菜单添加事件)
- Asp.net 实现验证码功能的Web控件