我想使用webserver / jquery从sql server检索数据,我读了很多文章,但没有得到除下面的要求,但我认为它支持FrameWork .Net 3.5而不是2.0。我有同样的要求在ASP.Net 2.0中使用它。
//On WebServer Page..
WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class test : System.Web.Services.WebService
{
[WebMethod]
public string GetCustomer(string memberID)
{
string response = "<p>No customer selected</p>";
string connect = "Server=myserver;Initial Catalog=mydatabase;uid=myuser;pwd=mypassword";
string query = "SELECT name, father, mother from samaj where name=@memberID";
if (memberID != null)
{
StringBuilder sb = new StringBuilder();
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("memberID", memberID);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
sb.Append("<p>");
sb.Append("<strong>" + rdr["name"].ToString() + "</strong><br />");
sb.Append(rdr["father"].ToString() + "<br />");
sb.Append(rdr["mother"].ToString() + "<br />");
response = sb.ToString();
}
}
}
}
}
return response;
}
}
}
//.aspx page...
<script type="text/javascript" src="jquery-1.3.2.min.js" ></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#Customers").change(function()
{
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "test.asmx/GetCustomer",
data: "{ memberID: '" + $('#Customers').val() + "'}",
dataType: "json",
success: function(data)
{
$("#CustomerDetails").html(data.d);
}
});
});
});
</script>
<form id="form1" runat="server">
<div id="SelectCustomers">
<asp:DropDownList ID="Customers" runat="server">
</asp:DropDownList>
</div>
<div id="CustomerDetails">
</div>
</form>
The DropdownList Binding In Default.aspx.cs page...
protected void Page_Load(object sender, EventArgs e)
{
string connect = "Server=myserver;Initial Catalog=mydatabse;uid=myuser;pwd=mypwd
string query = "SELECT name FROM samaj";
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
Customers.DataSource = cmd.ExecuteReader();
Customers.DataValueField = "name";
Customers.DataBind();
}
}
.asmx页面测试确定它很好地检索数据但在客户端它没有返回任何数据。如何在ASP.Net 2.0中实现它?。