當前位置:首頁 » 數據倉庫 » asp連接資料庫實例
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

asp連接資料庫實例

發布時間: 2022-06-04 11:02:56

『壹』 ASp連接資料庫

用的最多的就是ACCESS和sql
Server資料庫,連接語句如下:
1.
ASP連接Access資料庫語句
Set
Conn=Server.CreateObject("ADODB.Connection")
Connstr="DBQ="+server.mappath("www/bbs.mdb")+";DefaultDir=;DRIVER={Microsoft
AccessDriver(*.mdb)};"
Conn.Open
connstr
其中Set
Conn=Server.CreateObject("ADODB.Connection")為建立一個訪問數據的對象
server.mappath("www/bbs.mdb")是告訴伺服器access
資料庫訪問的路徑
2.
ASP連接Sqlserver資料庫語句
Set
conn
=
Server.CreateObject("ADODB.Connection")
conn.Open"driver={SQLServer};server=202.108.32.94;uid=wu77445;pwd=p780522;database=w
ww_panwei_com"
conn
open
其中/Set
conn
=
Server.CreateObject("ADODB.Connection")為設置一個資料庫的連接對象
driver=()告訴連接的設備名是SQL-SERVER
server是連接的伺服器的ip地址,Uid是指用戶的用戶名,pwd是指的用戶的password,
database是用戶資料庫在伺服器端的資料庫的名稱

『貳』 如何用ASP連接SQLSERVER資料庫

連接方法有三種分別為通過ODBC DSN建立連接,通過oledb建立連接 通過driver建立連接三種,下面我們來看看第一種。

通過driver建立連接
代碼如下
<%
Const DataBaseType=1
If DataBaseType=0 then
DBPath="/jb51/news.asp"
SqlNowString = "Now()"
ystr=true
nstr=false
suiji="rnd(id)"
Else
'如果是SQL資料庫,請認真修改好以下資料庫選項
DataServer = "www111cnnet" '資料庫伺服器IP
DataUser = "jb51net" '訪問資料庫用戶名
DataBaseName = "jb51net" '資料庫名稱
DataBasePsw = "密碼" '訪問資料庫密碼
SqlNowString = "getdate()"
ystr=1
nstr=0
suiji="newid()"
End if
On Error Resume Next
If DataBaseType = 1 Then
ConnStr="driver={SQL Server};server="&dataserver&";UID="&datauser&";PWD="&databasepsw&";Database="&databasename
Else
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(DBPath)
End If
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr
If Err Then Err.Clear:Set conn = Nothing:Response.Write "資料庫連接出錯,請檢查Conn.asp文件中的資料庫參數設置。":Response.End
%>
通過driver建立連接
通過driver建立頁面與資料庫的連接,同樣不需要創建ODBC DSN數據源,但必須知道實際的資料庫文件路徑或者數據源名(例如,SQLserver的資料庫)。
代碼如下
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open"driver={SQL Server};server=abc;DataSource=(test);uid=;pwd=;database=UserDB"

編寫腳本和資料庫源建立連接
ADO(ActiveX Data Objects ) 提供 Connection 對象,可以使用該對象建立和管理應用程序和 ODBC 資料庫之間的連接。Connection 對象具有各種屬性和方法,可以使用它們打開和關閉資料庫連接。編寫資料庫連接腳本,首先應創建 Connection 對象的實例,接著打開資料庫連接

代碼如下
'********************************************************************
' 與SQL Server2000有關的連接
' 可以參照它建立您的資料庫連接
'********************************************************************
'敬請注意:
'請根據情況配置StrServer,StrUid,StrSapwd,StrDbName四個參數
Dim StrServer,StrUid,StrSaPwd,StrDbName
StrServer="(local)" '資料庫伺服器名
StrUid="testuser" '您的登錄帳號
StrSaPwd="12345" '您的登錄密碼
StrDbName="db_test_com" '您的資料庫名稱
Dim Conn '資料庫連接
Dim StrDSN '資料庫連接字元串
Dim Rs '命令字元串
StrDSN="driver={SQL server};server="&StrServer&";uid="&StrUid&";pwd="&StrSaPwd&";database="&StrDbName
'建立和資料庫master的連接
set Conn = Server.CreateObject("ADODB.Connection")
set Rs=Server.CreateObject("ADODB.RecordSet")
Conn.Open StrDSN

『叄』 求一個asp與sql資料庫連接的應用實例,越簡單越好。

創建資料庫連接對象
<%
set dxm=server.CreateObject("adodb.connection")
blm="driver=sql server;server=你計算機的IP地址;database=web;uid=sa;pwd="
dxm.open blm
%>

uid=sa;pwd= 你可以打開你的查詢分析器看看他是用什麼身份驗證的
如果不是sa和空密碼就自己設置一個用戶.

dxm和blm是自己定的對象名和變數名

添加數據:
<%
set sql=server.CreateObject("adodb.recordset")
sql.open "select * from 表名",dxm,1,3 '這里是全表查詢

sql.addnew
sql("欄位名")=要添加的數據(如果是用表單提交就用 request.form("表單文本框名稱") )

你有幾個欄位就添加幾次(例如:sql("name")=request.from("username"))

sql.update
%>

上面是添加數據的方法,現在該查詢了:
<%
set sql=server.CreateObject("adodb.recordset")
sql.open "select * from 表名",dxm,1,3 '這里是全表查詢
%>
在你想要顯示的地方做上<%=sql("欄位名")%>就可以了.

刪除:
將添加數據那裡的代碼改為:
<%set sql=server.CreateObject("adodb.recordset")
sql.open "select * from 表名 where 條件",dxm,1,3

sql.delete
%>

條件 就是例如name=小張 等等類似的

改:同添加一樣
將添加中的sql.addnew去掉
改為:sql.update
代碼如下:
<%
set sql=server.CreateObject("adodb.recordset")
sql.open "select * from 表名",dxm,1,3 '這里是全表查詢

sql.update
sql("欄位名")=要更改的數據(如果是用表單提交就用 request.form("表單文本框名稱") )

你改幾個欄位就寫幾次(例如:sql("name")=request.from("username")
sql("sex")=request.from("sex"))

%>

這是用sql語句的對象做的,下面給你一些增、刪、改、查 的代碼

增:
全表添加:insert into 表名 values (值1,值2,……值n)
部分欄位添加:
insert into 表名(欄位1,欄位2,……欄位n) values(值1,值2,……值n)

刪:delete from 表名 where 條件
改:update 表名 set 欄位名1=新值1,欄位名2=新值2,……where 條件
查:select * from (全表查詢)
select * from 表名 where 條件 (帶條件查詢)

『肆』 ASP怎樣連接ACCESS資料庫 最好給個例子

ASP連接ACCESS資料庫
Dim conn,connstr
on error resume next
connstr="dbq="+server.mappath("&DataBase&")+";defaultdir=;driver={microsoft access driver (*.mdb)};"
set conn=server.createobject("adodb.connection")
conn.open connstr
sub CloseConn()
conn.close
set conn=nothing
end sub

『伍』 ASP怎麼連接SQL資料庫

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data.SqlClient;//注意需要添加此句

namespaceaspnet3
{
publicpartialclassdatatest:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
stringstrconn="server=localhost;uid=sa;pwd=longlt;database=School";
SqlConnectionconn=newSqlConnection(strconn);//創建連接
stringsql="select*fromstudents";
conn.Open();
SqlCommandcmd=newSqlCommand(sql,conn);//執行查詢
Response.Write("連接成功");
SqlDataReaderdr=cmd.ExecuteReader();//查詢結果
if(dr.Read())
{
//利用dr[索引]對資料庫表進行操作,dr[]返回object;
//可以用欄位做索引,也可用列號0,1..做索引
Response.Write(dr[0].ToString()+"<br>");
}

//this.Lab.Text="suc";
}
}
}

在上面的例子中,我們連接了一個sa下的School資料庫,並查詢了其中students欄位的內容。

連接資料庫分為三個步驟:先定義連接信息,再創建一個連接,最後打開連接

stringstrconn="server=localhost;uid=sa;pwd=longlt;database=School";//在這一段修改資料庫的信息
SqlConnectionconn=newSqlConnection(strconn);//創建連接
conn.Open();//打開連接

『陸』 如何用asp連接SQL Server 資料庫

<%
Dim mode
mode=request.form("text4")
if mode=1 then
name=request.form("text1")
age=request.form("text2")
addr=request.form("text3")
if not isnumeric(age) then response.write "age must be number!<a href=javascript:history.go(-1)>try again</a>":response.end
age=clng(age)
end if
"********************************************************************
" 與SQL Server2000有關的連接
" 可以參照它建立您的資料庫連接
"********************************************************************
"敬請注意:
"請根據情況配置StrServer,StrUid,StrSapwd,StrDbName四個參數
Dim StrServer,StrUid,StrSaPwd,StrDbName
StrServer="(local)" "資料庫伺服器名
StrUid="testuser" "您的登錄帳號
StrSaPwd="12345" "您的登錄密碼
StrDbName="db_test_com" "您的資料庫名稱
Dim Conn "資料庫連接
Dim StrDSN "資料庫連接字元串
Dim Rs "命令字元串
StrDSN="driver={SQL server};server="&StrServer&";uid="&StrUid&";pwd="&StrSaPwd&";database="&StrDbName
"建立和資料庫master的連接
set Conn = Server.CreateObject("ADODB.Connection")
set Rs=Server.CreateObject("ADODB.RecordSet")
Conn.Open StrDSN
"********************************************************************
"********************************************************************

Dim strsql
"********************************************************************
" 讀資料庫的相關操作
sub readdb()
strsql="select * from test"
rs.open strsql,conn,1,1
if rs.EOF then response.write "no record at all":exit sub
response.write "<table border=1>"
response.write "<tr>"
for i=0 to rs.Fields.Count-1
response.write "<td><font color=blue>"&rs.Fields(i).Name&"</font></td>"
next
response.write "</tr>"
while not rs.EOF
response.write "<tr>"
for i=0 to rs.Fields.Count-1
response.write "<td>"&rs.Fields(i).Value&"</td>"
next
response.write "</tr>"
rs.MoveNext
wend
response.write "</table>"
rs.Close
end sub
"********************************************************************

"********************************************************************
" 寫資料庫的相關操作
sub insertdata()
strsql="INSERT INTO test(name,age,addr) VALUES(""&name&"","&age&",""&addr&"")"
rs.Open strsql,conn,1,3
end sub
"********************************************************************
if mode=1 then
call insertdata()
response.write "insert ok!"
elseif mode=2 then
call readdb()
end if
"釋放資料庫連接對象
set rs=nothing
set conn=nothing
%>
<HTML>
<HEAD>
<TITLE></TITLE>
<script language=javascript>
function clickit(flag){
var form1=document.form2
form1.text4.value=flag;
if (flag==1){
if (form1.text1.value==""){
alert("name cant empty!");
return false;
}
if (form1.text2.value==""){
alert("age cant empty!");
return false;
}
if (form1.text3.value==""){
alert("addr cant empty!");
return false;
}
}
form1.submit();
return true;
}
</script>
</HEAD>
<BODY>
<form method=post name=form2>
name:<INPUT type="text" id=text1 name=text1 size=12>
age:<INPUT type="text" id=text2 name=text2 size=12>
city:<INPUT type="text" id=text3 name=text3 size=12><br>
<INPUT type="hidden" id=text4 name=text4>
<INPUT type="button" value="write" id=button1 name=button1 onclick="clickit(1)">
<INPUT type="button" value="read" id=button2 name=button2 onclick="clickit(2)">
</form>
</BODY>
</HTML>

『柒』 ASP 怎麼連接SQL資料庫

ASP與SQL資料庫連接語句具體如下:

Set conn = Server.CreateObject("ADODB.Connection")
connstr = "provider=Sqloledb;server=伺服器名;uid=用戶名;pwd=密碼;database=資料庫名"
conn.Open connstr
If Err Then
err.Clear
Set conn = Nothing
Response.Write "資料庫連接出錯,請檢查連接字串"
Response.End

(7)asp連接資料庫實例擴展閱讀:

SQL常用命令使用方法:

(1) 數據記錄篩選:

sql="select * from 數據表 where 欄位名=欄位值 order by 欄位名 "

sql="select * from 數據表 where 欄位名 like 『%欄位值%『 order by 欄位名 "

sql="select top 10 * from 數據表 where 欄位名 order by 欄位名 "

sql="select * from 數據表 where 欄位名 in (『值1『,『值2『,『值3『)"

sql="select * from 數據表 where 欄位名 between 值1 and 值2"

(2) 更新數據記錄:

sql="update 數據表 set 欄位名=欄位值 where 條件表達式"

sql="update 數據表 set 欄位1=值1,欄位2=值2 …… 欄位n=值n where 條件表達式"

(3) 刪除數據記錄:

sql="delete from 數據表 where 條件表達式"

sql="delete from 數據表" (將數據表所有記錄刪除)

『捌』 求asp。net連接資料庫的最最簡單的例子

string MyConn = "Initial Catalog=Northwind;Data Source="您伺服器名稱";User ID=賬戶名 Password=密碼";//定義資料庫連接參數
SqlConnection MyConnection = new SqlConnection();//定義一個數據連接實例
MyConnection.ConnectionString = MyConn;
try
{
MyConnection.Open();
Response.Write("連接資料庫成功!");
}
catch (Exception e)
{
Response.Write(e.Message);
}
finally
{
MyConnection.Close();
}

『玖』 asp 資料庫連接,讀取,寫入,修改的簡單例子

Makes a qualified network management key to lie in is the person, is asolid person Although said: " Does was inferior to looks, lookswas inferior to said, said is inferior to the dawdle, but I thought isthe person is most important, 1st, makes the network management, mainly is maintains theserver, the terminal, the customer end and network synthesis wiring aswell as network planning and so on, perhaps just started to think workload very big, mood very bothersome, pressure very big, was the workis impetuous for others' feeling, passes through pondering over whicha period of time working practice and lived, was allowed to feel themain question was oneself has not learned to adjust oneself, adjustedown life appeal, in the nature work question also meets You Rener tosolve, and has understood the life happiness. 2nd, is a qualified network administrator most to need to grasp anetwork reasonable plan, dynamic management, static surveillance,long-distance debugging maintenance, including network topology,network agreement transmission step, network flow control, QOs, eachkind of agreement disposition and reasonable use. Network administrator itself is the technical post, therefore thetechnology must first. As for any technology most important, that mustthink each unit the demand, the simple possibility so long asconnected and can the exchange visits be good. The complex networkpossibly was several people even more people's matter, had thedivision of labor and the cooperation, various person of maintenanceand the study direction is dissimilar. The common middle and smallscale unit all does not suppose the network management, because thecomputer are few, does not need specially to set up sentries, hasoutside the question to ask the person to go. Surpasses 20 unitspossibly to have to suppose the special network management or theconcurrent job network management, such unit's network management hasthe IT various aspects on the request the knowledge, jumps over GuangYuehao. Two: The enterprise network management needs to grasp the skill makesnetwork management nearly anything to need to know that a spot, notnecessarily must fine, certainly you also must have own strong point. 1. Made the system is the most basic request, from 98 to 2003, all hadto be able to play from unix to linux, fine (this difficulty systemvery was not necessarily all high) 2. Can maintain the PC hardware and the printer (to spurts inkfrom needle type to arrive laser), if this part does is not good,possibly every day suffices your busy morning 3. Meets the MAIL service and the customer end disposition and themanagement, mainly has Exchange, Imail, Qmail, Sendmail and so on, thepresent enterprise all has own MAIL, moreover occupies the status highis absolutely not allow to neglect 4. To the windows/*nix system must know the common servicedisposition, most basic certainly is DHCP (DHCPD), DNS (BIND), IIS(APACHE), FTP (WUFTPD/VSFTPD), AD (SAMBA), WINS and so on, if theseall not too understand, hurries ruthlessly to make up Otherwise didnot must go 5. The database at least needs to understand SQL SERVER with MYSQL, ifmeets ORACLE/SYBASE/DB2/INFORMIX, that wages definitely can be high10% (ha-ha a little exaggerates, if these can, but also did not makeDBA to go?) 6. Certainly wants to the switchboard and the router simpleestablishment and the management, otherwise only could go to the smallbusiness (mainly is CISCO, China is 3COM, north electricity, certainlyto TP-LINK, the D-LINK low end equipment also had to be familiarwith). 7. The familiar synthesis wiring technology (at least knew howmakes 568A/568B), the optical fiber technology also needs slightly tounderstand 12, if you respond to a call for recruits is the factoryspeech, the workshop often can pull the optical fiber with theworkshop between 8. Must know how plans the network, enhances the network the stability(to be most important) as far as possible, security and use factorand so on 9. Can write the script, was not effective is windows or *nix, thescript often can twice the result with half the effort cause yourworking efficiency (to assemble language and so on /C has beenbetter). 10. Must know how the fast security backup with does restorethe data 11. To technology and so on proxy firewall 殺毒 must be familiar,otherwise which day was your network completely paralysed has notknown our matter The 12.WLAN technology also must grasp as soon as possible, this is atendency, the very many enterprises' partial networks all melted intoit 13. To turns on the net technology to have to be familiar, at leastmust know ADSL, ISDN, FTTX, FR, how DDN is a matter 14. Certainly some companies hire when the manager requests you tomeet ASP, PHOTOSHOP, DW and so on, they mainly are the website routinemaintenance 15. Must have a clear understanding to the entire network model andthe overhead construction, at least must know the level, theagreement, the connection, the service and so on knows, if can gnawthoroughly the TCP/IP agreement these three volumes book, then youwere allowed to start the cow 16. Has a clear understanding to the ERP system 17. Most important, also is decided the destiny the matter, needs tolearn " Enres " Small does not enre then the chaotic bigstratagem, this speech is very appropriate to the network management