Sunday, February 19, 2012

C# App Sql Server Express Connection Problem

I have a baffling connection problem from my C# console app to a sql
server express database.

The console application opens the sql database using the following
code:-


vDataSource = "server = " + vgSqlServer + "; " +
"database = " + vgSqlServerDb + ";" +
"uid = XXX; pwd = XXX; ";


//create the connection
vSqlConnection = new SqlConnection(vDataSource);


try
{
vSqlConnection.Open();
}
catch (Exception vSEx)
{
//connection failed
Console.WriteLine("Failed");
Console.Write("Connection Failed.\n\nError Number: " +
"\n\n" + vSEx.Message,
"Database Connection Failed");
}

The exception message is:
Request for the permission of type
'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.


I have placed the application on a network drive ( just copying it not
using publisher) and from my PC the application works fine. However
when I try to run the application from any other machine on the same
network the connection fails with an . On looking at this
message the problem appear to be because the sqlclient code cannot get permission to the database.


Both machines are using the same exe and have similar set-ups.
Other Windows applications written in C# and accessing the same database
work perfectly on both machines.


I have tried connection using both SQl Server Login id's and windows
authentication with no effect

If I publish the application to the network it appears to work perfectly!! As the application is to be called by other apps I would really like to just call the exe not the full published version


My machine has both SqlServer express manager and VS so my question
is - does anybody know of a reason why a console application will not
work on a PC where a windows application works correctly?

Sorry this is so long but I have tried to include all the details -
please ask if you need any further info.


Thanks you for your time....


DC
The Wine Society


First of all, thank you for the details, many posters just send in: I have an error message during the establishment of a connection" which is in most cases not detailed enough (*cough*cough*) to help them.

Starting your application you are running in a CAS policy problem, whereas the execution of certain libraries is NOT allowed from network drives (therefore it works when starting them locally)

See this here for more information:

http://www.devx.com/vb2themax/Article/19886

BTW: You should try to use the SQLConenctionBuilder class instead of putting your string manually together. (it included in the System.Data.SqlClient namespace)

Jens K. Suessmeyer

http://www.sqlserver2005.de

No comments:

Post a Comment