如何在ASP中使用mySQL

2016-01-29 18:00 3 1 收藏

如何在ASP中使用mySQL,如何在ASP中使用mySQL

【 tulaoshi.com - ASP 】

  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

%



<TABLE



?/span<TR

牋 <TD<bName</b</TD

牋 <TD<bEmail 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.


来源:http://www.tulaoshi.com/n/20160129/1503918.html

延伸阅读
标签: SQLServer
懒得翻译原文了,一般在视图中使用Order by是无效的,除非使用了Top关键字,那么我们可以用Top 100 percent来欺骗SQL的语法分析器。 USE pubs GO CREATE VIEW AuthorsByName AS SELECT TOP 100 PERCENT * FROM authors ORDER BY au_lname, au_fname GO
标签: ASP
  VBScript5中增加了许多新功能,最振奋人心的当属类和正则表达式的出现。以下是本人写的一个解析html代码的类。我是 学php的,语法有不习惯的地方,请大家多包含。 <% Class HTMLParse     ' 设置 Initialize 事件。     Private Sub Class_Initialize      &nbs...
如何在EDITBOX控件中使用背景位图 作者:zhoudb 下载本文源代码 在VC中,标准的WINDOWS控件如TREEVIEW,EDITBOX,COMBOBOX和LISTBOX等控件都不支持选择背景位图的属性,所以如果要使这些标准控件达到这种效果,必须有些非常规的方法。本文介绍一个CEDIT类如何实现背景位图,并且可以更换背景的例...
标签: ASP
  MYSQL数据库以它短小、方便、速度快、免费等优点成为很多网站目前首选数据库,但一般都是用PHP+MYSQL相结合来开发各种动态页面,其实ASP也可以使用MYSQL数据库开发动态页面,小弟我也是刚刚学会,不敢独享,所以特写了这篇文章供大伙参考。 我的环境是WINDOWS98+PWS4.0+mysql-3.23.32-win+PHP4 必要的软件:PWS4.0(呵呵,废...
标签: word
如何在Word2013中使用声音提示功能   第1步,打开Word2013文档窗口,依次单击文件→选项按钮。 第2步,打开Word选项对话框,切换到高级选项卡。在常规区域选中提供声音反馈复选框,并单击确定按钮。 Word2013怎样使用字符度量单位   第1步,打开Word2013文www.Tulaoshi.com档窗口,依次单击文件→选项按...