wpf C# SQLite 更新错误(SQL 逻辑错误或“(”: 语法错误。”) 附近的数据库丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38226225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# SQLite Update error (SQL Logic error or missing database near "(": syntax error.”)
提问by Scott E
I have a WPF application with various textboxes and a dropdown box. I am trying to update my SQLite db, with the details that are entered into them. I have re-worked the method before I used to insert the data into the SQLite db in the first place. The issue is when I execute the code I get an error “SQL Logic error or missing database near "(": syntax error.”
我有一个带有各种文本框和下拉框的 WPF 应用程序。我正在尝试使用输入的详细信息更新我的 SQLite 数据库。在我用来将数据插入 SQLite 数据库之前,我已经重新使用了该方法。问题是当我执行代码时,我收到错误“SQL 逻辑错误或在“(”附近丢失数据库:语法错误。”
private void ButtonEditExmUpdate_OnClick(object sender, RoutedEventArgs e)
{
var ModifiedDateTime = DateTime.Now;
{
var ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
using (var DbConnection = new SQLiteConnection(ConnString))
{
try
{var InsertIntoTable = (@"Update [Exm]
Set(ExmID = @ExmID, ExmEnvironmentType = @ExmEnvironmentType, ExmURL = @ExmURL, ExmServer = @ExmServer, ExmModifiedDate = @ExmModifiedDate, ExmDeleted = @ExmDeleted)
WHERE(Exm.ExmDeleted = 0) AND (Exm.ExmID = '" + ExmID.Text + "')");
var insertIntoUsernamePasswordString = (@"Update [ExmUsernamePassword]
Set (ExmUsername = @ExmUsername, ExmPassword = @ExmPassword, ExmServer = @ExmServer, ExmModifiedDate = @ExmModifiedDate)
WHERE(ExmUsernamePassword.Deleted = 0) AND (ExmUsernamePassword.ExmUsernamePasswordsID = '" + ExmUsernamePasswordsID.Text + "')");
var insertIntoExmTable = new SQLiteCommand(insertIntoExmTableString);
var insertIntoExmUsernamePasswordTable = new SQLiteCommand(insertIntoUsernamePasswordString);
insertIntoExmTable.Connection = DbConnection;
insertIntoExmUsernamePasswordTable.Connection = DbConnection;
DbConnection.Open();
insertIntoExmTable.Parameters.AddWithValue("@ExmEnvironmentType", ComboBoxExmEnvironmentType.Text);
insertIntoExmTable.Parameters.AddWithValue("@ExmURL", TextBoxExmUrl.Text);
insertIntoExmTable.Parameters.AddWithValue("@ExmServer", TextBoxExmServerName.Text);
insertIntoExmTable.Parameters.AddWithValue("@ExmModifiedDate", ModifiedDateTime);
insertIntoExmUsernamePasswordTable.Parameters.AddWithValue("@ExmUsername", TextBoxExmUsername.Text);
insertIntoExmUsernamePasswordTable.Parameters.AddWithValue("@ExmPassword", TextBoxExmPassword.Text);
insertIntoExmUsernamePasswordTable.Parameters.AddWithValue("@ExmServer", TextBoxExmServerName.Text);
insertIntoExmUsernamePasswordTable.Parameters.AddWithValue("@ExmModifiedDate", ModifiedDateTime);
try
{
insertIntoExmTable.ExecuteNonQuery();
DbConnection.Close();
DbConnection.Open();
insertIntoExmUsernamePasswordTable.ExecuteNonQuery();
DbConnection.Close();
MessageBox.Show("Successfully updated");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
}
Error: SQL Logic error or missing database near "(": syntax error.
错误:SQL 逻辑错误或“(”附近的数据库丢失:语法错误。
回答by Scott E
I found what was causing the issue, this was because my SET statements were within brackets, so the final code looks like this for the SQL strings.
我找到了导致问题的原因,这是因为我的 SET 语句在括号内,因此 SQL 字符串的最终代码如下所示。
var InsertIntoTable = (@"Update [Exm]
Set ExmID = @ExmID, ExmEnvironmentType = @ExmEnvironmentType, ExmURL = @ExmURL, ExmServer = @ExmServer, ExmModifiedDate = @ExmModifiedDate, ExmDeleted = @ExmDeleted
WHERE(Exm.ExmDeleted = 0) AND (Exm.ExmID = '" + ExmID.Text + "')");
var insertIntoUsernamePasswordString = (@"Update [ExmUsernamePassword]
Set ExmUsername = @ExmUsername, ExmPassword = @ExmPassword, ExmServer = @ExmServer, ExmModifiedDate = @ExmModifiedDate
WHERE(ExmUsernamePassword.Deleted = 0) AND (ExmUsernamePassword.ExmUsernamePasswordsID = '" + ExmUsernamePasswordsID.Text + "')");

