我想使用jquery从数据库中显示一些字段。这是连接数据库和显示的代码
<script type="text/javascript">
$(document).ready(function () {
$('#btnsearch').click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{ CustomerID: '" + $('#txtid').val() + "'}",
url: "Customer.aspx/FetchCustomer",
dataType: "json",
success: function (data) {
var Employee = data.d;
$('#CustomerDetails').append
('<p><strong>' + Employee.Id + "</strong><br />" +
Employee.fname + "<br />" +
Employee.lname + "<br />" +
"</p>")
}
});
});
});
</script>
.cs代码
[WebMethod]
public Employee FetchCustomer(string employeeId)
{
Employee c = new Employee();
SqlConnection con = new SqlConnection("Data Source=BAIJU-PC;Initial Catalog=Baiju;Integrated Security=True");
// SqlDataAdapter da = new SqlDataAdapter("select * from emp wher id='" + employeeId + "'", con);
con.Open();
SqlCommand cmd = new SqlCommand("select * from emp wher id='" + employeeId + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
c.id = dr["id"].ToString();
c.fname = dr["fname"].ToString();
c.lname = dr["LNAME"].ToString();
}
return c;
}
public class Employee
{
public string id { get; set; }
public string fname { get; set; }
public string lname { get; set; }
}
错误是当我运行应用程序.cs代码触发它执行此代码
SqlDataReader dr = cmd.ExecuteReader();
之后它没有执行。 如何解决这个问题
答案 0 :(得分:3)
您的选择查询语法不正确:
试试这个:
SqlCommand cmd = new SqlCommand("select * from emp where id='" + employeeId + "'", con);
-------------------------------------------------------^
而不是:
SqlCommand cmd = new SqlCommand("select * from emp wher id='" + employeeId + "'", con);