2012-02-29 84 views
0

我想問如何使用javascript ADODB.Recordset對象執行插入語句? 謝謝先進的Tal。在Javascript中使用插入語句使用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,則必須將「'」附加到變量中。如果來自alert(sql)的SQL語句與您的Table Schema格式正確,應該沒問題。希望這個幫助。

/* 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;