###前提・実現したいこと
テキストボックスに入力したキー情報を元にdataGridViewに該当するレコードを表示させたい
###発生している問題・エラーメッセージ
テーブルにあるキーを入力してもelseの方に行ってしまう
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Data.SqlClient; 6using System.Drawing; 7using System.Linq; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11using System.IO; 12using System.Drawing.Printing; 13 14namespace ProductDisplay 15{ 16 public partial class FormProductDisplay : Form 17 { 18 /// <summary> 19 /// SQLserver接続変数 20 /// </summary> 21 private SqlConnection connection; 22 23 /// <summary> 24 /// ページカウント変数 25 /// </summary> 26 private int page = 1; 27 28 /// <summary> 29 /// 印刷する最大ページ数 30 /// </summary> 31 private const int MAX_PAGE = 200; 32 33 /// <summary> 34 /// ProductDisplayフォームのコンストラクタ 35 /// </summary> 36 public FormProductDisplay() 37 { 38 InitializeComponent(); 39 } 40 41 /// <summary> 42 /// FormProductDisplayのイベントハンドラ 43 /// DBに接続してseihin.txtをINSERTする 44 /// </summary> 45 /// <param name="sender">使用しない</param> 46 /// <param name="e">使用しない</param> 47 private void FormProductDisplay_Load(object sender, EventArgs e) 48 { 49 string sqlDelete = @"DELETE FROM product_table"; 50 using (connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 51 { 52 SqlCommand cmd = new SqlCommand(sqlDelete, connection); 53 connection.Open(); 54 cmd.ExecuteNonQuery(); 55 } 56 57 try 58 { 59 using (connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 60 { 61 string sql = @" 62 INSERT INTO product_table ( 63 product_id, 64 basic_order_lot, 65 product_code, 66 chassis_number, 67 assembled_ymd, 68 assembled_hms, 69 struct_number, 70 yyyymm 71 ) 72 VALUES ( 73 @product_id, 74 @basic_order_lot, 75 @product_code, 76 @chassis_number, 77 @assembled_ymd, 78 @assembled_hms, 79 @struct_number, 80 @yyyymm 81 ); 82 "; 83 84 connection.Open(); 85 SqlCommand command = new SqlCommand(sql, connection); 86 try 87 { 88 command.Parameters.Add(new SqlParameter("@product_id", connection)); 89 command.Parameters.Add(new SqlParameter("@basic_order_lot", connection)); 90 command.Parameters.Add(new SqlParameter("@product_code", connection)); 91 command.Parameters.Add(new SqlParameter("@chassis_number", connection)); 92 command.Parameters.Add(new SqlParameter("@assembled_ymd", connection)); 93 command.Parameters.Add(new SqlParameter("@assembled_hms", connection)); 94 command.Parameters.Add(new SqlParameter("@struct_number", connection)); 95 command.Parameters.Add(new SqlParameter("@yyyymm", connection)); 96 97 StreamReader read = new StreamReader("seihin.txt"); 98 try 99 { 100 string line; 101 while ((line = read.ReadLine()) != null) 102 { 103 command.Parameters["@product_id"].Value = line.Substring(0, 11); 104 command.Parameters["@basic_order_lot"].Value = line.Substring(11, 8); 105 command.Parameters["@product_code"].Value = line.Substring(19, 6); 106 command.Parameters["@chassis_number"].Value = line.Substring(25, 4); 107 command.Parameters["@assembled_ymd"].Value = line.Substring(29, 8); 108 command.Parameters["@assembled_hms"].Value = line.Substring(37, 11); 109 command.Parameters["@struct_number"].Value = line.Substring(48, 7); 110 command.Parameters["@yyyymm"].Value = line.Substring(55, 6); 111 command.ExecuteNonQuery(); 112 } 113 read.Close(); 114 } 115 catch (Exception ex) 116 { 117 MessageBox.Show("ファイルの読み込みに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 118 System.Diagnostics.Debug.WriteLine(ex.ToString()); 119 } 120 } 121 catch (Exception ex) 122 { 123 MessageBox.Show("ファイルを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 124 System.Diagnostics.Debug.WriteLine(ex.ToString()); 125 } 126 connection.Close(); 127 } 128 } 129 catch (Exception ex) 130 { 131 MessageBox.Show("データベースに接続出来ませんでした。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 132 System.Diagnostics.Debug.WriteLine(ex.ToString()); 133 } 134 } 135 136 /// <summary> 137 /// 検索ボタンのイベントハンドラ 138 /// 製品IDを元に検索してdataGridViewに表示する 139 /// </summary> 140 /// <param name="sender">使用しない</param> 141 /// <param name="e">使用しない</param> 142 private void buttonSelect_Click(object sender, EventArgs e) 143 { 144 using (connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 145 { 146 try 147 { 148 connection.Open(); 149 } 150 catch (Exception ex) 151 { 152 MessageBox.Show("データベースを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 153 System.Diagnostics.Debug.WriteLine(ex.ToString()); 154 } 155 156 string sql = @"SELECT * FROM product_table WHERE product_id = @product_id;"; 157 SqlCommand command = new SqlCommand(sql, connection); 158 command.Parameters.AddWithValue("@product_id", textBoxProductID.Text); 159 using (SqlDataReader reader = command.ExecuteReader()) 160 { 161 reader.Read(); 162 if (textBoxProductID.Text == reader["product_id"].ToString()) 163 { 164 dataGridViewProduct.Rows.Add(reader["product_id"], reader["basic_order_lot"], reader["product_code"], 165 reader["chassis_number"], reader["assembled_ymd"], reader["assembled_hms"], 166 reader["struct_number"], reader["yyyymm"]); 167 } 168 else 169 { 170 MessageBox.Show("一致する製品IDが見つかりませんでした。", "information", MessageBoxButtons.OK, MessageBoxIcon.Information); 171 } 172 reader.Close(); 173 } 174 connection.Close(); 175 } 176 }
###試したこと
GetValueなど色々試してみたのですがブレークポイントを作って値を見てみたところ何も取得できていなかったです。
SQLManagementstudioで見たところテーブルにINSERTは出来ていました
###補足情報(言語/FW/ツール等のバージョンなど)
5819718 2A11H185 2ZJHBD 5795 2000-07-26 08:31:16 8H31040 200008
が順にproduct_id,basic_order_lot,product_code,chassis_number,assembled_ymd,assembled_hms,struct_number,yyyymmです
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。