I have installed SQL Server 2008 Express, and I'm trying to access it from C#.
I'm not so sure I set it up right in SQL Server Management Studio. I have a password, and a .sdf file. Nothing is in the database at the moment. Here's my snippe开发者_StackOverflow中文版t of code in C#
SqlConnection userSqlData = new SqlConnection("Data Source = localhost;database=UserData;Password=Mine;connection timeout = 10");
try
{
userSqlData.Open();
}
catch
{
MessageBox.Show("Failed");
}
The connection always times out. Obviously I've set something up wrong here. How do I get it to connect to database on local computer?
Try connecting to SQL Server express ;) You connect to the default instance. Express is a named instance.
http://www.connectionstrings.com/sql-server-2008
has all kinfds of examples, including for express databases.
The default connection string for SQL Server looks like this:
Data Source=myServerAddress\SQLEXPRESS;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
You need the named instance in Data Source
. You also need to specify the User Id
of the user you're authenticating as, not just the password.
Here's a good resource for additional options:
http://www.connectionstrings.com/sql-server-2008
精彩评论