Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Run code from database
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Run code from database

 Post Reply Post Reply Page  12>
Author
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Topic: Run code from database
    Posted: 23 August 2006 at 5:24pm
This question is for ASP and ASP.NET so I decided to insert it here in General since it may cover both.
 
It just occured to me that would be beneficial, if I could run code stored in a database and not in files, what I mean is this; say I have the following lines stored in a database:
 

Dim a
Dim b
Dim c
a = 1
b = 2
c = a + b
Response.Write(c)
 
I would then get it from the DB and assign it to a variable, but how do I run this code without creating a file? What I mean is if there was something like; Server.RunThis(someVar)
 
I need to know how to do this in ASP and also in ASP.NET (VB please) anyone has ideas?


Edited by theSCIENTIST - 23 August 2006 at 6:01pm
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 5:58pm
I came up with this for ASP 3.0:
 

<%
Dim dbData
dbData = "Dim a" & vbCr &_
         "Dim b" & vbCr &_
         "Dim c" & vbCr &_
         "a = 1" & vbCr &_
         "b = 2" & vbCr &_
         "c = a + b" & vbCr &_
         "Response.Write(c)" & vbCr &_
Response.Write("This is it:<br /><br />")
Execute(dbData)
%>
 
Didn't know about the Execute, quite handy, all I need now is a function to assign all lines of code to a variable adding a sufix of " & vbCr &_ " per line so it can be executed.
 
I still need the ASP.NET version of this.
 
Anyone has other ways?
Back to Top
dfrancis View Drop Down
Senior Member
Senior Member


Joined: 16 March 2005
Location: United States
Status: Offline
Points: 442
Post Options Post Options   Thanks (0) Thanks(0)   Quote dfrancis Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 6:38pm
I'm confused.
 
The c variable will be executed just like you wrote it.
 
<%
Dim a,b,c
 
a = 1
b = 2
c = a+b
 
Response.Write(c)
%>
 
result
 
3


Edited by dfrancis - 23 August 2006 at 6:38pm
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 7:25pm
Yes, you probably didn't understand why I need this.
 
I want to have useful and easy to access blocks of code in a database, that I can call and use at any time without having .asp files, so I'll be calling this code from a database making it easy to update the code without having the usual issues with FSO when dealing with files.
Back to Top
dfrancis View Drop Down
Senior Member
Senior Member


Joined: 16 March 2005
Location: United States
Status: Offline
Points: 442
Post Options Post Options   Thanks (0) Thanks(0)   Quote dfrancis Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 7:42pm
you're right... I didn't understand.
 
You're talking about DTS (Data transformation service) or a query string?
 
I think your question surpasses my ability to answer... in other words... I have no idea. Sorry.
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 9:23pm
Now it's sure more code but here is an example for .net written in vb.net to dynamically execute c# code.
Essentially you have a textbox and a button, it will compile it and run it once you hit the button. Works quite well. You can easily change below to load from a database and execute whenever.


Imports System.Reflection
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp

    
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' *** Example form input has code in a text box
        Dim lcCode As String = Me.txtCode.Text
        Dim loCompiler As ICodeCompiler = (New CSharpCodeProvider + CreateCompiler)
        Dim loParameters As CompilerParameters = New CompilerParameters
        ' *** Start by adding any referenced assemblies
        loParameters.ReferencedAssemblies.Add("System.dll")
        loParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        ' *** Must create a fully functional assembly as a string
        lcCode = ("using System;"& vbCrLf& vbCrLf&"using System.IO;"& vbCrLf& vbCrLf&"using System.Windows.Forms;"& vbCrLf& vbCrLf&" "& vbCrLf& vbCrLf&"namespace MyNamespace {"& vbCrLf& vbCrLf&"p"& _
        "ublic class MyClass {"& vbCrLf& vbCrLf&" "& vbCrLf& vbCrLf&"public object DynamicCode(params object[] Parameters) {"& vbCrLf& vbCrLf _
                    + (lcCode + "}   }    }"))
        ' *** Load the resulting assembly into memory
        loParameters.GenerateInMemory = false
        ' *** Now compile the whole thing
        Dim loCompiled As CompilerResults = loCompiler.CompileAssemblyFromSource(loParameters, lcCode)
        If loCompiled.Errors.HasErrors Then
            Dim lcErrorMsg As String = ""
            lcErrorMsg = (loCompiled.Errors.Count.ToString + " Errors:")
            Dim x As Integer = 0
            Do While (x < loCompiled.Errors.Count)
               lcErrorMsg = (lcErrorMsg + (""& vbCrLf&"Line: " _
                            + (loCompiled.Errors(x).Line.ToString + (" - " + loCompiled.Errors(x).ErrorText))))
               x = (x + 1)
            Loop
            MessageBox.Show((lcErrorMsg + (""& vbCrLf& vbCrLf + lcCode)), "Compiler Demo")
            Return
        End If
        Dim loAssembly As Assembly = loCompiled.CompiledAssembly
        ' *** Retrieve an obj ref  generic type only
        Dim loObject As Object = loAssembly.CreateInstance("MyNamespace.MyClass")
        If (loObject = Nothing) Then
            MessageBox.Show("Couldn't load class.")
            Return
        End If
        Dim loCodeParms() As Object = New Object((1) - 1) {}
        loCodeParms(0) = "West Wind Technologies"
        Try
            Dim loResult As Object = loObject.GetType.InvokeMember("DynamicCode", BindingFlags.InvokeMethod, Nothing, loObject, loCodeParms)
            Dim ltNow As DateTime = CType(loResult,DateTime)
            MessageBox.Show(("Method Call Result:"& vbCrLf& vbCrLf + loResult.ToString), "Compiler Demo")
        Catch loError As Exception
            MessageBox.Show(loError.Message, "Compiler Demo")
        End Try
    End Sub
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 23 August 2006 at 9:24pm
For some reason the code pastes ugly, hope you can decipher it,
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 24 August 2006 at 10:14am
i wouldn't really bother trying to do this, imagine if you're database was compromised. Also, in .net using the CodeDom API is very slow, your page will hang for around a couple of seconds whilst the database code executes
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.