JavaScript操作數據庫JS操作Access數據庫,跟其他語言操作差不多,總結了一下習慣代碼,僅供參考學習。
現在在F盤有文件abc.mdf,表名為Student,一共2個字段,Id數字類型主鍵,stuName文本類型,現對該表進行增刪改查的操作:
1.查詢
復制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>數據查詢</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.增加操作
復制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>增加操作</TITLE>
<script language="javascript">
function addUser(id,stuName)
{
//用 JavaScript 寫服務器端連接數據庫的代碼示例
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.execute(sql);
alert("添加成功");
}
catch(e){
document.write(e.description);
alert("添加失敗~~~");
}
conn.close();
}
</script>
</HEAD>
<BODY>
<table width=100 border=1>
<tr bgcolor='#f4f4f4'>
<td>編號</td>
<td>姓名</td>
</tr>
<tr>
<td><input id="stuId" /></td>
<td><input id="stuName"/></td>
</tr>
</table>
<input name="1" type="button" value="添加" onclick="addUser(stuId.value,stuName.value)"/>
</BODY>
</HTML>
3.刪除操作
復制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>刪除操作</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.execute(sql);
conn.close();
conn = null;
alert("修改成功");
}
</script>
</HEAD>
<BODY>
<input name="1" type="button" value="刪除" onclick="delStu(1)"/>
</BODY>
</HTML>
4.修改操作
復制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>修改操作 </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.execute(sql);
conn.close();
conn = null;
alert("修改成功");
}
</script>
<BODY>
<table width=100 border=1>
<tr bgcolor='#f4f4f4'>
<td>編號</td>
<td>姓名</td>
</tr>
<tr>
<td><input id="stuId" /></td>
<td><input id="stuName"/></td>
</tr>
</table>
<input name="1" type="button" value="修改" onclick="updateUser(stuId.value,stuName.value)"/>
</BODY>
</HTML>
另外,JS也可以操作SQL Server數據庫
數據庫名為:MySchool,表名為Student,StudentId為int類型,自增列,studentName為學生姓名,為varchar類型。數據庫用戶名為sa,密碼是ok,
復制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>SQL數據查詢</TITLE>
<Script >
var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Driver={SQL server};Server=.;DataBase=MySchool;UID=sa;Password=ok;");//打開數據庫
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>