您的位置:首页技术开发ASP技巧 → 如何在ASP中使用mySQL

如何在ASP中使用mySQL

时间:2004/11/7 4:02:00来源:本站整理作者:蓝点我要评论(0)

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牋牋牋牋牋牋牋牋?牋牋牋牋牋?sheepcow@planetunreal.com



Fred牋牋牋牋牋牋牋牋 牋牋牋牋牋?freddy@thebigisp.com



Ben Harding牋牋 牋牋牋牋牋?benharding@hisisp.com



Dave Geralding 牋牋牋牋牋?daveg@mymail.com







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



%>















?/span>



Name







?/span>







<%



?Do While Not rsEmailData.EOF



%>







?/span>











?/span>







<%



牋?rsEmailData.MoveNext



?Loop







%>







Email Address
<%=rsEmailData("name").Value %> <%=rsEmailData("emailadd").Value %>








<%



?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.







After using databases you should always clear up. Close the database and the record set, and set them to

nothing so that the memory used by them is regained, do this by writing this:







<%



?rsEmailData.Close



?adoDataConn.Close







?Set adoDataConn = Nothing



?Set rsEmailData = Nothing



%>


相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 VB.NET 2005编写定时关 Jquery get/post下乱码解决方法 前台gbk gb如何使用数据绑定控件显示数据ASP脚本循环语句ASP怎么提速

人气排行 轻松解决"Server Application Error"和iis"一起学习DataGridView调整列宽用ASP随机生成文件名的函数Jquery get/post下乱码解决方法 前台gbk gbODBC Drivers错误80004005的解决办法返回UPDATE SQL语句所影响的行数的方法用Javascript隐藏超级链接的真实地址两个不同数据库表的分页显示解决方案