I am playing with C# and a local database (An empty SQL Server Compact Edition database for local data
)
But I am unable to connect to the database and get data.
This is what I try:
// Properties.Settings.Default.DatabaseConnectionString = Data Source=|DataDirectory|\Database.sdf
// I guess Visual Studio put it there after I created my database...
using(SqlConnection sqlConnection = new SqlConnection(Properties.Settings.Default.DatabaseConnectionString)) {
using(SqlCommand sqlCommand = new SqlCommand("SELECT * FROM users WHERE id = @id", sqlConnection)) {
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@id", 1);
try {
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader(CommandBehavior.SingleRow);
if(reader.Read()) {
System.Console.WriteLine(reader);
System.Console.WriteLine(reader["id"]);
System.Console.WriteLine(reader["name"]);
}
}
catch(Exception e) {
System.Console.WriteLine(e.GetBaseException());
}
finally {
sqlConnection.Close();
}
}
}
My whole programm hangs for a while, after the pause I get this message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)