Windowsフォームでログイン画面を実装中です。
画面の入力内容とクエリの結果を照らし合わせる部分でエラーがでてしまい解決できません。
落ちるのは
//入力内容とクエリの結果を判定
の部分(56行目~)です。
エラーの内容
System.InvalidOperationException: 'No connection associated with this command'
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 login : Form 15 { 16 public login() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 if (Logintxt.Text == "") 24 { 25 MessageBox.Show("ユーザIDを入力してください。"); 26 } 27 else if(Passwordtxt.Text=="") 28 { 29 MessageBox.Show("パスワードを入力してください。"); 30 } 31 else 32 { 33 34#region SQL文 35 36 string dbPath = Application.StartupPath + @"\kabukanri.db"; 37 using (SQLiteConnection con = new SQLiteConnection("Data Source=" + dbPath)) 38 { 39 40 DataTable dt = new DataTable(); 41 StringBuilder sql = new StringBuilder(); 42 SQLiteCommand cmd = new SQLiteCommand(); 43 44 sql.AppendLine("select CD,パスワード from ログイン者"); 45 sql.AppendLine("where CD = @CD"); 46 sql.AppendLine("and パスワード = @パスワード"); 47 48 cmd.Parameters.Add("CD", System.Data.DbType.String); 49 cmd.Parameters.Add("パスワード",System.Data.DbType.String); 50 51 cmd.Parameters["CD"].Value = Logintxt.Text; 52 cmd.Parameters["パスワード"].Value = Passwordtxt.Text; 53 54 55 cmd.CommandText = sql.ToString(); 56 //入力内容とクエリの結果を判定 57 using (SQLiteDataReader reader = cmd.ExecuteReader()) 58 { 59 string CD,PW; 60 61 CD = reader.GetString(reader.GetOrdinal("CD")); 62 PW = reader.GetString(reader.GetOrdinal("パスワード")); 63 64 } 65 } 66 } 67#endregion 68 69 //TO-DOログインID・PWの一致チェックのロジック 70 Menu Menu = new Menu(); 71 //メインメニューを開く 72 Menu.Show(); 73 //ログイン画面を閉じる 74 this.Visible = false; 75 } 76 } 77} 78
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。