I'm querying a SQL Server Compact database with "SELECT * FROM User", and I get an error parsing the query:
Token line number = 1,Token line offset = 15,Token in error = User
How do I fix this?
The code I'm using is t开发者_开发问答his:
public static List<User> GetUsers()
{
List<User> users = new List<User>();
using (SqlCeConnection con = new SqlCeConnection(Properties.Settings.Default.DatabaseConnection))
{
con.Open();
using (SqlCeCommand command = new SqlCeCommand("SELECT * FROM " + TABLE, con))
{
SqlCeDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string user = reader.GetString(1);
User usr = null;
using (MemoryStream s = new MemoryStream())
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
s.Write(ASCIIEncoding.ASCII.GetBytes(user.ToCharArray()), 0, user.Length);
s.Position = 0;
usr = (User)serializer.Deserialize(s);
}
}
}
}
return users;
}
Note: I also get this error trying to add information.
It looks like the token 'User' is not being understood. Just a guess (I don't have a SqlCE to test on) try "quoting" the table name with square brackets:
"SELECT * FROM [User]"
精彩评论