How does js add, delete, modify, and query databases?

Source: Internet
Author: User

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
Copy codeThe Code is as follows:
<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)
{
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
Copy codeThe Code is as follows:
<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
Copy codeThe Code is as follows:
<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
Copy codeThe Code is as follows:
<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,
Copy codeThe Code is as follows:
<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.