使用Javascript读取和写入访问数据库 [英] Read and write to an access database using Javascript

查看:495
本文介绍了使用Javascript读取和写入访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想提一下,我知道在与数据库的web交互中,由于安全原因,以及因为javascript

为是不提供和兼容性的Windows文件系统。



表示我面对的是一种情况,我想创造性地考虑。

我不允许任何访问服务器端脚本, SQL。



,我需要为内部网创建一个基于客户端的应用程序,以便能够随时间进度存储数据。



我已经找到了2个解决方案,但没有一个有足够的文档,让我正确使用。



一个是名为ACCESSdb的JavaScript库,可以在这里找到: ACCESSdb

很遗憾,我无法理解如何使用它从数据库写入或读取数据...



,另一个是那3个

  function AddRecord() {
var adoConn = new ActiveXObject(ADODB.Connection);
var adoRS = new ActiveXObject(ADODB.Recordset);

adoConn.Open(Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='/ \dbName.mdb');
adoRS.Open(Select * From tblName,adoConn,1,3);

adoRS.AddNew;
adoRS.Fields(FieldName)。value =Quentin;
adoRS.Update;

adoRS.Close();
adoConn.Close();
}

删除记录:

  function DeleteRecord(){
var adoConn = new ActiveXObject(ADODB.Connection);
var adoRS = new ActiveXObject(ADODB.Recordset);

adoConn.Open(Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='\\dbName.mdb');
adoRS.Open(Select * From tblName其中FieldName ='Quentin',adoConn,1,3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}

编辑记录:

  function EditRecord(){
var adoConn = new ActiveXObject(ADODB.Connection);
var adoRS = new ActiveXObject(ADODB.Recordset);

adoConn.Open(Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='\\dbName.mdb');
adoRS.Open(Select * From tblName其中FieldName ='Quentin',adoConn,1,3);

adoRS.Edit;
adoRS.Fields(FieldName)。value =New Name;
adoRS.Update;

adoRS.Close();
adoConn.Close();
}

其中只有添加新记录因为某些原因为我工作...

也发现,要读取第一行中的任何单元格的值,我所要做的是写:

  alert(adoRS(cellNum)); 

但是如何获取后面行中的单元格的值?让我们说(第3行,单元格5)。



感谢您阅读这篇文章!我会感谢您的帮助了很多!



Jake

解决方案

首先, '



其次,这里是一个删除命令的版本:

  function DeleteRecord(){
var adoConn = new ActiveXObject(ADODB.Connection);
var adoCmd = new ActiveXObject(ADODB.Command);

adoConn.Open(Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='\\dbName.mdb');
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText =Delete * From tblName其中FieldName ='Quentin';
adoCmd.Execute();

adoConn.Close();然后,编辑命令(无循环 - >更新所有[匹配]记录)。
}



< :

  function EditRecord(){
var adoConn = new ActiveXObject(ADODB.Connection);
var adoCmd = new ActiveXObject(ADODB.Command);

adoConn.Open(Provider = Microsoft.Jet.OLEDB.4.0; Data Source ='\\dbName.mdb');
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText =Update tblName Set FieldName ='New Value'其中FieldName ='Quentin';
adoCmd.Execute();

adoConn.Close();
}

请注意,我没有测试这个(现在没有Access) ,所以可能有一些
语法错误...



希望它的工作和帮助。


first I want to mention that I am aware that in web interaction with databases should
always be with server side languages due to security reasons and for the fact that javascript
as is doesn't offer and compatibility with the windows file system.

that said I am facing a though situation and I am trying to think creatively.
I am not allowed any access to a server sided scripting and SQL.

and I need to create a client based application for an intranet that will be able to store data as time progress.

I have found 2 solutions so far but none of them has enough documentation for me to use correctly.

one is a javascript library called ACCESSdb which can be found here:ACCESSdb
unfortunately I couldn't understand how to use it to write or read data from the DB...

and the other is those 3 pieces of code:

Adding a Record:

function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='/\dbName.mdb'");
adoRS.Open("Select * From tblName", adoConn, 1, 3);

adoRS.AddNew;
adoRS.Fields("FieldName").value = "Quentin";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

Removing a Record:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}  

Editing a Record:

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);

adoRS.Edit;
adoRS.Fields("FieldName").value = "New Name";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

out of them only the add new record one worked for me for some reason...
also I found that to read the value of any cell in the first row all I had to do was to write:

alert(adoRS(cellNum));  

but how do I get the value of cells in the later rows? lets say (row 3,cell 5).

Thank you for reading this far! I'll appreciate your help a lot!

Jake

解决方案

First, make sure that '/\' and '\' (in connection string) is just a typo in SO.

Second, here is a version of Delete command:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Delete * From tblName Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}

And, Edit command (without looping -> updates all [matching] records):

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Update tblName Set FieldName = 'New Value' Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}  

Please note, I have not tested this (do not have Access right now), so there might be some syntax bugs...

Hope it works and helps.

这篇关于使用Javascript读取和写入访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆