다음을 통해 공유


JScript Code Example to Return a Recordset

JScript Code (rs.js)

main();

function main()
{
  DP = "SQLOLEDB";
  DS = "MySQLServer";
  DB = "NORTHWIND";

  adOpenForwardOnly = 0;
  adLockReadOnly = 1;
  adCmdText = 1;
  try 
  {
    var objRs = new ActiveXObject("ADODB.Recordset");
  }
  catch (e)
  {
    alert("ADODB namespace not found.");
    exit(0);
  }

  strConn =  "Provider="         +DP+
            ";Initial Catalog="  +DB+
            ";Data Source="      +DS+
            ";Integrated Security=SSPI;"
  strComm = "SELECT ProductID,ProductName,UnitPrice "+
            "FROM Products " + 
            "WHERE CategoryID = 7"  // select Produce

  objRs.open(strComm, 
             strConn, 
             adOpenForwardOnly,
             adLockReadOnly,
             adCmdText);

  objRs.MoveFirst();
  while (objRs.EOF != true) 
  {
    alert(objRs("ProductID")+"\t"
         +objRs("ProductName")+"\t"
         +objRs("UnitPrice"));
    objRs.MoveNext();
  }

  objRs.Close
  objRs = null;
}


function alert(str)
{
  WScript.Echo(str);
}

Try It!

  1. Save the code above into a text file. Save the file as rs.js.
  2. Open a command prompt and cd to the directory where you have saved the JScript file (rs.js).
  3. Type CScript rs.js from the command prompt.