質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

SQL Server

SQL Serverはマイクロソフトのリレーショナルデータベース管理システムです。データマイニングや多次元解析など、ビジネスインテリジェンスのための機能が備わっています。

Visual Studio 2012

Microsoft Visual Studio 2012は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2010の次のバージョンです

Q&A

解決済

1回答

638閲覧

テキストボックスに入力したキー情報を元にレコードを表示させたい

hinatti

総合スコア14

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

SQL Server

SQL Serverはマイクロソフトのリレーショナルデータベース管理システムです。データマイニングや多次元解析など、ビジネスインテリジェンスのための機能が備わっています。

Visual Studio 2012

Microsoft Visual Studio 2012は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2010の次のバージョンです

0グッド

0クリップ

投稿2017/09/11 04:32

###前提・実現したいこと
テキストボックスに入力したキー情報を元に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です

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

string id = reader["product_id"].ToString();としてあげて変数idと比較したらちゃんと動作しましたー
dataGridViewの表示は
dataGridViewProduct[0, 0].Value = reader["product_id"].ToString();
dataGridViewProduct[1, 0].Value = reader["basic_order_lot"].ToString();
dataGridViewProduct[2, 0].Value = reader["product_code"].ToString();
dataGridViewProduct[3, 0].Value = reader["chassis_number"].ToString();
dataGridViewProduct[4, 0].Value = reader["assembled_ymd"].ToString();
dataGridViewProduct[5, 0].Value = reader["assembled_hms"].ToString();
dataGridViewProduct[6, 0].Value = reader["struct_number"].ToString();
dataGridViewProduct[7, 0].Value = reader["yyyymm"].ToString();
と記述してあげたら出来ました。

投稿2017/09/12 02:00

hinatti

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問