我想使用 ajax jquery 将 SQL Server 记录放入 asp.net,但我没有得到
本文关键字:net asp 记录 ajax jquery Server SQL | 更新日期: 2023-09-27 17:56:43
如果我选择年份2011
,则会出现aData[0]=2011
的值,但是没有调用带有alert
的java脚本函数。
我没有得到任何结果。
.HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
<script src="Scripts/jquery.min.js"></script>
<script src="Scripts/jquery.jqplot.min.js"></script>
<script src="Scripts/jqplot.pieRenderer.min.js"></script>
<script type="text/javascript">
$(function () {
$("#ddl1").change(function () {
var selectedText = $(this).find("option:selected").text();
var selectedValue = $(this).val();
var aData = [];
aData[0] = selectedText;
var jsonData = JSON.stringify({ aData: aData});
$.ajax({
type: 'POST',
url: 'Default.aspx/getCityPopulation',
data:jsonData,
async: true,
cache:false,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: OnSuccess,
error: OnErrorCall
});
function OnSuccess(response) {
var aData = response.d;
var arr = [];
$.map(aData, function (item, index) {
var i = [item.cityname, item.population];
arr.push(i);
});
var plot1 = jQuery.jqplot('chartdiv', [arr],
{
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show: true, location: 'e' }
}
)
}
function OnErrorCall_(response) {
alert("Whoops something went wrong!");
}
e.preventDefault();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="chartdiv"></div>
<select id="ddl1" runat="server" name="ddl1">
<option value="" selected="selected">--Select--</option>
<option value="0">2010</option>
<option value="1">2011</option>
<option value="2">2012</option>
<option value="3">2013</option>
<option value="4">2014</option>
</select>
</form>
</body>
</html>
在.aspx
页面的代码隐藏中,我有这样的代码:
public class cityPopulation
{
public string cityname { get; set; }
public int population { get; set; }
}
[WebMethod]
public List<cityPopulation> getCityPopulation(List<string> aData)
{
List<cityPopulation> p = new List<cityPopulation>();
using (SqlConnection cn = new SqlConnection("server=.....;user id=sa;password=...;database=Example"))
{
string myQuery = "SELECT cityname, population FROM tblcitypopulation WHERE year = @year";
SqlCommand cmd = new SqlCommand(myQuery, cn);
//cmd.CommandText = myQuery;
cmd.CommandType = CommandType.Text;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@year";
parameter.Value = aData[0];
//cmd.Parameters.AddWithValue("@year", aData[0]);
// cmd.Connection = cn;
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
cityPopulation cpData = new cityPopulation();
cpData.cityname = dr["cityname"].ToString();
cpData.population = Convert.ToInt32(dr["population"].ToString());
//cpData.year = Convert.ToInt32(dr["year"].ToString());
p.Add(cpData);
}
return p;
}
}
您正在创建SqlParameter
- 但永远不会将其添加到SqlCommand
....另外:将using()
结构用于SqlCommand
和SqlDataReader
- 不仅仅是SqlConnection
!
试试这个代码:
[WebMethod]
public List<cityPopulation> getCityPopulation(List<string> aData)
{
List<cityPopulation> p = new List<cityPopulation>();
string myQuery = "SELECT cityname, population FROM tblcitypopulation WHERE year = @year";
using (SqlConnection cn = new SqlConnection("server=.....;user id=sa;password=...;database=Example"))
using (SqlCommand cmd = new SqlCommand(myQuery, cn))
{
cmd.Parameters.Add("@year", SqlDbType.Int).Value = aData[0];
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
cityPopulation cpData = new cityPopulation();
cpData.cityname = dr["cityname"].ToString();
cpData.population = Convert.ToInt32(dr["population"].ToString());
p.Add(cpData);
}
}
return p;
}
}