我正在使用sql进行Csharp。但是我收到了错误
解析查询时出错。 [令牌行号= 1,令牌行偏移= 40,令牌错误=第一]
private void CustomerAdded(Customer c)
{
string query = string.Empty;
int rowsAffected = 0;
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
query = string.Format("INSERT INTO CustomerRecord(CustomerId, First Name, Surname, PhoneNumber, City,Country, Account Manager) VALUES ({0},'{1}','{2}','{3}','{4}','{5}','{6}')",c.CustomerId,c.FirstName,c.Surname,c.PhoneNumber,c.City,c.Country,c.AccountManager);
SqlCeCommand command = new SqlCeCommand(query, connection);
rowsAffected = command.ExecuteNonQuery();(this is were the error say line 162)
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.ToString());
}
finally
{
connection.Close();
}
GetCustomers();
bs.ResetBindings(false);
答案 0 :(得分:1)
如果您的列名中有空格,则需要使用方括号。使用[First Name]
代替First Name
和[Account Manager]
而不是Account Manager
。如果要为所有列插入值,则不需要指定列名。
此外,您应该考虑使用parameterized queries来阻止SQL Injection
攻击。