本文介绍了使用JavaScirpt在sqlserver数据库中插入Record。但它不是在DB中插入记录。任何人都可以帮我解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<head>
<title></title>
<script language="javascript" type="text/javascript" >
function InsertRecord()
{
var txtid = document.getElementById('txtid').value;
var txtname = document.getElementById('txtname').value;
var txtsalary = document.getElementById('txtsalary').value;
var txtcity = document.getElementById('txtcity').value;
if (txtid.length != 0 || txtname.length !=0 || txtsalary.length !=0|| txtcity.length !=0)
{
var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "Data Source=RTSIND018\sqlexpress;Initial Catalog=AliDB;User ID=sa;Password=***";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("insert into Emp_Info values('" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "')", connection);
alert("Insert Record Successfuly");
txtid.value = " ";
connection.close();
}
else
{
alert("Please Enter Employee \n Id \n Name \n Salary \n City ");
}
}
function ShowAll()
{
var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "Data Source=RTSIND018\sqlexpress;Initial Catalog=AliDB;User ID=sa;Password=***";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("select * from Emp_Info ", connection);
rs.MoveFirst();
var span = document.createElement("span");
span.style.color = "Blue";
span.innerText = " ID " + " Name " + " Salary" + " City ";
document.body.appendChild(span);
while (!rs.eof)
{
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "\n " + rs.fields(0) + " | " + rs.fields(1) + " | " + rs.fields(2) + " | " + rs.fields(3);
document.body.appendChild(span);
rs.MoveNext();
}
rs.close();
connection.close();
}
</script>
<body>
<input id="txtid" type="text" /></p>
<p style="font-size: medium; color: #000000;">
Name
<input id="txtname" type="text" /></p>
<p style="font-size: medium; color: #000000;">
Salary
<input id="txtsalary" type="text" /></p>
<p style="font-size: medium; color: #000000;">
City
<input id="txtcity" type="text" /></p>
<input id="ShowRecord" type="button" value="Insert" onclick="InsertRecord()" />
<input id="showall" type="button" value="Show All Record" onclick="ShowAll()" /></div>
</body>
</html>
推荐答案
这篇关于使用JavaScirpt在sqlserver数据库中插入Record。但它不是在DB中插入记录。任何人都可以帮我解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!