现在我正在为下一学期学习JavaScript。
问题是,我无法在我的数据库中插入数据(MS Access)。语法中有什么问题或缺失?
注意:我还尝试了+
而不是&
。这是我的代码:
function register(){
var fname = document.getElementById('fname').value;
var lname = document.getElementById('lname').value;
var usernn = document.getElementById('username').value;
var passw = document.getElementById('password').value;
}
var con = new ActiveXObject("ADODB.Connection");
var constring = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source = Z:\JavaScript Practice Code\jscript.accdb";
var rset = new ActiveXObject("ADODB.Recordset");
con.Open(constring);
function register(){
var sql = "Insert into register(fname,lname,usern,passw) Values('"&fname&"','"&lname&"','"&usernn&"','"&passw&"')";
rset.Open(sql, con);
con.Close();
rset.Close();
}
答案 0 :(得分:3)
ADODB.Recordset
个对象用于通过SELECT
语句检索行。要执行INSERT
语句,您应该使用ADODB.Command
这样的对象:
var adVarWChar = 202;
var adParamInput = 1;
// test data
var fname = "Gord";
var lname = "Thompson";
var con = new ActiveXObject("ADODB.Connection");
con.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Public\\Database1.accdb");
var cmd = new ActiveXObject("ADODB.Command");
cmd.ActiveConnection = con;
cmd.CommandText = "INSERT INTO Employee (FirstName, LastName) VALUES (?, ?)";
cmd.Parameters.Append(cmd.CreateParameter("?", adVarWChar, adParamInput, 50, fname));
cmd.Parameters.Append(cmd.CreateParameter("?", adVarWChar, adParamInput, 50, lname));
cmd.Execute();
con.Close();