2012-02-29 19 views
0

javascript ADODB.Recordsetオブジェクトを使用してinsertステートメントを実行する方法を尋ねますか? 先進的なTalに感謝します。Javascriptで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;