2015-12-19 25 views
0

我的代碼會拋出主題中提到的這個錯誤。我不知道起源,因爲使用ODBC連接到它的工作原理,但使用SQL連接到SQL Server Express不起作用。如何解決它?附加信息:'?'附近語法不正確

SqlCommand Command = new SqlCommand("insert into lojas (NIF, Loja, bloqueado, DataFim, lastupdate, Nome) values (?, ?, ?, ?, ?, ?)", connection); 
Command.CommandType = CommandType.Text; 
Command.Parameters.AddWithValue("@NIF", grid_lic.CurrentRow.Cells[1].Value); 
Command.Parameters.AddWithValue("@Loja", grid_lic.CurrentRow.Cells[2].Value);   
Command.Parameters.AddWithValue("@Bloqueado", grid_lic.CurrentRow.Cells[3].Value); 
Command.Parameters.AddWithValue("@DataFim", grid_lic.CurrentRow.Cells[4].Value); 
Command.Parameters.AddWithValue("@lastupdate", grid_lic.CurrentRow.Cells[5].Value); 
Command.Parameters.AddWithValue("@Nome", grid_lic.CurrentRow.Cells[6].Value); 

connection.Open(); 
Command.ExecuteNonQuery(); 

回答

1

?在SQL Server的t-sql語法中不是有效的參數佔位符。你需要更新你的查詢有一個名爲參數:

insert into lojas (NIF, Loja, bloqueado, DataFim, lastupdate, Nome) 
values (@NIF, @Loja, @Bloqueado, @DataFim, @lastupdate, @Nome) 

當你添加的參數值,在AddWithValue調用中使用的參數的名字將被用來放值在正確的位置查詢。

+0

我認爲這是問題,但現在當我插入數據時出現了另一個問題:「無法將值NULL插入列'Id',表'Licenciamento.dbo.lojas';列不允許爲空值。 「。但是,Id在sql server中設置爲自動增量。不應該自動填充到數據網格中嗎? – TFDD