我有一个带有各种文本框和下拉框的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);
}
}
}
}
}
}
错误: SQL逻辑错误或缺少数据库附近"(&#34 ;:语法错误。
答案 0 :(得分:1)
我发现是什么导致了这个问题,这是因为我的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 + "')");