當(dāng)前位置: 首頁(yè)IT技術(shù) → 教你數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)現(xiàn)添加,顯示,修改,刪除的方法

教你數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)現(xiàn)添加,顯示,修改,刪除的方法

更多

本文章將給大家用最精簡(jiǎn)的實(shí)例介紹到實(shí)現(xiàn)顯示、添加、修改、刪除的比較簡(jiǎn)便的方法,雖然是以ASP例子介紹,但同樣適用于PHP、JSP、ASP.NET等語(yǔ)言。文中用到了java script腳本,大家也可從中學(xué)到部分的java script知識(shí),希望給大家?guī)?lái)幫助和啟發(fā)。

  因?yàn)榻鼛滋旃ぷ魈,就不祥?xì)的寫(xiě)原理文章了,我在代碼里面寫(xiě)的注釋非常清楚,相信懂點(diǎn)網(wǎng)頁(yè)編程的朋友應(yīng)該都能夠看懂。

  聲明:希望各位朋友轉(zhuǎn)載時(shí),不要把原有作者版權(quán)去掉,謝謝合作。

  由于本人寫(xiě)作水平有限,寫(xiě)的不好的地方,還請(qǐng)大家多多包涵,如要批評(píng)或意見(jiàn),請(qǐng)加我QQ。

  以下正文開(kāi)始:

  功能介紹:

  平時(shí)很多人至少需要做三個(gè)表單才能實(shí)現(xiàn)數(shù)據(jù)的顯示、添加、修改、刪除,本文講的是只用一個(gè)添加表單,就可實(shí)現(xiàn)數(shù)據(jù)的顯示、添加、修改、刪除功能。用此方法寫(xiě)程序時(shí),至少可以省兩倍的精力。

  實(shí)現(xiàn)例子:

  例子說(shuō)明:

數(shù)據(jù)庫(kù)名:db.mdb
表名:pub_doc
字段:id和memo1-----memo30 (多少個(gè)字段大家可以自己設(shè)置)
公用部分:
公用部分,可以存成單個(gè)文件,用include調(diào)用。

<%
'數(shù)據(jù)庫(kù)連接
db="db.mdb"
set conn=server.createobject("adodb.connection")
conn.open "provider=microsoft.jet.oledb.4.0;data source="& server.mappath(db)

'發(fā)布時(shí)間:2005年12月30日 晚
'文章作者:翟振凱 (小琦)
'個(gè)人站:http://www.xiaoqi.net   
'技術(shù)站:http://www.iisvs.net
'商業(yè)站:http://www.iisvs.com
'論壇站:http://www.tdqy.com
'QQ:53353866 22336848

'添加數(shù)據(jù)函數(shù)
function add_form(sl)'添加數(shù)據(jù)函數(shù)

'response.write request("act")
'response.end

if request("act")="add_save" and sl<>"" then

'------------------生成SQL語(yǔ)句----------------小琦

'--------循環(huán)memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","
                        if request("memo"&i&"")<>"" then                               
                                zd_value=zd_value&"'"&request("memo"&i&"")&"'"&","
                        else
                                zd_value=zd_value&"'"&null&"'"&","
                        end if                       
                        next
'//--------循環(huán)memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)
zd_value=left(zd_value,len(zd_value)-1)

sql="Insert into pub_doc("&zd_name&")values("&zd_value&")"

'//------------------生成SQL語(yǔ)句----------------小琦
conn.execute(sql)
response.write "添加數(shù)據(jù)成功!"
Response.End
end if

end function

'修改數(shù)據(jù)函數(shù)
function edit_form(sl)'修改數(shù)據(jù)函數(shù)

if request("id")<>"" and request("act")="edit_save" and sl<>"" then

'------------------生成SQL語(yǔ)句----------------小琦
               
'--------循環(huán)memo
                zd_name="" '字段名子
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&"="
                        if request("memo"&i&"")<>"" then                               
                                zd_name=zd_name&"'"&request("memo"&i&"")&"'"&","
                        else
                                zd_name=zd_name&"'"&null&"'"&","
                        end if                       
                        next
'//--------循環(huán)memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

'//------------------生成SQL語(yǔ)句----------------小琦

sql="Update pub_doc set "&zd_name&" where id="&request("id")&""
conn.execute(sql)

response.write "修改成功!"

Response.End()
end if
end function

function read_edit_form(sl)'修改前讀取數(shù)據(jù)函數(shù)
if request("id")<>"" and request("act")="edit" and sl<>"" then'讀取要修改的數(shù)據(jù)

'------------------生成SQL語(yǔ)句----------------小琦
                        zd_name="" '字段名子
'--------循環(huán)memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","       
                        next
'//--------循環(huán)memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL語(yǔ)句----------------小琦
set rs = conn.execute(sql)'查詢(xún)

response.write "<script language=java script>"'寫(xiě)java script的腳本
response.write "function read_data(){"'

'--------循環(huán)賦值
                        for each i in split(zd_name,",")
                                response.write "frm."&i&".value="""&Replace(Replace(Replace(rs(i)&"||",chr(13),"\n"),chr(10),"\n"),"||","")&""";"'
                        next
'//--------循環(huán)賦值

response.write "}</script>"

end if
end function

function read_form(sl)'讀取數(shù)據(jù)函數(shù)
if request("id")<>"" and request("act")="" and sl<>"" then'讀取要查看的數(shù)據(jù)

'------------------生成SQL語(yǔ)句----------------小琦
'--------循環(huán)memo
                        For i = 1 To sl
                                zd_name=zd_name&"memo"&i&","       
                        next
'//--------循環(huán)memo

'去掉最后的“,”
zd_name=left(zd_name,len(zd_name)-1)

sql="select top 1 "&zd_name&" from pub_doc where id="&request("id")&""
'//------------------生成SQL語(yǔ)句----------------小琦
set rs = conn.execute(sql)'查詢(xún)

response.write "<sc"+"ript language=java script>"'寫(xiě)java script的腳本
response.write "function read_data(){"'
'--------循環(huán)賦值
                        for each i in split(zd_name,",")
                        if rs(i)="" then kongge=" "'如果內(nèi)容為空,則用空格代替
                                response.write "frm."&i&".parentElement.innerText="""&rs(i)&kongge&""";"'
                        next
'//--------循環(huán)賦值
response.write "}</scr"+"ipt>"
end if
end function

function del()'刪除數(shù)據(jù)

if request("id")<>"" and request("act")="del" then

conn.execute("Delete from pub_doc where id="&request("id")&" ")
response.write "刪除成功!"
response.end

end if

end function

function xiaoqi_end()
if request("id")<>"" and request("act")="" then response.write "<sc"+"ript language=java script>frm.save.removeNode(true);read_data()</sc"+"ript>"
if request("id")<>"" and request("act")="edit" then response.write "<sc"+"ript language=java script>read_data()</sc"+"ript>"
session("act")=""
end function

function get_act(sl)
add_form       sl
edit_form      sl
read_edit_form sl
read_form      sl
del
end function

'設(shè)置動(dòng)作
if request("act")="add" then session("act")="add_save"
if request("act")="edit" then session("act")="edit_save"
%>

  調(diào)用實(shí)例

<%get_act("26")‘使用了26個(gè)字段%>
<% if request("act")<>"" or request("id")<>"" then’如果不是顯示列表頁(yè)面%>

<p> </p>
<form method="POST" action="index.asp" name="frm">
        <div align="center">
                <table border="1" width="600" id="table2" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
                        <tr>
                                <td width="149">數(shù)據(jù)表</td>
                                <td width="149"><input type="text" name="memo1"></td>
                                <td width="150"><input type="text" name="memo2"></td>
                                <td width="150"><input type="text" name="memo3"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo4"></td>
                                <td width="149"><input type="text" name="memo7"></td>
                                <td width="150"><input type="text" name="memo6"></td>
                                <td width="150"><input type="text" name="memo5"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo9"></td>
                                <td width="149"><input type="text" name="memo10"></td>
                                <td width="150"><input type="text" name="memo11"></td>
                                <td width="150"><input type="text" name="memo12"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo16"></td>
                                <td width="149"><input type="text" name="memo15"></td>
                                <td width="150"><input type="text" name="memo14"></td>
                                <td width="150"><input type="text" name="memo13"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo17"></td>
                                <td width="149"><input type="text" name="memo18"></td>
                                <td width="150"><input type="text" name="memo19"></td>
                                <td width="150"><input type="text" name="memo20"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo24"></td>
                                <td width="149"><input type="text" name="memo23"></td>
                                <td width="150"><input type="text" name="memo22"></td>
                                <td width="150"><input type="text" name="memo21"></td>
                        </tr>
                        <tr>
                                <td width="149"><input type="text" name="memo25"></td>
                                <td width="149"><input type="text" name="memo26"></td>
                                <td width="150"><input type="text" name="memo8"></td>
                                <td width="150"><input type="submit" value="提交" name="save"></td>
                        </tr>
                </table>
        </div>
<input type="hidden" name="id" value="<%=request("id")%>">
<input type="hidden" name="act" value="<%=session("act")%>">
</form>
<p> </p>

<%
xiaoqi_end()
else%>

<p align="center"><a href="?act=add">添加</a></p>
<div align="center">
        <table border="1" width="600" id="table1" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
                <tr>
                        <td width="142">字段1</td>
                        <td width="142">字段2</td>
                        <td width="143">字段3</td>
                        <td width="63">查看</td>
                        <td width="63">修改</td>
                        <td width="63">刪除</td>
                </tr>
<%
set rs=server.createobject("adodb.recordset") 
sql = "select * from pub_doc order by id desc "
rs.open sql,conn,1,1
do while not rs.eof
%>               
                <tr>
                        <td width="142"> <%=rs("memo1")%></td>
                        <td width="142"> <%=rs("memo2")%></td>
                        <td width="143"> <%=rs("memo3")%></td>
                        <td width="63"><a href="?id=<%=rs("id")%>">查看</a></td>
                        <td width="63"><a href="?id=<%=rs("id")%>&act=edit">修改</a></td>
                        <td width="63"><a href="?id=<%=rs("id")%>&act=del">刪除</a></td>
                </tr>
<%
rs.MoveNext                                             
loop %>
        </table>
</div>
<%end if%>

  再給大家一個(gè)我原創(chuàng)的用java script將文本框轉(zhuǎn)換為文本的例子,相信很多人都會(huì)用到。大家存成html文件打開(kāi)即可看到效果。

  原理:從文本框中取值,把值賦于文本框的父對(duì)象。

<script language=java script>
function read_data(){
frm.memo1.parentElement.innerText="1";
frm.memo2.parentElement.innerText="2";
frm.memo3.parentElement.innerText="3";
}</script>

<p> </p>
<form method="POST" action="index.asp" name="frm">
<div align="center">
<table border="1" width="600" bordercolorlight="#000000" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" style="text-align: center">
<tr>
<td width="149">數(shù)據(jù)表</td>
<td width="149"><input type="text" name="memo1"></td>
<td width="150"><input type="text" name="memo2"></td>
<td width="150"><input type="text" name="memo3"></td>
</tr>
</table>
<p><input type="submit" value="提交" name="save"></div>
</form>
<script language=java script>frm.save.removeNode(true);read_data()</script>

  asp版完整實(shí)例代碼+數(shù)據(jù)庫(kù)
熱門(mén)評(píng)論
最新評(píng)論
發(fā)表評(píng)論 查看所有評(píng)論(0)
昵稱(chēng):
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)