Jquery Ajax案例

$(function () {
        $.ajax({
            type: "POST",//提交方法
            url: "Hotel.ashx",//后台处理文件地址
            datatype: "json",//返回数据类型
            async: false,
            data: {//提交数据
                "method": "NearHotel", "cityNum": cityNum
            },
            success: function (data) {
                var json = eval('(' + data + ')');
                var htmlStr = "";
                for (var j = 0; j < json.length; j++) {
                    htmlStr += parseInt(json[j].Price) + '元';
                }
                $("#Hspot" + i + " tr").html(htmlStr);
            },
            error: function () {
                //alert("ajax有错!");
            }
        });
    });

一般处理文件.ashx代码:

public class createPlan : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string returnStr = chooseStr(context);
        context.Response.Write(returnStr);
    }
    public static string chooseStr(HttpContext context)
    {
        string str = context.Request.Form["method"];
        switch (str)
        {
            case "createPlan":
                return CreatePlan.createPlan(context);
            case "popupInfo":
                return CreatePlan.popupInfo(context);
            case "popupCityInfo":
                return CreatePlan.popupCityInfo(context);
            case "NearInfo":
                return CreatePlan.NearInfo(context);
            case "UpdateHeat":
                return CreatePlan.UpdateHeat(context);
        }
        return ("服务没有找到方法" + str);
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

类CreatePlan.cs代码:

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

/// <summary>
/// CreatePlan 的摘要说明
/// </summary>
public class CreatePlan
{
    public CreatePlan()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /*更新热点热度*/
    public static string UpdateHeat(HttpContext context)
    {
        string cityname = context.Request["name"];
        string sql = "UPDATE TourNav SET Heat = Heat + 1 WHERE Name = '" + cityname + "'";
        int i = SqlDb.ExecuteUpdateSql(sql);
        return "";
    }

    /*将表转化为json串*/
    public static string GetTableToJson(DataTable dt)
    {
        string str = "[";
        int rows = 0;
        int rowscount = dt.Rows.Count;
        while (rows < rowscount)
        {
            str += "{";
            int cols = 0;
            int colscount = dt.Columns.Count;
            while (cols < colscount)
            {
                string str3 = str;
                str = str3 + "\"" + dt.Columns[cols].ColumnName + "\":\"" + ((dt.Rows[rows][cols] != DBNull.Value) ? dt.Rows[rows][cols].ToString().Trim().Replace(@"\r", "") : "") + "\",";
                cols++;
            }
            str = str.Substring(0, str.Length - 1) + "},";
            rows++;
        }
        str = str.Substring(0, str.Length - 1) + "]";
        if (dt.Rows.Count > 0)
        {
            return str;
        }
        return string.Empty;
    }
}

SqlDB.cs代码:

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

/// <summary>
///SqlDb 的摘要说明
/// </summary>
public class SqlDb
{
    private static string CONNECTION_STRING = @"Data Source=你的ip地址; Initial Catalog=DBname;Integrated Security=false;User ID=sa;Password=sa;";
    public SqlDb()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static DataSet ExecuteSelectSql(string strSql)
    {
        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
        }
        finally
        {
            conn.Close();
        }
    }
    public static int ExecuteUpdateSql(string strSql)
    {

        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = strSql;
        int ret = -1;
        try
        {
            conn.Open();
            ret = comm.ExecuteNonQuery();

            return ret;
        }
        catch (Exception ex)
        {
            throw new Exception("执行SQL出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
        }
        finally
        {
            conn.Close();
        }

        //throw new NotImplementedException();
    }
    public static int ExecuteInsertSql(string strSql)
    {
        int ret = -1;
        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlCommand comm = new SqlCommand();
        comm.Connection = conn;
        comm.CommandType = CommandType.Text;
        comm.CommandText = strSql;
        try
        {
            conn.Open();
            ret = comm.ExecuteNonQuery();
            return ret;
        }
        catch (Exception ex)
        {
            throw new Exception("执行sql出现错误:\r\n" + strSql + "\r\n" + ex.ToString());
        }
        finally
        {
            conn.Close();
        }
    }
}

SqlDbHelper.cs

/// <summary>
/// 此处有两个数据库连接
/// CONNECTION_STRING连接YN库,对应ExecuteSelectSql、ExecuteNonQuerySql两个方法
/// CONNECTION_STRING1连接CYZN库,对应ExecuteSelectSql1、ExecuteNonQuerySql1两个方法
/// 使用时注意!!!!
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// SqlDbHelper 的摘要说明
/// </summary>
public class SqlDbHelper
{
    public SqlDbHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private static string CONNECTION_STRING = @"Data Source=172.17.130.225; Initial Catalog=YN;Integrated Security=false;User ID=sa;Password=sa;";
    /// <summary>
    /// 执行返回数据集的查询sql语句
    /// </summary>
    /// <param name = strsql >sql语句</param>
    /// <param name = pars>参数数组</param>
    /// <returns>查询数据集</returns>
    public static DataSet ExecuteSelectSql(string strSql, params SqlParameter[] pars) 
    {
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlCommand comm = new SqlCommand(strSql, conn);
        try
        {
            try
            {
                comm.Parameters.AddRange(pars);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(comm);
                da.Fill(ds);
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return ds;
        }
        finally
        {
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
    /// <summary>
    /// 执行仅要求返回影响行数的sql语句
    /// </summary>
    /// <param name = strsql >sql语句</param>
    /// <param name = pars>参数数组</param>
    /// <returns>影响行数</returns>
    public static int ExecuteNonQuerySql(string strSql, params SqlParameter[] pars)
    {
        int ret = -1;
        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlCommand comm = new SqlCommand(strSql, conn);
        try
        {
            try
            {
                comm.Parameters.AddRange(pars);
                conn.Open();
                ret = comm.ExecuteNonQuery();
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return ret;
        }
        finally
        {
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }  
    private static string CONNECTION_STRING = @"Data Source=知己ip; Initial Catalog=DBname;Integrated Security=false;User ID=sa;Password=sa;";
    /// <summary>
    /// 执行返回数据集的查询sql语句
    /// </summary>
    /// <param name = strsql >sql语句</param>
    /// <param name = pars>参数数组</param>
    /// <returns>查询数据集</returns>
    public static DataSet ExecuteSelectSql1(string strSql, params SqlParameter[] pars)
    {
        DataSet ds = new DataSet();
        SqlConnection conn = new SqlConnection(CONNECTION_STRING1);
        SqlCommand comm = new SqlCommand(strSql, conn);
        try
        {
            try
            {
                comm.Parameters.AddRange(pars);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(comm);
                da.Fill(ds);
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return ds;
        }
        finally
        {
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
    /// <summary>
    /// 执行仅要求返回影响行数的sql语句
    /// </summary>
    /// <param name = strsql >sql语句</param>
    /// <param name = pars>参数数组</param>
    /// <returns>影响行数</returns>
    public static int ExecuteNonQuerySql1(string strSql, params SqlParameter[] pars)
    {
        int ret = -1;
        SqlConnection conn = new SqlConnection(CONNECTION_STRING);
        SqlCommand comm = new SqlCommand(strSql, conn);
        try
        {
            try
            {
                comm.Parameters.AddRange(pars);
                conn.Open();
                ret = comm.ExecuteNonQuery();
            }
            catch (SqlException exception)
            {
                throw new Exception(exception.Message);
            }
            return ret;
        }
        finally
        {
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值