2

I know this requirement may seem weired to many of you but it is one of my project requirement. Is it possible to add code in Sp and execute in .Net exe. Like on button click i call one SP that SP returns few line of code and then program execute the code.

Is it possible if yes then how?

I am using framework 1.1

Shaharyar
  • 12,254
  • 4
  • 46
  • 66
Deepesh
  • 5,346
  • 6
  • 30
  • 45
  • 7
    Firstly, .NET Framework 1.1, what the deuce, please consider a more uptodate version. Secondly, attempting to run dynamic evaluations from code pulled from a Database Stored Procedure sounds like an odd way to be going about your business. Care to explain why you're attempting this? – Jeff Watkins May 31 '12 at 10:05
  • 4
    try here - possibly related to http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – Dave Lawrence May 31 '12 at 10:06
  • @Jeff The system is a legacy system and client is not willing to upgrade it to new frameworks, he has his own reasons for it. Secondly its an Client Server application. If we get any bug in client app we need to redistribute it to more then 1500 locations so if we are able to execute code at runtime then it can come to rescue sometime – Deepesh May 31 '12 at 10:09

4 Answers4

3

Searched for 'dynamic c# code execution' and Google came up with this blog from 2002... This might work for you..

http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm

The article refers to System.CodeDom.Compiler to compile a assembly on the fly and then using Reflection to execute that code. Sounds like fun ;)

EDIT: As DaveL suggested: Is it possible to dynamically compile and execute C# code fragments?

Community
  • 1
  • 1
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
1

If you are talking about compiling TSQL into a table and then running this TSQL later via C#, yes it is possible. What you will need to do is write a simple parser for TSQL script files (.sql). The parser will read through the SQL script and at each GO statement, add the TSQL statement into the relevant SQL Server table (The table might have two columns 1. Database to run against/name of process to run against. 2. The query itself.). You can then execute these TSQL blocks at dynamically at runtime.

If you are talking about running C# that was compiled into an SQL Server data table, this is a different ball game and I am not aware of an easy way to do this (it it is possible at all).

I hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • I am talking about C#. Like i have a method and the end of that method i want to add some extra code which depends on runtime. This code will be saved in database and then later on we execute it. – Deepesh May 31 '12 at 10:14
  • Ok, the convoluted route. You can create a plugin architecture with assemblies following a specific pattern and published interface. Load the assemblies dynamically from the database as binary objects and execute them according to the interface you've defined. It ain't pretty but it would work. – Jeff Watkins May 31 '12 at 10:22
1

Another possibility:

By using some binary data type like varbinary in your database, you could store Assemblies in a table as binary data. You could then read that binary data from the Database, store it locally, load the Assembly via Assembly.LoadFrom(), and then use reflection to find some special type or entry point which you could use.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

You're probably wanting the DLR (where the D stands for Dynamic), unfortunately this is .NET 4.0 upwards.

Sounds like you're in an awful legacy code netherworld; rather you than me.

You could create your own scripting language to run within your app but it would be slow and not ideal. Something like YACC would get you part the way there but it's a bumpy ride (having seen somebody attempt this kind of behaviour in Java).

Jeff Watkins
  • 6,343
  • 16
  • 19