AI智能
改变未来

Asp.net实现点赞功能

在数据库中用一组数据来存放已经点赞过的人名,用半角符号分开,用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 + \"\'\";

数据库中

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Asp.net实现点赞功能