我有这段代码:
public void FillData()
{
// 1 step. Open connection
string conection = @"Data Source=|DataDirectory|db.sdf;Persist Security Info=False;";
try
{
SqlCeConnection c = new SqlCeConnection(conection);
c.Open();
// 2 step. Create new DataAdapter
using (SqlCeDataAdapter a = new SqlCeDataAdapter("SELECT * FROM USER", c))
{
// 3 step. Use DataAdapter to fill table
DataTable t = new DataTable();
a.Fill(t);
// 4 step. Render data on the DataGridView
dataGridViewUsers.DataSource = t;
}
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
}
}
我可以连接到数据库,但SqlCeDataAdapter
启动了这个错误:
There was an error parsing the query. [Token line number = 1, Token line offset = 15, Token in error = USER]
我的数据库有一个名为USER
的表,所以这段代码有什么问题?
答案 0 :(得分:2)
USER是保留字。在查询中将其作为[USER]
传递。您的查询应该是这样的。
new SqlCeDataAdapter("SELECT * FROM [USER]", c))