Question

Extended Stored Procedure in C#

Asked by: francoiswirth

Hi,

I'm looking for an example of how to write an extended stored procedure in C# for SQL 2000.

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-09-01 at 13:30:27ID21548274
Tags

stored

,

extended

,

procedure

Topic

C# Programming Language

Participating Experts
7
Points
500
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Extended stored procedure
    Hi, I've written a small extended stored procedure using the wizard included in VC++ 6.0. After registering it with the SQL Server I figured out, that I cannot directly issue a select query using the esp. My esp requires two parameters and returns a list of two columns and ...
  2. Extended stored procedures
    Does somebody know if it is possible to write an extended stored procedure in Visual Basic, as an ActiveX DLL? If so, where can in found some information about it on the net? Thanks Gunther
  3. Parameters on Extended Stored Procedure
    1.- Do the Extended Stored Procedures admit more than one parameter (SVRPROC)? Meaning that the prototype may be other than SRVRETCODE xp_ExtProcName (SRVPROC *) I'd need to pass an integer value SRVRETCODE xp_ExtProcName (SRVPROC *; MyValue : Integer) 2.- If m...
  4. Creating an Extended Stored Procedure
    Can anyone confirm if you can actually create a DLL which can be used as an Extended Stored Procedure - and if so let me know where I am going wrong please. I have created my app which works fine as a EXE but when I add it to SQL and try execute the Extended SPROC I get ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: funkyfingerPosted on 2005-09-01 at 14:20:08ID: 14806042

I don't understand what you mean by extended. I might not know what you are talking about. However I can show you how to write a stored procedure and add it to a database, is that what you want?

 

by: BlackTigerXPosted on 2005-09-01 at 16:25:32ID: 14806629

Extended stored procedures are created and loaded using DLL's

 

by: sohamrajPosted on 2005-09-01 at 21:22:30ID: 14807652

Hi,

Please explain, what you want?

 

by: francoiswirthPosted on 2005-09-01 at 22:06:49ID: 14807749

Extended Stored Procedures is written as DLL's and placed inside the BINN directory of SQL Server. You can than register the Extended Stored Procedure with SQL Server and call it like any other SP. This way you extend SQL Server to send a message to MSMQ for example.

Here is the spec from Microsoft:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odssql/ods_6_ref_02_8k2t.asp

An example in C++
http://www.codeproject.com/database/extended_sp.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odssql/ods_6_ref_02_8k2t.asp

I would like to write one in C# but would like to have an example to work from.

I know that one issue around C# is that it cannot export DLL methods, but MSIL can, here is an artical on that:
http://www.c-sharpcorner.com/Code/2003/Aug/ExportManagedCodeasUnmanaged.asp.
I don't mind editing the MSIL each time. But I need a Guru on Interop to help me to get this working somehow...

So basically, I think it must be technically possible to do this.

Can anyone help PLEASE

 

by: fruhjPosted on 2005-09-01 at 22:34:23ID: 14807831

I'm not sure you can do what you want to in SQL server 2000
SQL server 2005 supports stored Procedures in C# and in VB.net as well as TSQL.

They covered all this stuff at a recent one day developers conference I attended.

At first I was really excited, as I pictured writing these in something like the enterprise manager I was used to with 2000, however after getting down and dirty with the MS guys, it appears that what you are asking to do with SQL2000, is exactly what and how they did it with SQL2005 Stored Procedures written in C# (or VB) are still run in the "managed code" runtime environment so they are not as fast as native TSQL, and in some cases they might not be portable, as the C#/VB.NET Stored Procedure code appeared to have some additional installation requirements at the time I saw it (that could change, I saw the beta/CTP version)

My advice would be to go on MS's website and order  a free Community Technology Preview version of SQL 2005 - and play with it, then decide how to proceed.


- Jack

 

by: francoiswirthPosted on 2005-09-01 at 22:46:46ID: 14807871

Thanks for the answer,

Yes it will be much easier in SQL 2005, the problem is that SQL 2000 will be still around for a very long time. The clients I work for need this for SQL 2000 and they will probably only move to SQL 2005 in middle 2007. (or after SP2 as they all say)

I know this is extemely difficult, but I don't think impossible. If I could give 50 000 points for an example I would.

If it is impossible, can someone please tell me for a learning experience why it cannot be done.



 

by: TheAvengerPosted on 2005-09-02 at 00:08:54ID: 14808138

It is not possible to call C# code from SQL Server 2000 directly. You can call only C/C++ libraries, just like from a VB 6 application.

There are however some workarounds.

- Create a class library with C#. Create a type library as a wrapper around the class library and register it as a COM object. Create a C/C++ library project with VS 6 to instanciate the COM object and call this C/C++ library from SQL Server. An example for creating a COM object and its type library with C# can be found around the net, e.g. under http://www.csharphelp.com/archives/archive281.html. This solution is however slow because of the COM objects, whose model is heavy.

- Create a class library with C#. Create a mixed managed/unmanaged library in C++ .Net. This library can expose functions as a normal Win32 DLL and can call .Net class libraries, so it can access also your C# library. The normal Win32 DLL part can be used from SQL Server 2000. So you would have something like: SQL Server <-> Bridge library C++ .Net <-> C# library. Some examples are availabe on the Web as well.

I think there are some other options as well. You can google for something like "calling managed code from unmanaged code" and see different solutions. I personally used the second option I described and it works well for me but I had to do some programming in C++/C++ .Net

 

by: fruhjPosted on 2005-09-02 at 05:38:44ID: 14809526

One thing that I didn't realize in the early days of playing with .net is that is very similar to Java in how it is implimented.  Java apps aren't native win32 code, they can't run on a PC without the java interpreter.  This was done on purpose - the creators of Java felt if they could use the interpreter on windows to run the java code, then they could also create interpreters for other systems, and indeed they have - java code can be run on many different platforms.

Microsoft choose to go the same route with .net. They use the term "Managed Code" - but it means the same thing, you're not really running a windows application, your're running a .net application completely inside of it's "managed environment"  Thats why you see these articles like the one Avenger had you search on (Calling managed code from unmanaged code)  Thats also why this is so difficult and why you're having a hard time finding information about it.

I can't really say why microsoft took this route, as I can't imagine that they plan to port the "managed environment" to other platforms as sun did with Java.

I still feel strongly that you should get a hold of SQL 2005 CTP and try the C# stored procedures - from what I remember it wasn't that impressive, there were limitations.  I came away feeling that the C# support was more of a marketing ploy than a usable feature. It made me want to dig in deeper with Transact-SQL.

 

by: TheAvengerPosted on 2005-09-02 at 05:49:17ID: 14809584

@fruhj: Just some small remarks:

- Managed code is not called like this just to avoid calling it Win32 code. It is really "managed": you don't care about memory allocation or cleaning, it manages your arrays, etc. The name of code, produced when you compile the application is IL (intermediate language). Of course, the story with Java is the same with the exception that .Net has a much better support for Windows systems

- Microsoft indeed consider porting .Net to other platforms. There are also at least several open projects for implementing .Net on Linux, one of which I think already supports ASP.NET very well.

- A connection between C# and Win32 DLLs is still much easier than Java - Win32 DLLs, especially the part of using Win32 code from C#

 

by: BlackTigerXPosted on 2005-09-02 at 07:08:25ID: 14810216

check this article too
http://www32.brinkster.com/srisamp/sqlArticles/article_33.htm

seems you don't need the C++ wrapper

 

by: acperkinsPosted on 2005-09-02 at 07:30:29ID: 14810441

I think we are getting a little off-track here.  The questioner has been very clear what they want (to refresh your memories see here: http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21548274.html#14807749) but we seem to be ignoring his/her comments.  

Here is what they do not want:
A way to access .NET or COM components.  

For a very good example of what they do want to achieve, see here:
http://www.sqldev.net/xp/xpsmtp.htm

If you are not familiar with XPSMTP.DLL it is a DLL to send SMTP emails.  No, I realize it is not written in C#, that is not the point.  What it is is a way to access functionality from SQL Server as an extended stored procedure as if it was native to SQL Server.

 

by: arbertPosted on 2005-09-05 at 14:39:41ID: 14825510

"with SQL2005 Stored Procedures written in C# (or VB) are still run in the "managed code" runtime environment so they are not as fast as native TSQL, "

Hmmm, evidently you haven't worked with this--very FAR from the truth....

Like BlackTigerX's link above pointed out, you either use SP_OACreate, or use a wrapper.  Other than that, there really isn't a way in 2000 to handle it.  There is a pretty good (old) thread on usenet about it:

http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_frm/thread/681cc4cc40d51bd3/e814083dd99efe2d?lnk=st&q=%22sql+server+2000%22+c%23+extended+procedure&rnum=1&hl=en#e814083dd99efe2d

Brett

 

by: fruhjPosted on 2005-09-05 at 18:20:44ID: 14826167

Hey Brett's worked with this stuff infinitely more than I have (and probably more than most people here) so if he says it can be done and theres' no performance hit for doing so then It looks like youre on the right track - sorry if I led you astray.
- Jack

 

by: arbertPosted on 2005-09-05 at 20:36:47ID: 14826538

I wouldn't say that.  I've worked with it enough to be awed by the possibilities with the speed compared to TSQL....On the flipside, it's going to present a nightmare for DBAs and who gets access to code stuff at the database level--interesting new frontier......

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...