お世話になります。
ただいま検索画面の、実装をおこなっています。
#region 検索時項目が入っている場合のSQL部分でif文を使い
テキストボックスに条件が入っている場合は、そのSQLを通る条件で実装したのですが
改行を');'で区切るとエラーになってしまいます。
どのように区切ればいいのでしょうか。
パラメーターはどのようにすればSQL側に渡せるのでしょうか・
よろしくお願いします。
C#
1 ( "select" +); 2 ( " 損益.日付||曜日.曜日," +); 3 ( "損益.損益額," +); 4 ( "コメント.コメント" +); 5 ( " from" +); 6 ( " 損益テーブル 損益" +); 7 ( " left join 曜日テーブル 曜日" +); 8 ( " on 曜日.ID = 損益.曜日ID" +); 9 ( " left join コメントテーブル コメント" +); 10 ( " on コメント.日付 = 損益.日付" +); 11 if() 12 ( " where 損益.日付 >= '@'" +); 13 if() 14 ( " and 損益.日付 <= '@'" +); 15 if() 16 ( " and 損益.損益額 >= '@'" +); 17 if() 18 ( " and 損益額 <= '@'",con );
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.Data.SQLite; 11 12namespace 基幹システム.株式関係 13{ 14 public partial class kabu : Form 15 { 16 public kabu() 17 { 18 InitializeComponent(); 19 } 20 21 private void kabu_Load(object sender, EventArgs e) 22 { 23 //DB接続等 24 string dbPath = Application.StartupPath + @"\kabukanri.db"; 25 using (SQLiteConnection con = new SQLiteConnection("Data Source=" + dbPath)) 26 { 27 DataTable datatable = new DataTable(); 28 #region 画面ロード時に初期表示するためのSQL 29 SQLiteDataAdapter adapter = new SQLiteDataAdapter 30 ("select " + 31 "損益テーブル.日付 ||(曜日テーブル.曜日) as 日付" + 32 ",損益テーブル.損益額" + 33 ",コメントテーブル.コメント from 損益テーブル left join コメントテーブル on" + 34 " 損益テーブル.日付 = コメントテーブル.日付 left join" + 35 " 曜日テーブル on 損益テーブル.曜日ID = 曜日テーブル.ID; ", con); 36 adapter.Fill(datatable); 37 #endregion 38 this.dataGridView1.DataSource = datatable; 39 40 //ヘッダーを変更 41 dataGridView1.Columns[0].HeaderText = "日付"; 42 dataGridView1.Columns[1].HeaderText = "損益額"; 43 dataGridView1.Columns[2].HeaderText = "コメント"; 44 45 46 } 47 } 48 //検索ボタンを押下したときの処理 49 private void btnserch_Click(object sender, EventArgs e) 50 { 51 //string dbpath = Application.StartupPath + @"\kabukanri.db"; 52 //using (SQLiteConnection con = new SQLiteConnection("Data source=" + dbpath)) 53 //{ 54 // con.Open(); 55 // using (SQLiteTransaction trans = con.BeginTransaction()) 56 // { 57 // SQLiteCommand cmd = con.CreateCommand(); 58 59 // } 60 61 //} 62 //DB接続等 63 string dbPath = Application.StartupPath + @"\kabukanri.db"; 64 using (SQLiteConnection con = new SQLiteConnection("Data Source=" + dbPath)) 65 { 66 DataTable datatable = new DataTable(); 67 #region 検索時項目が入っている場合のSQL 68 SQLiteDataAdapter adapter = new SQLiteDataAdapter 69 ("select" +); 70 (" 損益.日付||曜日.曜日," +); 71 ("損益.損益額," +); 72 ("コメント.コメント" +); 73 (" from" +); 74 (" 損益テーブル 損益" +); 75 (" left join 曜日テーブル 曜日" +); 76 (" on 曜日.ID = 損益.曜日ID" +); 77 (" left join コメントテーブル コメント" +); 78 (" on コメント.日付 = 損益.日付" +); 79 (" where 損益.日付 >= '@'" +); 80 (" and 損益.日付 <= '@'" +); 81 (" and 損益.損益額 >= '@'" +); 82 (" and 損益額 <= '@'", con); 83 #endregion 84 adapter.Fill(datatable); 85 86 this.dataGridView1.DataSource = datatable; 87 88 } 89 90 } 91 } 92} 93
回答1件