5

I have a user defined SQL function that I am able to call from management studio using syntax dbo.Function(arg)

Now, when I have to call this function from C# if I don't specify **dbname**.dbo.Function(arg) I get an error that SQL server does not find this user defined function. How can I solve this without specifing dbname ? I already connect to the server using a connection string that specifies the "initial catalog = dbname"


It seems that I cannot reproduce mentioned behavior at this point :-) (either using SQL server 2005 or 2008) I have to put this question on hold

5
  • 1
    Could you show C# code and exact exception? Commented Nov 18, 2011 at 14:55
  • 2
    You may need to grant EXEC permissions on the function to the login used by your application. Commented Nov 18, 2011 at 14:57
  • @MartinSmith I use Windows Authetication in both management studio (where it works) and C# code. As I've said if I add in code also dbname ( e.g. SELECT DATEPART(YEAR, Activitylogs.Datastamp) AS YEAR_VALUE, dbname.dbo.ISOweek(Activitylogs.Datastamp) ... it works Commented Nov 18, 2011 at 15:07
  • 1
    Using a SqlCommand with command text select dbo.Function() works for me. Commented Nov 18, 2011 at 15:20
  • It is possible that I saw this only intermittent. I will have to put the question on hold because it seems to work now. Thank you all ! Commented Nov 18, 2011 at 15:34

1 Answer 1

2

Your connection string needs to specify the database to use initially. It might look something like this:

var cn = new SqlConnection(
    "SERVER=SomeServer;DATABASE=SomeDb;Integrated Security=SSPI;"
);

Without that, you're probably being dumped into the master database, which is why you need to fully qualify the function name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.