javascript怎么与数据库连接

javascript怎么与数据库连接,第1张

第一步定义连接字符串

var MM_conn_STRING = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=F:\\cs\\benlen\\gbmdb.mdbPersist Security Info=False"

第二步创建adodb对象

var con = Server.CreateObject("ADODB.Connection")con.ActiveConnection = MM_conn_STRINGcon.Open()

这下 就连接好了数据库。

第三步创建记录集

var rs = Server.CreateObject("ADODB.Recordset")rs.ActiveConnection = MM_conn_STRINGrs.Source = "SELECT * FROM admin"rs.CursorType = 0rs.CursorLocation = 2rs.LockType = 1rs.Open()var rs_numRows = 0

这样我们数据库连接就完成了

以下是示例代码:

<script type="text/javascript" language="javascript" charset="gb2312">

<!--

function getCountFromDB() {

//以当前页面文件为基础,找到文件所在的绝对路径。

var filePath = location.href.substring(0, location.href.indexOf("index.html"))

var path = filePath + "MyData.mdb"

//去掉字符串中最前面的"files://"这8个字符。

path = path.substring(8)

var updateCnt = 0

//生成查询和更新用的sql语句。

var sqlSelCnt = "SELECT * FROM [user]"

//建立连接,并生成相关字符串。

var con = new ActiveXObject("ADODB.Connection")

con.Provider = "Microsoft.Jet.OLEDB.4.0"

con.ConnectionString = "Data Source=" + path

con.open

var rs = new ActiveXObject("ADODB.Recordset")

rs.open (sqlSelCnt,con)

while (!rs.eof) {

var cnt = rs.Fields("content")

document.write (cnt)

rs.moveNext

}

javascript连接sqllite方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta content="text/htmlcharset=ISO-8859-1" http-equiv="content-type">

<title>Prova</title>

</head>

<body>

<script type="text/javascript">

//Provenia SRL ITC - Paola Savioli

//Questa funzione apre il database SQL Lite

//Il parametro che va cambiato è il nome del database

function ApriDatabase() {

try {

if (window.openDatabase) {

var shortName = 'Ristoranti.sqllite'

var version = '1.0'

var displayName = 'Ristoranti italia'

var maxSize = 65536// in bytes

db = openDatabase(shortName, version, displayName, maxSize)

}

} catch (e) {

alert('Apri Database' + e)

}

}

//Provenia SRL ITC - Paola Savioli

// Questa funzione eseque una query su un database aperto con la funzione ApriDatabase

function EseguiQuery($query, callback) {

try {

ApriDatabase()

if (window.openDatabase) {

db.transaction(

function (tx) {

tx.executeSql($query, [], function (tx, result) {

if (typeof (callback) == "function") {

callback(result)

} else {

if (callback != undefined) {

eval(callback + "(result)")

}

}

}, function (tx, error) {})

})

return rslt

}

} catch (e) {

alert('Esegui Query' + e)

}

}

function VisualizzaComuni() {

try {

var schemanode = document.GetElementById('RCOMUNI')

schemanode.innerHTML = ""

var result = EseguiQuery('SELECT * FROM COMUNE')

for (var i = 0i <result.rows.lenght++i) {

var row = result.row.item(i)

var notediv = document.createElement('div')

notediv.innerHTML = 'Codice Provincia:' + row['PROVINCIA'] + 'Nome:' + row['NAME']

schemanode.appendchild(notediv)

}

} catch (e) {

alert('Visualizza Comuni' + e)

}

}

</script>

<input type="button" name='select' onClick="VisualizzaComuni()"

value='Visualizza Comuni'>

<div id="RCOMUNI"></div>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9641670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存