Reputation: 381
I have read many tutorial and examples, but still can't connect to the sql server.
I'm using window authentication and this simple code (stripped down to highlight the connection part)
SqlConnection myConnection = new SqlConnection(@"Data Source=localhost;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");
try
{
myConnection.Open();
"connected"
}
catch(SqlException ex)
{
"Send curriculum to mcdonald" + ex.Message
}
I've tried localhost\sqlexpress, computername\sqlexpress and a lot of other settings, the service are on, the database is there and reacheable from sql server management and so on. It's the first time that I try to use sql server so probably I'm forgetting something fundamental, what must I check to be sure to make it work?
Upvotes: 0
Views: 88
Reputation: 69504
SqlConnection myConnection = new SqlConnection("Data Source=localhost\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");
OR
SqlConnection myConnection = new SqlConnection("Data Source=.\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");
OR
SqlConnection myConnection = new SqlConnection("Data Source=ServerName\\InstanceName;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");
After reading you comments try this..
SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=MyDatabase;Trusted_Connection=True;connection timeout=5");
Upvotes: 1