Ajax调用存储过程简单实例
ajax基于javascript ,就是通过网页脚本向服务器发送异步的请求,改变同步请求的不便
同步请求 一个请求必须等待到服务器的响应 然后整个页面刷新
异步请求 则不需要等待服务器的响应 因此页面可以实现局部刷新
这样的好处是提供了更好的用户体验,仅供初学者学习。
页面代码如下:
代码
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function addNumber()
{
createXMLHttpRequest();
var url= "Handler.ashx";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
//alert(xmlHttp.status) ;
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form2" runat="server">
<div style="text-align: center">
<div id="result"></div>
</div>
<input id="btnGetData" type="button" value="Ajax 获取存储过程数据" onclick="addNumber();"/>
</form>
</body>
</html>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function addNumber()
{
createXMLHttpRequest();
var url= "Handler.ashx";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=showResult;
xmlHttp.send(null);
}
function showResult()
{
//alert(xmlHttp.status) ;
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form2" runat="server">
<div style="text-align: center">
<div id="result"></div>
</div>
<input id="btnGetData" type="button" value="Ajax 获取存储过程数据" onclick="addNumber();"/>
</form>
</body>
</html>
非常简单的存储过程实例 如下:
存储过程代码
create procedure GetDataSam(
@number int
)
as
begin
declare @str nvarchar(200);
set @str='select top '+cast(@number as nvarchar)+ 'title from zhq_in_content order by createdate desc';
exec(@str);
end
@number int
)
as
begin
declare @str nvarchar(200);
set @str='select top '+cast(@number as nvarchar)+ 'title from zhq_in_content order by createdate desc';
exec(@str);
end
一般处理文件Handler.ashx;代码如下
一般处理文件代码
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
StringBuilder sb = new StringBuilder();
int num = 10;
DataSet ds = GetDataSetFromDB(num);
if(ds!=null &&ds.Tables[0].Rows.Count>0){
for (int i = 0; i < ds.Tables[0].Rows.Count ;i++)
{
if(sb.ToString()==""){
sb.Append(ds.Tables[0].Rows[i]["title"].ToString());
}
else
{
sb.Append("<br/>").Append(ds.Tables[0].Rows[i]["title"].ToString());
}
}
}
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
public DataSet GetDataSetFromDB(int num) {
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["foretech"].ToString());
try
{
conn.Open();
//string sql = "select top 10 title from zhq_in_content order by createdate desc;";
// SqlCommand comm = new SqlCommand("select top 10 title from zhq_in_content order by createdate desc;",conn);
SqlDataAdapter da = new SqlDataAdapter("GetDataSam", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter("@number",num);
para.Direction = ParameterDirection.Input;
da.SelectCommand.Parameters.Add(para);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
return ds;
}
else
{
return null;
}
}
finally
{
conn.Close();
}
}
}
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
StringBuilder sb = new StringBuilder();
int num = 10;
DataSet ds = GetDataSetFromDB(num);
if(ds!=null &&ds.Tables[0].Rows.Count>0){
for (int i = 0; i < ds.Tables[0].Rows.Count ;i++)
{
if(sb.ToString()==""){
sb.Append(ds.Tables[0].Rows[i]["title"].ToString());
}
else
{
sb.Append("<br/>").Append(ds.Tables[0].Rows[i]["title"].ToString());
}
}
}
context.Response.Write(sb.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
public DataSet GetDataSetFromDB(int num) {
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["foretech"].ToString());
try
{
conn.Open();
//string sql = "select top 10 title from zhq_in_content order by createdate desc;";
// SqlCommand comm = new SqlCommand("select top 10 title from zhq_in_content order by createdate desc;",conn);
SqlDataAdapter da = new SqlDataAdapter("GetDataSam", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter("@number",num);
para.Direction = ParameterDirection.Input;
da.SelectCommand.Parameters.Add(para);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
return ds;
}
else
{
return null;
}
}
finally
{
conn.Close();
}
}
}
仅供初学者学习,说不定去其他公司笔试的时候有用,用SQLHelper怕初学者看不懂,这个简单易懂。