當前位置:首頁 » 操作系統 » asp使用js連接資料庫資料庫

asp使用js連接資料庫資料庫

發布時間: 2022-09-06 16:58:34

① 跪求:ASP.NET中如何應用jstree,並與sql server資料庫進行連接

我正在用sql,也是2005。我比較傾向於你用express版本。其他版本的功能你基本用不到,倒是龐大許多。
express版本只有幾百m,開發版卻要2g多,幾乎十倍。你可以設想,一個十倍體積的軟體和一倍的軟體哪個輕便、啟動快。express就是專門針對初學者而用的。

② 在asp.net中JS怎樣操作資料庫

最好別使用js操作資料庫,js在源代碼裡面可以看到,別人不就知道你的資料庫信息了?還可以通過注入操作你的資料庫 如果最實在想用,參考例子 文件構成:
Access資料庫名為 MyData.Mdb, 裡面建了一個名為count的表,表由兩個欄位組成: ID和COUNT,表裡有一條數據: ('count','100')。 文本文件名為 count.txt,裡面隨便寫入一個數字。 靜態頁面名為Cnt.htm。
以上3個文件都放在同一目錄下。

由於採用Access資料庫,而它採用非標准SQL語法,所以要注意在表名和欄位名上需要加方括弧: []

下面是頁面里的代碼:

其中getCountFromDB方法是對Access資料庫進行操作,getCountFromTxt方法是對txt純文本進行操作,這兩個方法都在本地執行通過。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="javaScript">
<!--
function getCountFromDB() {
//以當前頁面文件為基礎,找到文件所在的絕對路徑。
var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm"));
var path = filePath + "MyData.mdb";
//去掉字元串中最前面的"files://"這8個字元。
path = path.substring(8);
var updateCnt = 0;

//生成查詢和更新用的sql語句。
var sqlSelCnt = "SELECT COUNT FROM [COUNT] WHERE ID = 'count'";
var sqlUpdCnt = "UPDATE [COUNT] SET [COUNT] = '";

//建立連接,並生成相關字元串 www.knowsky.com。
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("COUNT");
document.write(cnt);
//將取得結果加1後更新資料庫。
updateCnt = cnt * 1 + 1;
rs.moveNext;
}
rs.close();
rs = null;

sqlUpdCnt = sqlUpdCnt + updateCnt + "'";
con.execute(sqlUpdCnt);

con.close();
con = null;
}

function getCountFromTxt() {
var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm"));
var path = filePath + "count.txt";
path = path.substring(8);
var nextCnt = 0;

var fso, f1, ts, s;
//以只讀方式打開文本文件。
var ForReading = 1;
//以讀寫方式打開文本文件。
var ForWriting = 2;

fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.GetFile(path);
ts = f1.OpenAsTextStream(ForReading, true);
s = ts.ReadLine();
nextCnt = eval(s) + 1;
document.write("now count is :" + s);
ts.Close();

ts = f1.OpenAsTextStream(ForWriting, true);
ts.WriteLine(nextCnt);
ts.close();
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
getCountFromTxt();
//-->
</SCRIPT>
</BODY>
</HTML>

後記:雖然在只支持靜態頁面的免費空間不可用,但是我想一些支持Asp的免費空間應該能用,當然我沒有具體試過,只是憑自己想像。並且用這種方法來讀寫資料庫,只適合做計數器和留言版這樣的不需要保密性的工作,畢竟代碼全是用javascript寫成的,誰都可以看到,然後就可以得到路徑把db文件下載下來進行分析,所以沒辦法用它來保存一些不想讓別人知道的東西。就像我一個同事說的,用javascript連接資料庫進行操作,簡直就是霸王硬上弓,呵呵,的確如此,既然不能用在免費空間上作自己的計數器,那這段代碼就只能用來玩玩,自己找找良好感覺了。收集:

簡單的資料庫操作:<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language=javascript>
function connectDb()
{
var ConnDB = new ActiveXObject("ADODB.Connection");
ConnDB.ConnectionString="dsn=shbxdatabase;uid=ldbx;pwd=123456;";
ConnDB.Open();
var Rs = new ActiveXObject("ADODB.Recordset");
var tempstr="";
Rs.Open("select * from WDA1",ConnDB,1,3);
while(!Rs.EOF)
{
tempstr = tempstr + Rs("TNAME") + "<br/>";
Rs.MoveNext;
}
test.innerHTML=tempstr;
}
</script>
</HEAD>
<BODY onbeforeunload="return 'ok?'"><P><div id=test> </div>
<P><INPUT id=button1 type=button value=顯示 name=button1 onclick="connectDb()"></BODY>
</HTML>
>>>>>>-------------------------------------var db="D:\\js\\9t\\db1.mdb";
function opendb(dbname,sql)
{
var opendb =new ActiveXObject("ADODB.Recordset")
opendb.ActiveConnection = "DBQ="+dbname+";DRIVER={Microsoft Access Driver (*.mdb)};"
opendb.Source = sql
opendb.CursorType = 1
opendb.CursorLocation = 2
opendb.LockType = 3
opendb.Open()
return opendb
}
var rs=new Object();
rs=opendb(db,"select * from 產品 where id="+i);
document.all.xx1.innerHTML=rs("生產廠商").value;
document.all.xx2.innerHTML=rs("產品名稱")+"/"+rs("型號");
document.all.xx3.innerHTML=rs("售價");
document.all.xx4.innerHTML=rs("批發");
document.all.xx5.innerHTML=rs("備注");
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---------------------------------------
連接ACCESS只能用數據源,操作資料庫的方法跟ASP之類是一樣的。
只不過執行的語句不一樣罷了。
如:
String dbUrl = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbConStr = "jdbc:odbc:dnsName";
Connection Conn = null;
Statement stmt = null;
try{
Class.forName(dbUrl);
}catch(ClassNotFoundException e){
System.err.println("Not Found ClassForName" + e.getMessage());
}
try{
Conn = DriverManager.getConnection(dbConStr, "uid", "pwd"); //連接資料庫,uid與pwd分別為 用戶名及密碼(連接資料庫的),如果沒有既為空
stmt = Conn.createStatement();
}catch(Exception e){
System.err.println("Can't Connect the DATA" + e.getMessage());
}
//以下為操作資料庫的
String sql = "select * from tableName[....]";
ResultSet Rs = stmt.executeQuery(sql); // 返回記錄集
String sqlUpdate = "update...";
String sqlInsert = "insert into....";
stmt.executeUpdate(sqlUpdate); //更新記錄
stmt.executeUpdate(sqlInsert); //插入記錄
Conn.close();
stmt.close();

③ asp結合javascript調取資料庫

下面代碼使用servlet框架:

jsp頁面:

<formaction="DoRegisterServlet"method="post"id="form1">

<tableclass="tab1">

<tr>

<td>

用戶注冊:

</td>

<tdcolspan="2">

<divid="info"style="color:red;"></div>

</td>

</tr>

<tr>

<td>

工號:

</td>

<td>

<inputtype="text"name="userid"value=""id="gh"onBlur="checkuid(this.value)"/>

</td>

<td>

<divclass="fc"id="uid">

*請輸入8位純數字工號

</div>

</td>

</tr>
</table>
</form>
javascript:
function$(id){
returndocument.getElementById(id);
}
/*
*TODO:ajax驗證工號是否存在
*@param{Object}val
*/
functionajaxuid(val){
varxmlhttp;
if(window.XMLHttpRequest){
xmlhttp=newXMLHttpRequest();
}elseif(window.ActiveXobject){
try{
xmlhttp=newActiveXobject("Microsoft.XMLHTTP");
}catch(e){
xmlhttp=newActiveXobject("Msxml2.XMLHTTP");
}
}
xmlhttp.onreadystatechange=function(){
if(4==xmlhttp.readyState&&200==xmlhttp.status){
varinfo=xmlhttp.responseText;
$("info").innerHTML=info;

if(info=="可使用"){

$("uid").innerHTML="√有效";
$("uid").style.color="green";
flag1=true;

}else{

$("gh").focus();
flag1=false;
returnfalse;
}
}else{
$("info").innerHTML="後台驗證中請稍等.....";
}
}
xmlhttp.open("get","AjaxUidServlet?uid1="+val+"&t="+Math.random(),
true);
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
xmlhttp.send();
}
/*
*TODO:工號非空、格式認證
*@param{Object}val
*/
functioncheckuid(val){
if(val==""){
$("uid").innerHTML="工號不可為空!";
$("gh").focus();
}else{
varreg=newRegExp("^[0-9]{8}$","g");
if(reg.test(val)){

ajaxuid(val);
}else{

$("uid").innerHTML="工號格式不正確!";
$("info").innerHTML="";
$("gh").focus();
}
}
}
java代碼:
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importmain..user.UserDao;
/**
*@ClassName:AjaxUidServlet
*@Description:ajax驗證用戶注冊工號是否重復
*@author:zhangbin13075600
*@date:2013-8-15
*/
{
/**
*
*/
=1004L;
/**
*Constructoroftheobject.
*/
publicAjaxUidServlet(){
super();
}
/**
*Destructionoftheservlet.<br>
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*@
*@
*@
*@
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/plain;charset=UTF-8");
Stringval=request.getParameter("uid1");
UserDaoud=newUserDao();
PrintWriterout=response.getWriter();
booleanb=ud.findByUid(val);
if(b==true){
out.write("工號已存在");

}else{

out.write("可使用");

}

out.flush();
}
/**
*@
*@
*@
*@
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
/**
*Initializationoftheservlet.<br>
*
*@
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}

④ asp 用JS連接ACCESS資料庫

var db_bbs
db_bbs="dd.mdb"//定義資料庫
var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+db_bbs+"");

試試這樣看行不行!

⑤ js怎麼連接mysql資料庫連接

具體連接方法如下:

1、打開HBuilder工具,在Web項目中的js文件夾中,新建JavaScript文件ConnDB.js

使用require()引入mysql模塊,然後賦值給變數mysql

⑥ javascript連接資料庫的問題

可能是路徑出錯了,你的網頁和資料庫是在同一個目錄下么?

網頁似乎在桌面上!

還有就是許可權的問題!

⑦ 用JavaScript連接資料庫本地可以執行連接並查詢,發布到IIS上就無法連接了

資料庫在伺服器的話,就需要用後台語言(php、jsp或asp等)去讀取然後再返回給前端。而JavaScript是前端語言,它只會在本地電腦執行,如果用它去直接讀取伺服器上的資料庫也是可以的,但必須滿足幾個條件:瀏覽器的安全等級要調低(否則瀏覽器是不允許此類腳本運行的);資料庫要設置成允許外部連接;JavaScript的連接字串中,SERVER=(local) 中的(local)要改為資料庫所在伺服器的IP地址。做到這些也還不一定能成,要根據實際情況處理。總之,撇開嚴重的安全隱患不說,我們也是不建議用網頁腳本去直接連接資料庫的

⑧ 急!!! 我如何在ASP裡面接收從JS傳過來的數據,然後再寫進資料庫裡面,下面是一個關於留言的JS

你輸入的字元有中文是嗎?
那麼先JS部分這樣寫:

$.post("savely.asp",
{
operateType: "15",
name: escape(_name),
tel: _tel,
email: _email,
fax: _fax,
address: escape(_address),
company: escape(_company),
msg: _msg
},

這里是關鍵。然後在savely.asp頁面中寫:
先加一個ASP函數
'與javascript中的unescape()等效
Function UnEscape(ByVal str)
Dim x, s
x = InStr(str,"%")
s = ""
Do While x>0
s = s & Mid(str,1,x-1)
If LCase(Mid(str,x+1,1))="u" Then
s = s & ChrW(CLng("&H"&Mid(str,x+2,4)))
str = Mid(str,x+6)
Else
s = s & Chr(CLng("&H"&Mid(str,x+1,2)))
str = Mid(str,x+3)
End If
x=InStr(str,"%")
Loop
UnEscape = s & str
End Function
然後下面是調用方法:
Dim GetoperateType:GetoperateType = Request.Form("operateType")
Dim Getname:Getname = UnEscape(Request.Form("name"))
其他欄位也是這樣的寫法。就能得到數據了。 並且支持中文了。

⑨ 在asp中使用js循環語句提取資料庫中記錄

樓上說的不對,asp使用js做伺服器端語言的時候可以連接資料庫。只是這沒有什麼意義。

asp默認使用VBscript,連接資料庫後直接循環輸出即可

dimrs,sql,str
SetRS=Server.CreateObject("ADODB.RECORDSET")
sql="selecttop20*fromK1where1=1"
'rs.opensql,conn,1,1
'ifrs.bofandrs.eofthen
'str="thereisnothing!"
'else
'str=""
'dowhilenotrs.eof
'str=str&"<divclass='id'>"&rs("id")&"</div><divclass='t'>"&rs("title")&"</div><divclass='b'>"&rs("content")&"</div>"
'rs.movenext
'loop
'endif
'rs.close:setrs=nothing
'response.writestr
熱點內容
安卓手機怎麼登錄ipadid 發布:2024-04-27 23:52:25 瀏覽:670
瀏覽量和訪問次數 發布:2024-04-27 23:44:56 瀏覽:475
在linuxpython 發布:2024-04-27 22:38:57 瀏覽:316
機頂盒密碼是在哪裡 發布:2024-04-27 22:32:47 瀏覽:158
名圖買哪個配置值得買 發布:2024-04-27 22:32:36 瀏覽:878
比亞迪秦pro選哪個配置好 發布:2024-04-27 22:32:34 瀏覽:534
logn演算法 發布:2024-04-27 21:58:36 瀏覽:596
11選五的簡單演算法 發布:2024-04-27 21:46:14 瀏覽:71
ebay圖片上傳 發布:2024-04-27 21:31:50 瀏覽:587
微信電腦登錄顯示伺服器錯誤 發布:2024-04-27 20:58:08 瀏覽:135