The finished product :)

Results 1 to 2 of 2

Thread: The finished product :)

  1. #1
    Join Date
    Dec 1969

    Default The finished product :)

    I just got done writing my first object from scratch.<BR><BR>I have always hated the way you have to add records to a DB using SQL, when you have 10 or more DB Fields, the SQL string gets really hard to read.<BR><BR>Soo....<BR><BR>I created an object to do it all for you, all you have to do is supply the Data Source, the Table Name that you want to insert into, and the fields in the correct order of your DB table in a comma delimeted string.<BR><BR>Well, I wanted to show you guys the object, and I wanted to know if there are any really bad techniques in it, even tho it works.<BR><BR>Well here the is Class, its all commented, tell me what you think :)<BR><BR>Class AddtoDatabase<BR><BR> Public Data<BR> Public SQLTable<BR> Public SQLFields<BR> Public HTMLFields<BR> Public SplitSQLFields<BR> Public SplitHTMLFields<BR> Public DataValue<BR> Public DataType<BR> Public SQLValues<BR> Public DataSource<BR><BR> &#039;Set Default SQL Statment<BR><BR> Public Property Get DefaultSQL<BR> DefaultSQL = "INSERT INTO " & SQLTable & " (" & SQLFields & ")" & " VALUES ("<BR> End Property<BR><BR> <BR> Public Function CreateSQL()<BR> Dim HTMLField<BR> Dim sDbField, sField<BR> <BR> &#039;Turn user-defined comma delimeted list of SQL fields into an array of data<BR> SplitSQLFields = Split(SQlFields, ",")<BR> <BR> HTMLFields = ""<BR> <BR> &#039;Loop through the array created above<BR> For Each sDbField In SplitSQLFields<BR> <BR> &#039;Loop through the Request.Form Collection each loop of the SQL Fields Array.<BR> &#039;This will find the correct form field corresponding to DB field so they line up<BR> For Each sField in Request.Form<BR> If Instr(sField, sDbField) Then<BR> HTMLFields = HTMLFields & sField & ","<BR> End If <BR> Next<BR> <BR> Next<BR> <BR> &#039;Trim the last comma off of the comma delimeted list of HTML form fields we just created above<BR> HTMLFields = Left( HTMLFields, Len(HTMLFields) -1 )<BR> <BR> &#039;Split the string into an array<BR> SplitHTMLFields = Split(HTMLFields, ",")<BR> <BR> &#039;Loop through the HTML Fields array<BR> For Each HTMLField In SplitHTMLFields<BR> <BR> &#039;Data is the HTML form name, and DataValue is the vale of the HTML form field<BR> Data = HTMLField<BR> DataValue = Request(HTMLField)<BR> <BR> &#039;If Data has the tag of .INT then the class will set its datatype to Integer<BR> &#039;and add to the SQL String with the correct syntax for the datatype<BR> If Instr(Data, ".INT") Then<BR> DataType = "Integer"<BR> SQLValues = SQLValues & CInt(DataValue) & ","<BR> End If<BR><BR> &#039;If DataType is a String<BR> If Instr(Data, ".STR") Then<BR> DataType = "String"<BR> SQLValues = SQLValues & "&#039;" & CStr(DataValue) & "&#039;," <BR> End If<BR><BR> &#039;If DataType is a Date<BR> If Instr(Data, ".DAT") Then<BR> DataType = "Date"<BR> SQLValues = SQLValues & "#" & CDate(DataValue) & "#" & ","<BR> End If<BR><BR> &#039;If DataType is of Bollearn value<BR> If Instr(Data, ".BOL") Then<BR> DataType = "Boolean"<BR> SQLValues = SQLValues & CBool(DataValue) & ","<BR> End If<BR> <BR> Next<BR><BR> End Function<BR> <BR> &#039;Varible SQLVales is now a comma delimeted list of the values of the HTML form<BR> &#039;the list has a trailling comma that needs to be trimmed off<BR> Public Property Get TrimSQL<BR> <BR> TrimSQL = Left(SQLValues, Len(SQLValues) - 1)<BR> <BR> End Property<BR> <BR> &#039;Combine the Default SQL and the trimmed SQL and the last Parentheses to<BR> &#039;get a working SQL String<BR> Public Property Get NewSQL<BR><BR> NewSQL = DefaultSQL & TrimSQL & ")"<BR> <BR> End Property<BR> <BR> &#039;Change NewSQL to SQL, so the property can be called with object.SQL instead<BR> &#039;object.NewSQL<BR> Public Property Get SQL<BR> SQL = NewSQL<BR> End Property<BR> <BR> &#039;Open up a connection to the user-defined DataSource and execute the SQL<BR> Function ExecuteSQL<BR> Dim oConn<BR> Set oConn = Server.CreateObject("ADODB.Connection")<BR> oConn.ConnectionString = "DSN=" & DataSource<BR> oConn.Open<BR> oConn.Execute(SQL) <BR> <BR> oConn.Close<BR> Set oConn = Nothing<BR> End Function<BR> <BR>End Class<BR><BR>Heres the code needed to add a record to a db<BR><BR>&#060;%@Language=VBScript %&#062;<BR>&#060;% Option Explicit %&#062;<BR><BR>&#060;!--#include file="clsAddtoDB2.asp"--&#062; <BR>&#060;%<BR> Dim oAddtoDB<BR> <BR> Set oAddtoDB = New addtoDatabase<BR> <BR> oAddtoDB.DataSource = "sabastio-sabasti_objects"<BR> oAddtoDB.SQLTable = "Info"<BR> oAddtoDB.SQLFields = "FirstName,MiddleInitial,LastName,Age,Weight,Heigh t"<BR> oAddtoDB.CreateSQL()<BR> oAddtoDB.ExecuteSQL()<BR> <BR> Set oAddtoDB = Nothing<BR> <BR>%&#062;<BR><BR>dont you think that is just much simpler?<BR><BR>:)

  2. #2
    Join Date
    Dec 1969

    Default Now to fix it...

    Okay, not really "fix", per se. But to make it more OO in nature.<BR><BR>First, make *ALL* the DIMs at the top of the class be PRIVATE instead of PUBLIC.<BR><BR>Then see if you can change the class so you can invoke it this way:<BR><BR>&#060;% <BR>Dim oAddtoDB <BR><BR>Set oAddtoDB = New addtoDatabase <BR><BR>oAddtoDB.DataSource = "sabastio-sabasti_objects" <BR>oAddtoDB.SQLTable = "Info" <BR>oAddtoDB.ExecuteWithFormFields("FirstName,Midd leInitial,LastName,Age,Weight,Height")<BR><BR>oAdd toDB.Close() &#039; You REALLY should have this!<BR><BR>Set oAddtoDB = Nothing <BR><BR>%&#062; <BR><BR>Or even as simply:<BR><BR>fields = Array("FirstName","MiddleInitial","LastName","Age" ,"Weight","Height")<BR><BR>oAddtoDB.DataSource = "sabastio-sabasti_objects" <BR>oAddtoDB.Execute("Info", fields)<BR><BR>[I&#039;m not a big fan of passing around arguments that are delimited strings. I&#039;d rather see the client be responsible for providing the array. Besides, it gives the client the choice of *how* to produce the array.]<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts