Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server - execute scalar function without specifing db name

Tags:

sql

sql-server

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

like image 689
Ghita Avatar asked Nov 04 '22 10:11

Ghita


1 Answers

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.

like image 191
Yuck Avatar answered Nov 09 '22 16:11

Yuck