我想问一下如何使用javascript ADODB.Recordset对象执行insert语句? 谢谢先进的Tal。
这是我正在尝试运行的代码:
/* 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;
但是当我试图运行该代码时,它会给我一个错误信息。
答案 0 :(得分:1)
请尝试使用此代码。如果Screen_Template列类型为Varchar,则必须在变量后附加“'”。如果来自alert(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;