JavaScript operations on the database JS operations on the Access database, similar to operations in other languages, summarizes the habits of code for reference only.
The file abc. mdf is now available on disk F, and the table name is Student. There are two fields in total, the Id numeric primary key, and the stuName text type. Now you can add, delete, modify, and query the table:
1. Query
<HTML>
<HEAD>
<TITLE> Data Query </TITLE>
<Script>
Var conn = new ActiveXObject ("ADODB. Connection ");
Conn. Open ("DBQ = f: // abc. mdb; DRIVER = {Microsoft Access Driver (*. mdb )};");
Var rs = new ActiveXObject ("ADODB. Recordset ");
Var SQL = "select * from Student ";
Rs. open (SQL, conn );
Var html = "";
While (! Rs. EOF)
{Www.2cto.com
Html = html + rs. Fields ("Id") + "" + rs. Fields ("stuName ");
Rs. moveNext ();
}
Document. write (html );
Rs. close ();
Rs = null;
Conn. close ();
Conn = null;
</Script>
</HEAD>
<BODY>
</BODY>
</HTML>
2. Add operations
<HTML>
<HEAD>
<TITLE> Add operation </TITLE>
<Script language = "javascript">
Function addUser (id, stuName)
{
// Use JavaScript to write the sample code for connecting the server to the database
Var conn = new ActiveXObject ("ADODB. Connection ");
Conn. Open ("DBQ = F: // abc. mdb; DRIVER = {Microsoft Access Driver (*. mdb )};");
Var SQL = "insert into Student (ID, stuName) values (" + id + ", '" + stuName + "')";
Try {
Conn.exe cute (SQL );
Alert ("added successfully ");
}
Catch (e ){
Document. write (e. description );
Alert ("failed to add ~~~ ");
}
Conn. close ();
}
</Script>
</HEAD>
<BODY>
<Table width = 100 border = 1>
<Tr bgcolor = '# f4f4f4'>
<Td> NO. </td>
<Td> name </td>
</Tr>
<Tr>
<Td> <input id = "stuId"/> </td>
<Td> <input id = "stuName"/> </td>
</Tr>
</Table>
<Input name = "1" type = "button" value = "add" onclick = "addUser (stuId. value, stuName. value)"/>
</BODY>
</HTML>
3. Delete
<HTML>
<HEAD>
<TITLE> delete operation </TITLE>
<Script language = "javascript">
Function delStu (id)
{
Var conn = new ActiveXObject ("ADODB. Connection ");
Conn. Open ("DBQ = F: // abc. mdb; DRIVER = {Microsoft Access Driver (*. mdb )};");
Var SQL = "delete from Student where Id = 2 ";
Conn.exe cute (SQL );
Conn. close ();
Conn = null;
Alert ("modified successfully ");
}
</Script>
</HEAD>
<BODY>
<Input name = "1" type = "button" value = "delete" onclick = "delStu (1)"/>
</BODY>
</HTML>
4. Modification Operation
<HTML>
<HEAD>
<TITLE> Modification Operation </TITLE>
</HEAD>
<Script>
Function updateUser (userId, userName)
{
Var conn = new ActiveXObject ("ADODB. Connection ");
Conn. Open ("DBQ = F: // abc. mdb; DRIVER = {Microsoft Access Driver (*. mdb )};");
Var rs = new ActiveXObject ("ADODB. Recordset ");
Var SQL = "update Student set stuName = '" + userName + "'where Id =" + userId + "";
Conn.exe cute (SQL );
Conn. close ();
Conn = null;
Alert ("modified successfully ");
}
</Script>
<BODY>
<Table width = 100 border = 1>
<Tr bgcolor = '# f4f4f4'>
<Td> NO. </td>
<Td> name </td>
</Tr>
<Tr>
<Td> <input id = "stuId"/> </td>
<Td> <input id = "stuName"/> </td>
</Tr>
</Table>
<Input name = "1" type = "button" value = "modify" onclick = "updateUser (stuId. value, stuName. value)"/>
</BODY>
</HTML>
In addition, JS can also operate SQL Server databases.
Database Name: MySchool, table name: Student, StudentId: int type, auto-incrementing column, studentName: Student name, varchar type. The database username is sa and the password is OK,
<HTML>
<HEAD>
<TITLE> SQL data query </TITLE>
<Script>
Var conn = new ActiveXObject ("ADODB. Connection ");
Conn. Open ("Driver = {SQL server}; Server =.; DataBase = MySchool; UID = sa; Password = OK;"); // Open the DataBase
Var rs = new ActiveXObject ("ADODB. Recordset ");
Var SQL = "select * from Student ";
Rs. open (SQL, conn );
Var html = "";
While (! Rs. EOF)
{
Html = html + rs. Fields ("StudentId") + "" + rs. Fields ("studentName") + "<br/> ";
Rs. moveNext ();
}
Document. write (html );
Rs. close ();
Rs = null;
Conn. close ();
Conn = null;
</Script>
</HEAD>
<BODY>
</BODY>
</HTML>