4

私は現在、新しい開発に取り組んでおり、プロジェクトに必要な滑らかな感触が本当に必要なので、SQL Server 2008 からデータを取得するために Ajax または jQuery を使用することを考えました。JSON とその機能は初めてです。

html は次のとおりです。

Enter Employee ID
<br />
<asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox>&nbsp;
<br />
<input type="button" id="BtnSearch" runat="server" value="Search" />
<div id="emp" style="display: none; margin-top: 40px">
    ID:<span id="txtId"></span><br />
    Title:<span id="txtTitle"></span><br />
    Name:<span id="txtName"></span><br />
    Department:<span id="txtDepartment"></span><br />
</div>

Ajax は次のようになります。

<script type="text/javascript">
$(document).ready(function () {

    $("#MainContent_BtnSearch").click(function () {

        $("#emp").hide("slow");

        var empId = $("#MainContent_TxtEmpId").val();
        $.ajax({
            type: "GET",
            dataType: "json",
            contentType: "application/json",
            url: "",
            data: "{'employeeId': '" + empId.toString() + "'}",
            success: function (data) {
                $("#txtId").html(data.d.ID);
                $("#txtName").html(data.d.FullName);
                $("#txtTitle").html(data.d.Title);
                $("#txtDepartment").html(data.d.Department);
                /// show employee details
                $("#emp").show("slow");
            },
            error: function () {
                alert("Error calling the web service.");
            }
         });
     });
});

SQL Server 2008 のデータベースからデータを取得し、そのレコードを更新してデータベースの変更を保存できるようにコードを改善する方法はありますか?

4

2 に答える 2

0

` $('#sqlLoadPerson').sqlRun();私はmojoportalで働いていて、関数sqlRun();を呼び出すだけでjqueryをsqlに接続していました。たとえば、divを取ることができます

そして、この div id を使用し、この call sql run を使用する必要があります

これによりプロシージャが実行され、フロントエンドで結果が得られます

于 2013-04-21T05:21:04.633 に答える
0

your URL in the ajax call can be something like: Page.aspx/Method so: default.aspx/GetEmployeeById and make it Post

public class Employee
{
    // properties: ID, FullName, Title, Department
}


[WebMethod]
public static Employee GetEmployeeById(string employeeId)
{
  Employee emp = new Employee();
  string connect = "your connection string";
  string query = "SELECT * FROM Employees WHERE Id = @employeeId";

  using (SqlConnection conn = new SqlConnection(connect))
    {
      using (SqlCommand cmd = new SqlCommand(query, conn))
      {
        cmd.Parameters.AddWithValue("Id", employeeId);
        conn.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
          while (rdr.Read())
          {
            // fill your employee object
          }
        }
      }
    }
  return emp;
}

This is just an example, so your connectionstring and sql shouldn't really be in this method. Also make sure your employeeid parameter has a value.

于 2012-11-13T09:01:21.163 に答える