PC Review


Reply
Thread Tools Rate Thread

How to call SQL Server function?

 
 
Antonio Maciel
Guest
Posts: n/a
 
      30th Jul 2003
Hi.

I wrote a sql server user defined function that I need to call from vb.net .
This function returns a string that I need to store in a variable. How can I
call this function from my application?

Thanks,

Antonio Maciel


 
Reply With Quote
 
 
 
 
Anatoly
Guest
Posts: n/a
 
      30th Jul 2003
sqlCommand cmd = new sqlCommand("select dbo.myFunc()", myConnection);
myConnection.Open();
return cmd.ExecuteScalar();
myConnection.Close();

"Antonio Maciel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi.
>
> I wrote a sql server user defined function that I need to call from vb.net

..
> This function returns a string that I need to store in a variable. How can

I
> call this function from my application?
>
> Thanks,
>
> Antonio Maciel
>
>



 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      30th Jul 2003
Is that a stored procedure?

You can call stored procedures by setting the CommandType to
"StoredProcedure". The default is "Text", which is what you use for the
standard SQL Select clauses. Then set the CommandText property to the name
of the stored procedure. In this instance you need to create SQLParameter
objects for each of the stored procedure's parameters (if there are any) and
add them the the commands "Parameters" collection.

myConnection.Open();
try
{
SqlCommand cmd = new SqlCommand("myFunc", myConnection);
//^ SqlCommand(string commandText, SqlConnection connection)
cmd.CommandType = CommandType.StoredProcedure;
// you showed no parameters...
return cmd.ExecuteScalar();
}
catch{...handle error...}
finally
{
myConnection.Close();
}

note, your code below would not compile. you would get "code unreachable"
error pointing to your line after the return statement. The above would
work.

To see how to add parameters see the help (watch word wrap):
Visual Studio:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconusingstored
procedureswithcommand.htm

msdn website:
http://msdn.microsoft.com/library/de...ithcommand.asp

"Anatoly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> sqlCommand cmd = new sqlCommand("select dbo.myFunc()", myConnection);
> myConnection.Open();
> return cmd.ExecuteScalar();
> myConnection.Close();
>
> "Antonio Maciel" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi.
> >
> > I wrote a sql server user defined function that I need to call from

vb.net
> .
> > This function returns a string that I need to store in a variable. How

can
> I
> > call this function from my application?
> >
> > Thanks,
> >
> > Antonio Maciel
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can i call an UDF (user defined function ) on an MSDE2000 server from code ? Sagaert Johan Microsoft C# .NET 1 14th May 2006 10:43 PM
call server side function then javascript Lostdante via DotNetMonster.com Microsoft ASP .NET 5 18th Jan 2006 02:24 PM
Call a server side function from JavaScript!!! Stephen Microsoft ASP .NET 3 4th Oct 2004 08:55 PM
How to call a SQL function in SQL server from C#? Andy Li Microsoft C# .NET 2 11th Dec 2003 03:58 PM
Call javascript function from server Alvaro Gutierrez Microsoft ASP .NET 1 23rd Aug 2003 09:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:08 PM.