Print Page | Close Window

Run code from database

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: General Discussion
Forum Description: General discussion and chat on any topic.
URL: https://forums.webwiz.net/forum_posts.asp?TID=21118
Printed Date: 30 March 2026 at 1:18am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Run code from database
Posted By: theSCIENTIST
Subject: Run code from database
Date 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?


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com



Replies:
Posted By: theSCIENTIST
Date 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?


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dfrancis
Date 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


Posted By: theSCIENTIST
Date 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.


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: dfrancis
Date 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.


Posted By: michael
Date 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


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 23 August 2006 at 9:24pm
For some reason the code pastes ugly, hope you can decipher it,

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: Mart
Date 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


Posted By: theSCIENTIST
Date Posted: 24 August 2006 at 10:39am
Well, yeah if the database is compromised the code would be exposed, but that is also true is the web server is compromised and if I use files.
 
The idea is that of creating and editing the modules code for my CMS from the administration panel, the modules code would go on the database and no more headaches with FSO.
 
In ASP, theres no delay using the Execute(), in not sure if Execute() is also available in ASP.NET.


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: michael
Date Posted: 24 August 2006 at 3:23pm
Originally posted by Mart Mart wrote:

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


Well I would not do this myself either. But it does work and something similar can be used for snippets in a dev environment.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net