使用Javascript检查数据库中是否存在记录

时间:2013-02-27 14:47:59

标签: javascript sql select recordset

我正在尝试使用javascript检查记录是否存在(我知道这不是最安全的方法)但所有这些都是供内部使用,安全性不是问题。

所以我打开了一个记录集,

rs.Open("SELECT * FROM clie Where N_CLIENT =" + textbox1+ " AND C_POST_CLIENT = '" + textbox2+ "'",connection)

textbox1textbox2是我在clie表中查看的值,但首先我需要检查记录是否存在。我尝试将rs.Open分配给变量,然后将其与某些内容进行比较但不起作用

我尝试使用RecordCount,但我一直保持-1。我读过它不是为了那个,并且它不应该用于查找记录,所以必须有另一种方法来做到这一点。

更新 _

以下是我正在处理的整个功能

function RecordExists(textfield1, textfield2)
{
var connection = new ActiveXObject("ADODB.Connection") ;
    var connectionstring = "UID=admin;PWD=password";
    connection.Open(connectionstring);
    var rs = new ActiveXObject("ADODB.Recordset");
    var textbox1= new String();
    var textbox2=new String();
    textbox1= document.getElementById(textfield1).value;
    textbox2= document.getElementById(textfield2).value;

    var isEmpty=new String();

    rs.Open("SELECT count(*) as pers FROM clie HAVING N_CLIENT =" + textbox1+ " AND C_POST_CLIE = '" + textbox2+ "'",connection);

    alert(rs.recordcount);
    //alert(rs.fields(1));
    //isEmpty = rs.Open("pers");
    alert("Empty"+isEmpty);
    if(pers=0)
        alert("Record does not exist! pers="+pers);
    else if(pers=1)
        alert("Record exists! pers="+pers);
    else
        alert("not working");
    rs.close;
    connection.close;
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

rs.Open("SELECT count(1) as pers FROM clie Where N_CLIENT =" + textbox1+ " AND C_POST_CLIENT = '" + textbox2+ "'",connection)

以这种方式检索pers字段:

perCounts = rs.('pers')

perCounts = rs.("pers")

然后,如果perCounts = 0,则用户不存在....如果1,那么用户存在于您的数据库中。

__ _ ____ 修改 _ < / EM> __ _ __ _ __ _ __ _ ___

function RecordExists(textfield1, textfield2)
{
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring = "UID=admin;PWD=password";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
var textbox1= new String();
var textbox2=new String();
textbox1= document.getElementById(textfield1).value;
textbox2= document.getElementById(textfield2).value;

var isEmpty=new String();

rs.Open("SELECT count(*) as pers FROM clie HAVING N_CLIENT =" + textbox1+ " AND C_POST_CLIE = '" + textbox2+ "'",connection);

alert(rs.recordcount);

rs.MoveFirst();

perCounts = rs.Fields(0).Value;


if(perCounts=0)
    alert("Record does not exist! pers="+pers);
else if(perCounts=1)
    alert("Record exists! pers="+pers);
else
    alert("not working");
rs.close;
connection.close;
}
}

Saludos。

答案 1 :(得分:0)

您应该使用count方法

  

rs.Open(“SELECT count(*)FROM clie Where N_CLIENT =”+ textbox1 +“AND   C_POST_CLIENT ='“+ textbox2 +”'“,连接)

这将返回结果数

  

0 = 0客户端1 = 1客户端2 = 2个客户端。 。