I am playing with C# and a local database (An empty SQL Server Compact Edition database for local data)
You are using a Sql Server Compact
file, not a Sql Server Local DB
To deal with Sql Server Compact, you need to use the System.Data.SqlServerCe
namespace, not System.Data.SqlServer
.
Replace SqlConnection
, SqlCommand
, ...
With SqlceConnection
, SqlCeCommand
, ...
And Stored procedures are not supported in Sql Server Compact (How to use Stored Procedure in SqlCE), so sqlCeCommand.CommandType
can not be CommandType.StoredProcedure
.
You need to use commandType.Text with command parameters.
using(SqlCeConnection sqlCeConnection = new SqlCeConnection(Properties.Settings.Default.DatabaseConnectionString)) {
using(SqlCeCommand sqlCeCommand = new SqlCeCommand("SELECT * FROM users WHERE id = @id", sqlCeConnection)) {
sqlCeCommand.CommandType = CommandType.Text;
sqlCeCommand.Parameters.AddWithValue("@id", 1);
try {
sqlCeConnection.Open();
SqlCeDataReader reader = sqlCeCommand.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 {
sqlCeConnection.Close();
}
}
}