2012-02-29 3 views
0

자바 스크립트 ADODB.Recordset 개체를 사용하여 insert 문을 실행하는 방법을 묻고 싶습니다. 고급 기능 덕분입니다. 자바에서 Insert 문 만들기 ADODB.Recordset을 사용하여

내가 실행하는 데 노력하고있어 코드 :

/* Getting access to the database */ 
var connection = new ActiveXObject("ADODB.Connection"); 
var connectionstring = "Data Source=srvp7rnd-herm;Initial Catalog=hermes;User ID=hermes;Password=hermes;Provider=SQLOLEDB"; 
connection.Open(connectionstring); 

/* JavaScript obect to access a SQL query's results */ 
var rs = new ActiveXObject("ADODB.Recordset"); 

/* Getting the current MAX(id) from the database */ 
rs.Open("SELECT MAX(id) FROM Screen_Template", connection); 
rs.MoveFirst; 
var maxID = rs.Fields.Item(0); 
maxID = maxID + 1; 

/* TODO: Get the last UID */ 
var sql = "INSERT INTO Screen_Template(template_name, OpCo, env, template_xml, language, id, title, role, UID) VALUES (" + templateName + "," + opco + "," + env + "," + "<hello>hello</hello>" + ",eng," + maxID + ",Hermes SMS message composer," + "manag, 10)"; 
alert(sql); 
rs.Open(sql, connection); 

/* Closing the connections */ 
rs.close; 
connection.close; 

하지만 난 그 코드를 실행하려고하고있을 때, 그것은 나에게 오류 메시지를 제공합니다.

답변

1

이 코드를 사용해보십시오. Screen_Template 열 유형이 Varchar 인 경우 변수에 " '"을 추가해야합니다. 경고 (SQL)의 SQL 문이 테이블 스키마의 올바른 형식이면 괜찮습니다. 희망이 도움이됩니다.

/* Getting access to the database */ 
    var connection = new ActiveXObject("ADODB.Connection"); 
    var connectionstring = "Data Source=srvp7rnd-herm;Initial Catalog=hermes;User ID=hermes;Password=hermes;Provider=SQLOLEDB"; 
    connection.Open(connectionstring); 

    /* JavaScript obect to access a SQL query's results */ 
    var rs = new ActiveXObject("ADODB.Recordset"); 

    /* Getting the current MAX(id) from the database */ 
    rs.Open("SELECT MAX(id) FROM Screen_Template", connection); 
    rs.MoveFirst; 
    var maxID = rs.Fields.Item(0); 
    maxID = maxID + 1; 
    rs.close; 

    /* TODO: Get the last UID */ 
    var sql = "INSERT INTO Screen_Template(template_name, OpCo, env, template_xml, language, id, title, role, UID) VALUES ('" + templateName + "','" + opco + "','" + env + "'," +"'<hello>hello</hello>'" + ",'eng'," + maxID + ",'Hermes SMS message composer'," + "'manag', 10)"; 
    alert(sql); 
    rs.Open(sql, connection); 

    /* Closing the connections */ 
    //rs.close; 
    connection.close;