如何使用 javascript adodb 连接在网格视图中显示 sql 服务器数据

How to display sql server data in grid view using javascript adodb connection

请检查下面我试过的脚本代码,

      <script type="text/javascript">
        var objConnection = new ActiveXObject("adodb.connection");
        var strConn = "driver={sql server};server=DESKTOP-IA5UQ36;database=arif;uid=;password=";

        objConnection.Open(strConn);
        var rs = new ActiveXObject("ADODB.Recordset");
        var strQuery = "SELECT Id , Name,F_Name,Cnic, Dues,Fees FROM  customer ORDER BY  Id";
        rs.Open(strQuery, objConnection);
        rs.MoveFirst();
        </script>
<script type="text/javascript">
    function rsEdit(varId) {
        objConnection.Open(strConn);
        var rsE = new ActiveXObject("ADODB.Recordset");
        var strQuery = "SELECT Id , Name,F_Name,Cnic, Dues,Fees FROM  customer WHERE  Id = " + varId;
        rsE.Open(strQuery, objConnection);
        rsE.MoveFirst();

      while (rsE.EOF != true)   
      {  
        document.write("<table>")
        document.write("<tr>") ;
        document.write("<td>ID: " + rsE("Id") + " <input id='regId' type='hidden' value='" + rsE("Id") + "'></td>");
        document.write("<td>Name: <input id='regName' type='text' value='" + rsE("Name") + "'></td>");
        document.write("<td>F_Name: <input id='regF_Name' type='text' value='" + rsE("F_Name") + "'></td>");
        document.write("<td>Cnic: <input id='regCnic' type='text' value='" + rsE("Cnic") + "'></td>");
        document.write("<td>Dues: <input id='regDues' type='text' value='" + rsE("Dues") + "'></td>");
        document.write("<td>Fees: <input id='regFees' type='text' value='" + rsE("Fees") + "'></td>");
        document.write("<td> <input onClick='rsSave()' type='button' ></td>");
        document.write("</tr>");
        document.write("</table>");

        rsE.MoveNext();  
      }   
    }

    function rsSave() {
        objConnection.Open(strConn);
        var rsE = new ActiveXObject("ADODB.Recordset");

        var regIdVal = document.getElementById("regId").value;
        var regNameVal = document.getElementById("regName").value;
        var regF_NameVal = document.getElementById("regF_Name").value;
        var regCnicVal = document.getElementById("regCnic").value;
        var regDuesVal = document.getElementById("regDues").value;
        var regFeesVal = document.getElementById("regFees").value;

        var strQuery = "UPDATE customer SET Name = '" + regNameVal + "',F_NameVal = '" + regF_NameVal + "',Cnic = '" + regCnicVal + "',Dues = '" + regDuesVal + "',Fees = '" + regFeesVal + "' WHERE Id = " + regIdVal;
        rs.Open(strQuery, objConnection);
    }

    function rsDelete(varId) {
        objConnection.Open(strConn);
        var rs = new ActiveXObject("ADODB.Recordset");
        var strQuery = "DELETE FROM customer WHERE Id = " + varId;
        rs.Open(strQuery, objConnection);
    }
</script>

</script>之后:

<table>
<tr>
    <th>ID<th>
    <th>Name<th>
    <th>F_Name<th>
    <th>Cnic<th>
    <th>Dues<th>
    <th>Fees<th>
</tr>

<script>

  while (rs.EOF != true)   
  {  
    document.write("<tr>") ;
    document.write("<td>" + rs("Id") + "</td>");
    document.write("<td>" + rs("Name") + "</td>");
    document.write("<td>" + rs("F_Name") + "</td>");
    document.write("<td>" + rs("Cnic") + "</td>");
    document.write("<td>" + rs("Dues") + "</td>");
    document.write("<td>" + rs("Fees") + "</td>");
    document.write("<td><button onClick='rsDelete(" + rs("id") + ")'>Del</button></td>"); 
    document.write("<td><button onClick='rsEdit(" + rs("id") + ")'>Edit</button></td>"); 
    document.write("</tr>");

    rs.MoveNext();  
  }  

</script>
</table>

但是这种用法是很不安全的,因为

  1. 连接字符串已打开
  2. 必须打开数据库服务器才能允许远程连接。