前提
あるサンプルを試しておりました。ボタンを押すと、adapter.Fill(dt);にて例外が発生します。SQliteについては詳しくなく、助けてほしいです。Syntax Error解決方法教えてください。
実現したいこと
データベースから値を取得
発生している問題・エラーメッセージ
場所 DDD.WinForm.Program.Main() 場所 C:\Users\kyota\source\repos\DDD\DDD\DDD.WinForm\Program.cs:行 19</ExceptionString></Exception></TraceRecord> 型 'System.Data.SQLite.SQLiteException' のハンドルされていない例外が System.Data.SQLite.dll で発生しました SQL logic error near "from": syntax error
該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Data.SQLite; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12namespace DDD.WinForm 13{ 14 public partial class WeatherLatestView : Form 15 { 16 private readonly string ConnectionString = @"Data Source=C:\DDD.db;Version=3"; 17 public WeatherLatestView() 18 { 19 InitializeComponent(); 20 } 21 22 private void LatestButton_Click(object sender, EventArgs e) 23 { 24 string sql = @" 25select DataDate, 26 Condition, 27 Temperature, 28from Weather 29where AreaId = @AreaId 30order by DataDate desc 31LIMIT 1 32"; 33 DataTable dt = new DataTable(); 34 using (var connection = new SQLiteConnection(ConnectionString)) 35 using (var command = new SQLiteCommand(sql, connection)) 36 { 37 connection.Open(); 38 39 command.Parameters.AddWithValue("@AreaId", this.AreaIdTextBox.Text); 40 using (var adapter = new SQLiteDataAdapter(command)) 41 { 42 adapter.Fill(dt); 43 } 44 } 45 46 if (dt.Rows.Count > 0) 47 { 48 DataDateLabel.Text = dt.Rows[0]["DataDate"].ToString(); 49 ConditionLabel.Text = dt.Rows[0]["Condition"].ToString(); 50 TemperatureLabel.Text = Math.Round(Convert.ToSingle(dt.Rows[0]["Temperature"]),2)+"℃"; 51 52 } 53 54 55 } 56 } 57} 58
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
何を何で作っているか書きましょう。 (例: Visual Studio 2022 で Windows Forms アプリをターゲットフレームワーク .NET Framework 4.8 で作っています)
回答1件
あなたの回答
tips
プレビュー