<p>I have a couple of questions regarding VBScript and ASP Classic:</p>

<ol>
<li><p>What is the preferred way to access an MS SQL Server database in VBScript/ASP?</p></li>
<li><p>What are best practices in regards to separating model from view from controller?</p></li>
<li><p>Any other things I should know about either VBScript or ASP?</p></li>
</ol>

<p>If you haven't noticed, I'm new at VBScript coding.  I realize numbers 2 &amp; 3 are kind of giant "black hole" questions that are overly general, so don't think that I'm expecting to learn everything there is to know about those two questions from here.</p>

[{'answer_id': 26181, 'author': 'Michael Pryor', 'author_id': 245, 'author_profile': 'https://Stackoverflow.com/users/245', 'pm_score': 5, 'selected': True, 'text': '<p>ADO is an excellent way to access a database in VBScript/Classic ASP.</p>\n\n<pre><code>Dim db: Set db = Server.CreateObject("ADODB.Connection")\ndb.Open "yourconnectionstring -&gt; see connectionstrings.com"\nDim rs: Set rs = db.Execute("SELECT firstName from Employees")\nWhile Not rs.EOF\n    Response.Write rs("firstName")\n    rs.MoveNext\nWend\nrs.Close\n</code></pre>\n\n<p>More info here: <a href="http://www.technowledgebase.com/2007/06/12/vbscript-how-to-create-an-ado-connection-and-run-a-query/" rel="noreferrer">http://www.technowledgebase.com/2007/06/12/vbscript-how-to-create-an-ado-connection-and-run-a-query/</a></p>\n\n<p>One caveat is that if you are returning a MEMO field in a recordset, be sure you only select ONE MEMO field at a time, and make sure it is the LAST column in your query.  Otherwise you will run into problems.\n(Reference: <a href="http://lists.evolt.org/archive/Week-of-Mon-20040329/157305.html" rel="noreferrer">http://lists.evolt.org/archive/Week-of-Mon-20040329/157305.html</a> )</p>\n'}, {'answer_id': 26315, 'author': 'JasonS', 'author_id': 1865, 'author_profile': 'https://Stackoverflow.com/users/1865', 'pm_score': 2, 'selected': False, 'text': '<p>On number 2, I think you have a few options...</p>\n\n<p>1) You can use COM components developed in VB6 or the like to separate some of your business logic from your UI.</p>\n\n<p>2) You can create classes in VBScript.  There is no concept of inheritance and other more advanced features are missing from the implementation, but you can encapsulate logic in classes that helps reduce the spagehtti-ness of your app.  Check out this: <a href="https://web.archive.org/web/20210505200200/http://www.4guysfromrolla.com/webtech/092399-1.shtml" rel="nofollow noreferrer">https://web.archive.org/web/20210505200200/http://www.4guysfromrolla.com/webtech/092399-1.shtml</a></p>\n'}, {'answer_id': 26336, 'author': 'Ryan Rinaldi', 'author_id': 2278, 'author_profile': 'https://Stackoverflow.com/users/2278', 'pm_score': 2, 'selected': False, 'text': "<p>Echoing some ideas and adding a few of my own: </p>\n\n<p>1) Best way to access the database would to abstract that away into a COM component of some sort that you access from VBScript.</p>\n\n<p>2) If you really wanted to you could write the controller in VBScript and then access that in the page.  It would resemble a Page Controller pattern and not a Front Controller that you would see in ASP.NET MVC or MonoRail</p>\n\n<p>3) Why are you doing this to yourself?  Most of the tooling required to do this kind of work isn't even available anymore.</p>\n"}, {'answer_id': 26699, 'author': 'George Godik', 'author_id': 2759, 'author_profile': 'https://Stackoverflow.com/users/2759', 'pm_score': 1, 'selected': False, 'text': '<p>way way back in the day when VBScript/ASP were still ok\nI worked in a utility company with a very mixed DB envrionment, I used to swear by this website: <a href="http://www.connectionstrings.com/" rel="nofollow noreferrer">http://www.connectionstrings.com/</a></p>\n\n<p>@michealpryor got it right</p>\n'}, {'answer_id': 60335, 'author': 'Scott', 'author_id': 6126, 'author_profile': 'https://Stackoverflow.com/users/6126', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve been stuck building on ASP, and I feel your pain.</p>\n\n<p>1) The best way to query against SQL Server is with parameterized queries; this will help prevent against SQL injection attacks.</p>\n\n<p>Tutorial (not my blog):<br>\n<a href="http://www.nomadpete.com/2007/03/23/classic-asp-which-is-still-alive-and-parametised-queries/" rel="nofollow noreferrer"><a href="http://www.nomadpete.com/2007/03/23/classic-asp-which-is-still-alive-and-parametised-queries/" rel="nofollow noreferrer">http://www.nomadpete.com/2007/03/23/classic-asp-which-is-still-alive-and-parametised-queries/</a></a></p>\n\n<p>2) I haven\'t seen anything regarding MVC specifically geared towards ASP, but I\'m definitely interested because it\'s something I\'m having a tough time wrapping my head around. I generally try to at least contain things which are view-like and things which are controller-like in separate functions. I suppose you could possibly write code in separate files and then use server side includes to join them all back together.</p>\n\n<p>3) You\'re probably coming from a language which has more functionality built in. At first, some things may appear to be missing, but it\'s often just a matter of writing a lot more lines of code than you\'re used to.</p>\n'}, {'answer_id': 91504, 'author': 'jammus', 'author_id': 984, 'author_profile': 'https://Stackoverflow.com/users/984', 'pm_score': 3, 'selected': False, 'text': '<p>Remember to <em>program into</em> the language rather than program in it. Just because you\'re using a limited tool set doesn\'t mean you have to program like it\'s 1999.</p>\n\n<p>I agree with JasonS about classes. It\'s true you can\'t do things like inheritance but you can easily fake it</p>\n\n<pre><code>Class Dog\n    Private Parent\n\n    Private Sub Class_Initialize()\n        Set Parent = New Animal\n    End Sub\n\n    Public Function Walk()\n        Walk = Parent.Walk\n    End Function\n\n    Public Function Bark()\n        Response.Write("Woof! Woof!")\n    End Function\nEnd Class\n</code></pre>\n\n<p>In my projects an ASP page will have the following:\nINC-APP-CommonIncludes.asp - This includes stuff like my general libraries (Database Access, file functions, etc) and sets up security and includes any configuration files (like connection strings, directory locations, etc) and common classes (User, Permission, etc) and is included in every page.</p>\n\n<p>Modules/ModuleName/page.vb.asp - Kind of like a code behind page. Includes page specific BO, BLL and DAL classes and sets up the data required for the page/receives submitted form data, etc</p>\n\n<p>Modules/ModuleName/Display/INC-DIS-Page.asp - Displays the data set up in page.vb.asp.</p>\n'}, {'answer_id': 91513, 'author': 'jammus', 'author_id': 984, 'author_profile': 'https://Stackoverflow.com/users/984', 'pm_score': 1, 'selected': False, 'text': '<p>Also for database access I have a set of functions - GetSingleRecord, GetRecordset and UpdateDatabase which has similar function to what Michael mentions above</p>\n'}, {'answer_id': 92146, 'author': 'Cirieno', 'author_id': 17615, 'author_profile': 'https://Stackoverflow.com/users/17615', 'pm_score': 4, 'selected': False, 'text': '<p>I had to walk away from my PC when I saw the first answer, and am still distressed that it has been approved by so many people.  It\'s an appalling example of the very worst kind of ASP code, the kind that would ensure your site is SQL-injectable and, if you continue using this code across the site, hackable within an inch of its life.</p>\n\n<p>This is NOT the kind of code you should be giving to someone new to ASP coding as they will think it is the professional way of coding in the language!</p>\n\n<ol>\n<li><p>NEVER reveal a connection string in your code as it contains the username and password to your database.  Use a UDL file instead, or at the very least a constant that can be declared elsewhere and used across the site.</p></li>\n<li><p>There is no longer any good excuse for using inline SQL for any operation in a web environment.  Use a stored procedure -- the security benefits cannot be stressed enough.  If you really can\'t do that then look at inline parameters as a second-best option... Inline SQL will leave your site wide open to SQL injection, malware injection and the rest. </p></li>\n<li><p>Late declaration of variables can lead to sloppy coding.  Use "option explicit" and declare variables at the top of the function.  This is best practice rather than a real WTF, but it\'s best to start as you mean to go on.</p></li>\n<li><p>No hints to the database as to what type of connection this is -- is it for reading only, or will the user be updating records?  The connection can be optimised and the database can handle locking very efficiently if effectively told what to expect.</p></li>\n<li><p>The database connection is not closed after use, and the recordset object isn\'t fully destroyed.</p></li>\n</ol>\n\n<p>ASP is still a strong language, despite many folks suggesting moving to .NET -- with good coding practices an ASP site can be written that is easy to maintain, scaleable and fast, but you HAVE to make sure you use every method available to make your code efficient, you HAVE to maintain good coding practices and a little forethought.  A good editor will help too, my preference being for PrimalScript which I find more helpful to an ASP coder than any of the latest MS products which seem to be very .NET-centric.</p>\n\n<p>Also, where is a "MEMO" field from?  Is this Access nomenclature, or maybe MySQL?  I ask as such fields have been called TEXT or NTEXT fields in MS-SQL for a decade.</p>\n'}, {'answer_id': 185205, 'author': 'jwalkerjr', 'author_id': 689, 'author_profile': 'https://Stackoverflow.com/users/689', 'pm_score': 2, 'selected': False, 'text': '<p>I agree with @Cirieno, that the selected answer would not be wise to use in production code, for all of the reasons he mentions. That said, if you have just a little experience, this answer is a good starting point as to the basics.</p>\n\n<p>In my ASP experience, I preferred to write my database access layer using VB, compiling down to a DLL and referencing the DLL via VBScript. Tough to debug directly through ASP, but it was a nice way to encapsulate all data access code away from the ASP code.</p>\n'}, {'answer_id': 1103513, 'author': 'My Alter Ego', 'author_id': 93868, 'author_profile': 'https://Stackoverflow.com/users/93868', 'pm_score': 2, 'selected': False, 'text': '<p>AXE - Asp Xtreme Evolution is a MVC framework for ASP classic</p>\n\n<p>There are some attempts at making test frameworks for asp:\naspUnit is good, but no longer maintained.</p>\n\n<p>I saw a sample on how to make your own one a few months back.\nThe example used nUnit to call functions against the website for automatic testing.\nI think i got it off <a href="http://www.beingnew.net/2008/11/automating-classic-asp-unit-tests-with.html" rel="nofollow noreferrer">here</a> (my line is borked so I can\'t check)</p>\n'}]
['https://Stackoverflow.com/questions/26137', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2147/']