Help! I'm unable to connect to my SQL Server 2005 database from a C#
class.
This error message happens on the conn.Open() statement in my code:
Request for the permission of type
'System.Data.SqlClient.SqlClientPermission, System.Data,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
failed.
I have VS 2005 and Sql Server 2005 installed on my dev workstation.
I'm running the code in the debugger and I get the above error. I have
tried a variety of connect strings - many more than listed here but
none worked so far. All works fine from SQL Server 2005 Management
Studio. I created a Integrated Services package, compiled and it runs
without a hitch. VS can connect to the database fine.
TCPIP was enabled and I also enabled Named Pipes - no change.
Can anybody help me understand what I need to do to be able to connect
from code?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
<snip>
cs = "Data Source=(local);Initial
Catalog=ScoreCard;Integrated Security=True";
cs = "Data Source=(local);Initial
Catalog=ScoreCard;Trusted_Connection=True";
cs = "Data Source=tcp:davel-vpc;Initial
Catalog=scorecard;User Id=someuser;Password=password;";
cs = "Data Source=myserver;Initial
Catalog=scorecard;User Id=someuser;Password=password";
<snip>
DataTable dt = new DataTable();
using ( SqlConnection conn = new SqlConnection(cs) )
Catalog=ScoreCard;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand("GetCities",conn))
{
cmd.CommandText = "SELECT * FROM city";
conn.Open();
<snip>
conn.Close();
}
Targus.
ds_lewis@hotmail.com - 31 Dec 2005 21:00 GMT
Here is some additional information for the problem.
I get the same error if I try to connect to a SQL Server 2K database on
a second machine.
But I have VS 2003 installed on that second machine and I can connect
to the SQL Server 2005 database on the first machine without any
errors.
So I think it has to be a VS 2005 issue.
Again, Please help!
Targus