当前位置: 首页 > 图文教程 > 网络编程 > ASP > 如何在ASP中使用mySQL

ASP
ASP技巧:在Access数据库中重命名表
用ASP编程实现网络内容快速查找
比较ASP生成静态HTML文件的几种方法
ASP实例:实现邮件发送普通附件和嵌入附件
如何用ASP实现去掉三个最高分和三个最低分
ASP实例:Access为后台数据库的网站统计系统
用标签替换的方法生成静态网页
例程:用ASP判断文件地址是否有效
学ASp动态网页必备:常用的38个函数
ASP教程:初次接触学习ASP脚本程序
ASPJPEG水印中关于文字水印的帮助文档(中英文对照)
ASP例子:ASP把汉字转化为拼音的函数
ASP教程:学习ASP应用Cookies的技巧
ASP入门:认识ASP程序所使用的几种脚本语言
初学者的ASP教程:常用ASP内置函数
ASP初级教程之ASP对表单和用户输入的处理
学习ASP文件引用的方法
用ASP编写更人性化的弹出窗口程序
谈谈学习ASP动态网页制作技术的编程心得
用ASP程序实现网站在线人数统计

如何在ASP中使用mySQL


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 24 ::
收藏到网摘: n/a

  Using A mySQL Databases
by Ben O'Neill


Databases are the best way to keep your web site up-to-date and dynamic. Databases are used these days by
thousands of web sites. They are used for storing news and general information. Web sites like the ASP
Index (www.aspin.com) are run on large databases. Databases make a web site easy to update and once you
have the base script, to add, remove and modify things in a database is very easy.



To start you need to know how to connect to a database. ASP can connect to virtually any type, from
Microsoft Access to SQL. In this example I'll be using mySQL and OLE DB to connect to it.



mySQL can be downloaded from the mySQL web site (www.mysql.com). You will also need the provider used to
connect to it, also available from the mySQL web site.



You may be asking what's OLE DB? I'm used to ODBC and DSN. OLE DB is faster and more stable. It's almost
exactly the same.



First we need to connect to the database, because it's a mySQL database you also need to supply a database
name. (in mySQL you can have mulitple databases on the same SQL server.)



<%

strConnection = "driver={MySQL};server=localhost;uid=benoneill;pwd=mypassword;database=databasename"



Set adoDataConn = Server.CreateObject("ADODB.Connection")

adoDataConn.Open strConnection

%>



And now we've connected. Let's pretend we've got a big list of lots and lots of email addresses, here's
the contents of our database, it allows me to show you how it works better.



Table Name: emailadds



name牋牋牋牋牋牋牋?牋牋牋牋牋?emailadd

------------------------------------------------------

Ben牋牋牋牋牋牋牋牋?牋牋牋牋牋[email protected]

Fred牋牋牋牋牋牋牋牋 牋牋牋牋牋[email protected]

Ben Harding牋牋 牋牋牋牋牋[email protected]

Dave Geralding 牋牋牋牋牋[email protected]



Now we have the database open let's run a query to list and output all the names and email address in a
nice easy to view table.



<%

?strQuery = "SELECT * FROM emailadds"



?Set rsEmailData = adoDataConn.Execute(strQuery)



?If Not rsEmailData.BOF Then

%>



<TABLE>



?/span><TR>

牋 <TD<b>Name</b></TD>

牋 <TD><b>Email Address</b></TD>

?/span></TR>



<%

?Do While Not rsEmailData.EOF

%>



?/span><TR>

牋 <TD><%=rsEmailData("name").Value %></TD>

牋 <TD><%=rsEmailData("emailadd").Value %></TD>

?/span></TR>



<%

牋?rsEmailData.MoveNext

?Loop



%>



</TABLE>



<%

?Else



牋? Response.Write("Sorry, no email addresses found.")



?End If

%>





There we go. If no records are found then it says "Sorry, no email addresses found".



That query is simple enough, it tells the database to get (SELECT) all the records and all the fields from
the table named emailadds.



How about we make it only show people with the name "ben" somewhere in their name, simple change the query
to this:



SELECT * FROM emailadds WHERE name LIKE '%ben%'


That query would return only 2 records, Ben and Ben Harding.



It's important you use single quotes ('), because double quotes won't work. You can also be very selective
and do:



SELECT * FROM emailadds WHERE name='Ben'


That query would only return Ben, not Fred or Ben Harding, or Dave Geralding.