| Author |
Topic Search Topic Options
|
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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
|
|
|
 |
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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?
|
|
|
 |
dfrancis
Senior Member
Joined: 16 March 2005
Location: United States
Status: Offline
Points: 442
|
Post Options
Thanks(0)
Quote Reply
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
|
 |
theSCIENTIST
Senior Member
Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
|
Post Options
Thanks(0)
Quote Reply
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.
|
|
|
 |
dfrancis
Senior Member
Joined: 16 March 2005
Location: United States
Status: Offline
Points: 442
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
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
|
|
|
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 August 2006 at 9:24pm |
|
For some reason the code pastes ugly, hope you can decipher it,
|
|
|
 |
Mart
Senior Member
Joined: 30 November 2002
Status: Offline
Points: 2304
|
Post Options
Thanks(0)
Quote Reply
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
|
 |