Giới thiệu
Trong bài viết này, tôi sẽ giải thích cách chèn các bản ghi vào cơ sở dữ liệu bằng cách sử dụng TextBox trong JavaScript.
Lưu ý: Chương trình này sẽ chỉ hoạt động với Internet Explorer.This program will work only with Internet Explorer.
Đầu tiên tôi đã tạo một cơ sở dữ liệu empdetail. & Nbsp; sau đó tôi đã tạo một bảng trong cơ sở dữ liệu này. & Nbsp;
Mã truy vấn
- CREATE TABLE [dbo].[Emp_Info][
- [Emp_Id] [int] NULL, int] NULL,
- [Name] [varchar][50] NULL,
- [Salary] [int] NULL, int] NULL,
- [City] [varchar][50] NULL
- ] & nbsp; on & nbsp; [Chính] & nbsp;
Bây giờ chèn một số dữ liệu vào bảng EMP_INFO.Sau đó sử dụng quy trình sau.
Hoàn thành chương trình
Insert_Value.htm
- "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- "//www.w3.org/1999/xhtml">
- "text/javascript" >
- function InsertRecord[] function InsertRecord[]
- {
- var txtid = document.getElementById['txtid'].value; var txtid = document.getElementById['txtid'].value;
- var txtname = document.getElementById['txtname'].value; var txtname = document.getElementById['txtname'].value;
- var txtsalary = document.getElementById['txtsalary'].value; var txtsalary = document.getElementById['txtsalary'].value;
- var txtcity = document.getElementById['txtcity'].value; var txtcity = document.getElementById['txtcity'].value;
- & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;| & nbsp; txtsalary.length & nbsp;! = 0 || & nbsp; txtcity.length & nbsp;! = 0]if [txtid.length != 0 || txtname.length !=0 || txtsalary.length !=0|| txtcity.length !=0]
- {
- var connection = new ActiveXObject["ADODB.Connection"]; var connection = new ActiveXObject["ADODB.Connection"];
- var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB"; var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";
- connection.Open[connectionstring];
- var rs = new ActiveXObject["ADODB.Recordset"]; var rs = new ActiveXObject["ADODB.Recordset"];
- rs.Open["insert into Emp_Info values['" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "']", connection]; "insert into Emp_Info values['" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "']", connection];
- alert["Insert Record Successfuly"]; "Insert Record Successfuly"];
- txtid.value = " "; " ";
- connection.close[];
- }
- else else
- {
- & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;; \ n & nbsp; Mức lương & nbsp;"Please Enter Employee \n Id \n Name \n Salary \n City "];
- }
- }
- function ShowAll[] function ShowAll[]
- {
- var connection = new ActiveXObject["ADODB.Connection"]; var connection = new ActiveXObject["ADODB.Connection"];
- var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB"; var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";
- connection.Open[connectionstring];
- var rs = new ActiveXObject["ADODB.Recordset"]; var rs = new ActiveXObject["ADODB.Recordset"];
- rs.Open["select * from Emp_Info ", connection]; "select * from Emp_Info ", connection];
- rs.MoveFirst[];
- var span = document.createElement["span"]; var span = document.createElement["span"];
- span.style.color = "Blue"; "Blue";
- span.innerText = " ID " + " Name " + " Salary" + " City "; " ID " + " Name " + " Salary" + " City ";
- document.body.appendChild[span];
- while [!rs.eof] while [!rs.eof]
- {
- var span = document.createElement["span"]; var span = document.createElement["span"];
- span.style.color = "green"; "green";
- & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;; "& nbsp;+& nbsp; rs.fields [0] & nbsp;+& nbsp;" & nbsp;; "& nbsp;+& nbsp; rs.fields [2] & nbsp;+& nbsp;" & nbsp;"\n " + rs.fields[0] + " | " + rs.fields[1] + " | " + rs.fields[2] + " | " + rs.fields[3];
- document.body.appendChild[span];
- rs.MoveNext[];
- }
- rs.close[];
- connection.close[];
- }
- "text/css">
- #main
- {
- height: 264px;
- }
- #ShowRecord
- {
- width: 67px;
- z-index: 1;
- left: 20px;
- top: 257px;
- position: absolute;
- }
- #showall
- {
- z-index: 1;
- left: 114px;
- top: 257px;
- position: absolute;
- }
- "height: 431px">
- "show"
- style="font-size: x-large; font-weight: bold; height: 298px; color: #009999;"> "font-size: x-large; font-weight: bold; height: 298px; color: #009999;">
- Insert Employee Record "font-size: medium; color: #000000;">
- Employee Id
- "txtid" type="text" />
- "font-size: medium; color: #000000;">
- Name
- "txtname" type="text" />
- "font-size: medium; color: #000000;">
- Salary
- "txtsalary" type="text" />
- "font-size: medium; color: #000000;">
- City
- "txtcity" type="text" />
- "ShowRecord" type="button" value="Insert" title="InsertRecord[]" />
- "showall" type="button" value="Show All Record" title="ShowAll[]" />