Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I store VB Code in a Database field and then run it in VB.NET

Status
Not open for further replies.

mjk9564

Programmer
Apr 23, 2003
64
US
What I want to do is store vb code in a database field and depending on what the user selects from a combobox run the code. They call this Macro Substitution in Visual FoxPro.

Code:
Example

dim strCommand as String = database.fieldname.tostring()

&strCommand  'This would be visual foxpro way

Any help would be appreciated!

Thanks,
Matt
 
Yep it is possibel, so keep looking and don't give up. I just don't exactly know how.

Christiaan Baes
Belgium

"My new site" - Me
 
I would guess that Chiph or Jebenson would know, I'm pretty sure I have seen one other the other post code that was related to this.

On the other hand, what code are you trying to run from the database? There may be a simpler, more secure, and more efficient way to handle what you are trying to do.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Right now all I need to be able to run is simple code. What I have is a print procedure at it allows the user to choose their header and footer. They can also choose from a couple built-in solutions like dates, page count, page number, etc. Basically, I would need to run code like Format(DateTime.Now, "d"). I hope that helps. I'm using VB.NET 2005 by the way.

Matt
 
Surprisingly, this is not a trivial task! :)

It seems that you can do this using the Microsoft.VisualBasic.VBCodeProvider class. This class enables you to take the code you want executed, compile it in memory, instantiate an object from the in-memory class and go from there.

After searching I found a couple of promising articles on the web:

[URL unfurl="true"]http://www.codeproject.com/dotnet/evaluator.asp[/url]

[URL unfurl="true"]http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm[/url]

Unfortunately, these samples are in C#, although the first one does have a sample program in VB that uses the .dll generated by the C# project. I think you may be able to take the .dll from that project and reference it in yours without having to write your own solution. The VB sample demonstrates how to use the .dll's methods.

Let us know how/if this works out.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Here's 3 methods:

1.
We something like this in our current app, only it's VBScript, and not VB.NET. One of our guys found a C#-based VBScript interpreter, which you could translate into VB.NET, or left as C# and put in it's own assembly (probably the best solution). Do some googling to find it.

2.
If you want to load compiled (binary) VB.NET code from your database, look at the Assembly.Load() overloaded method, one of which accepts a byte array. This array of bytes should be the binary image of an assembly.

3.
If you want to run raw VB.NET code, you'd do something like this (sorry for any C#-isms):
[tt]
Dim myCodeProvider as new VBCodeProvider()

Dim myCompiler as ICodeCompiler
myCompiler = myCodeProvider.CreateCompiler()

Dim cp as New CompilerParameters()
cp.GenerateInMemory = true
cp.IncludeDebugInformation = true
cp.MainClass = "foo"
cp.OutputAssembly = "customassemblyfromdb"
cp.ReferencedAssemblies.Add("System.dll")
cp.WarningLevel = 4
cp.TreatWarningsAsErrors = true

Dim cr as CompilerResults
cr = myCompiler.CompileAssemblyFromSource(cp, "the string containing VB.NET statements goes here")

if (cr.Errors.Count > 0) Then
' Something didn't compile. Uh-Oh.
else
Dim a as Assembly
a = cr.CompiledAssembly

Dim mi as MethodInfo
mi = a.EntryPoint

Object[] parms = new [Object]() {23, "hello" }
Object o = mi.Invoke(null, parms)
end if
[/tt]

Note that you'll want to define a common interface to be used by your stored code so that you're (somewhat) assured that the method signature on your entry point will match up to the code you're calling it with.

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks I will try these methods out and see what works. I'll let you know what I find out.
 
AFAIK you should be able to use the old COM ScriptControl 1.0 for simple tasks like this. This supports VBScript and JScript so you'll have to write your script in the appropriate dialect. It can certainly be added as a reference to a .NET app and exposes all the required events, methods and properties




Bob Boffin
 
Yeah, they went a little further than I did.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top