2014-02-19 27 views
2

使用jQuery數據庫連接我想顯示使用數據庫jquery.this某些領域是用於連接數據庫和顯示如何在asp.net

<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

使用試例外,throw.BTW什麼錯誤message.debug您的sql – KumarHarsh

+2

除了語法錯誤,請考慮使用準備語句,而不是直接在查詢中有變量。這將吸引SQL注入。 –

+0

在我看來,參數必須是CustomerID而不是employeeId。 –

回答

3

你的選擇查詢有不正確的語法:

試試這個:

SqlCommand cmd = new SqlCommand("select * from emp where id='" + employeeId + "'", con); 
-------------------------------------------------------^ 

相反的:

SqlCommand cmd = new SqlCommand("select * from emp wher id='" + employeeId + "'", con);