ajax 视频点赞功能

视频点赞功能与数据库 asp.net部分

第一种做法:
将视频添加在html页中后,再添加点赞和踩的功能按钮

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<video src="Properties/q.mp4" autoplay="autoplay">

</video>
<form action="Handler2.ashx" method="post">
<input type="submit" value="赞" name="zan" />
<span id="zan">{zancunt}</span>
<input type="submit" value="踩" name="cai" />
<span id="cai">{caicunt}</span>

</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;

namespace WebApplication1
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string zan = context.Request["zan"];
string cai = context.Request["cai"];
string html = File.ReadAllText(context.Server.MapPath("/HtmlPage1.html"));//读取文件通过物理方式。
DataTable dt = SqlHelper.ExecuteDataTable("select * from videocunt");
int ZanCunt = Convert.ToInt32(dt.Rows[0][0]);
int CaiCunt = Convert.ToInt32(dt.Rows[0][1]);


if (string.IsNullOrEmpty(zan) && string.IsNullOrEmpty(cai))
{


html = html.Replace("{zancunt}", ZanCunt.ToString());
html = html.Replace("{caicunt}", CaiCunt.ToString());

 

 

}

else if (!string.IsNullOrEmpty(zan))
{


html = html.Replace("{zancunt}", (ZanCunt + 1).ToString());
SqlHelper.ExecuteNonQuery("update videocunt set ZanCunt=ZanCunt+1 ");
html = html.Replace("{caicunt}", CaiCunt.ToString());

 


}
else
{

html = html.Replace("{caicunt}", (CaiCunt + 1).ToString());
SqlHelper.ExecuteNonQuery("update videocunt set CaiCunt=CaiCunt+1 ");
html = html.Replace("{zancunt}", ZanCunt.ToString());


}

context.Response.Write(html);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}


<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="connStr" connectionString="server=.;database=vido;uid=sa;pwd=123" />
</connectionStrings>

</configuration>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
public class SqlHelper
{
private static readonly string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;//读取数据库字段
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters )//取多个数值
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();//取一个一行的第一个数值
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)//从数据库的拿出一个表
{
DataSet set = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
if (parameters!=null)
{
cmd.Parameters.AddRange(parameters);
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(set);
return set.Tables[0];
}
}
}
}

posted @ 2017-05-16 09:42  西瓜冰镇老板  阅读(451)  评论(0编辑  收藏  举报